Q & A · Real Scenario Logic's

SQL Question and Answer #2

Everything we see is a perspective, not the truth.

Question Pic

Question

SQL Script

SELECT $42;
SELECT $  42;
SELECT $    42;
SELECT $	42;

Q & A

Will Line 1 Query Run without any error? If ‘Yes’, what’s the Result? If ‘No’, Why it throws error?

Keep Guess….

The Answer is

Yes, Line 1 Query would Run without any error. It gives Result as ‘42.00‘. To know how it works? Check here.

Will Line 2 Query Run without any error? If ‘Yes’, what’s the Result? If ‘No’, Why it throws error?

Keep Guess….

The Answer is

If we already check/know, how Line 1 Query Works. then the answer for is ….Yes, Line 2 Query would Run without any error. It gives Result as ‘42.00‘. Because SQL Server avoids the (n) spaces(1 space here) between$‘ (Dollar Symbol) and the numeric value used(42 here). So, it treats Line 2 Query as just like Line 1 Query.

Will Line 3 Query Run without any error? If ‘Yes’, what’s the Result? If ‘No’, Why it throws error?

Keep Guess….

The Answer is

Yes, Line 3 Query would Run without any error. It gives Result as ‘42.00‘. This is because SQL Server avoids the (n) spaces (4 spaces here) between$‘ (Dollar Symbol) and the numeric value used(42 here). So, it treats Line 3 Query as just like Line 1 Query.

Is Line 3 and Line 4 Query Same? Will Line 4 Query Run without any error? If ‘Yes’, what’s the Result? If ‘No’, Why it throws error?

Keep Guess….

The Answer is

No, The Line 3 and Line 4 Query are not Same. They may be looking like same but actually different. Behind the scenes, Line 3 Query has 4 spaces while Line 4 Query has 1 TAB. So, Line 4 Query would throw error because of TAB between ‘$‘ (Dollar Symbol) and the numeric value used(42 here).

Results Pic – For Line 1,2,3 Query

Results Pic – For Line 1,2,3 Query

Error Pic – For Line 4 Query

Error Pic – For Line 4 Query

NOTE-

I have used IntelliSense and Line Numbers as key things here.

If you wish to change multiple spaces to single spaces in the above queries, Please check below blog posts-

Thanks for reading and Follow Blog and Show your Support for many more interesting upcoming Posts!

Advertisement

2 thoughts on “SQL Question and Answer #2

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s