Welcome to EMC Consulting Blogs Sign in | Join | Help

SSIS Junkie

Stuff I didn't know about T-SQL!

In the course of developing some stored procs today I learned some stuff I didn't know.

1) SELECT 'abc' AS column1 can be rewritten as SELECT column1 = 'abc'

2) SELECT SUBSTRING('qwerty', 1, LEN('qwerty') - 1) & SELECT SUBSTRING('qwert ', 1, LEN('qwert ') - 1) don't give the same result because LEN strips off trailing white spaces. Yes, this is stated in BOL but let's be honest....who actually reads and digests BOL? :)   Its something to be aware of anyway cos it was creating problems for me this afternoon before I discovered the cause!

 

Am I alone in not knowing these things? I'm glad to be able to say that it was also news to my 2 fellow SQL Server devs on my project.

Riveting stuff eh?

This is a fairly pointless blog post I think you'll agree. Firmly placed into my "Inane Waffle" category!!!

-Jamie

 

Published 11 August 2005 15:50 by jamie.thomson
Filed under: ,

Comments

 

Simon said:

Reminds me of the good old days in SQL 6.5 when an empty string wasn't quite so empty.

len('') = 1
August 12, 2005 21:09
 

Roelof said:

Also, did you know that any value + Null = Null.

If you concatenate 2 fields and one has a Null value, your result will be Null.

therefore, you need to use the isnull() function.

E.g.:

Select [FirstName], [Surname], Isnull([Firstname],'') + ' ' + isnull([Surname],'') as FullName
from Customer
August 15, 2005 14:51
 

AB said:

If you set CONCAT_NULL_YIELDS_NULL to OFF, then {value} + NULL will return {value}
August 15, 2005 16:47
 

Adam Machanic said:

Hey, you do have SOME readers who actually care enough to read BOL on a regular basis (guilty) :)
August 15, 2005 17:53
 

jamie.thomson said:

Ah yes Adam, but my point was that you're only going to actually read BOL if you ever encounter the problem. Forewarned is forearmed (or something) :)

-Jamie
August 16, 2005 09:00
 

Tim said:

This may all sound pretty obscure, but you can easily come unstuck with these issues. Many a time I've developed queries in Query Analyser that fail when run from within an application because of different connection settings.

Another good gotcha is

SELECT @num = ISNULL(NUMCOL, 0) FROM NUMTABLE
WHERE KEYCOL = 2

If no row is returned, you still get @num = NULL, not 0
August 25, 2005 10:21
 

James Horsley said:

How about this one:

SELECT IsNull(Varchar10Col,'No value given') FROM AnyTable

Where Varchar10Col is varchar(10) will return
'No value g' NOT 'No value given' because IsNull (silently) casts the second parm to the type of the first - same is true of Coalesce
October 31, 2005 14:29
New Comments to this post are disabled

This Blog

Syndication

Powered by Community Server (Personal Edition), by Telligent Systems