Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

If (@Job_Name = N") SELECT @Job_Name = NULL ??? 1

Status
Not open for further replies.

steve728

Programmer
Mar 16, 2003
536
US
Will someone please explaing what the following line is doing?

If (@Job_Name = N") SELECT @Job_Name = NULL

I'm used to seeing more like:
If (@Job_Name = 'N') SELECT @Job_Name = NULL

Thanks,

Steve728
 
When you see N at the beginning of a string, it tells SQL Server that the string following it is a unicode string.

Without the N, SQL Server assumes it is an ASCII string. In your example, you are comparing to an empty string, so the N is irrelevant.



-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
Ya know...

I was just taking another look at this. That N business I said is accurate, but your code... There's a built-in function that you will find handy.

NullIf

Your code can be replaced with...

[tt][blue]Set @Job_Name = NullIf(@Job_Name, '')[/blue][/tt]

Ex.

Code:
[COLOR=blue]Declare[/color] @Job_Name nvarchar(20)

[COLOR=green]-- Test NullIf
[/color][COLOR=blue]Set[/color] @Job_Name = [COLOR=red]''[/color]
[COLOR=blue]Set[/color] @Job_Name = [COLOR=#FF00FF]NullIf[/color](@Job_Name, [COLOR=red]''[/color])
[COLOR=blue]Select[/color] @Job_Name

[COLOR=green]-- Make sure it works for non-null values.
[/color][COLOR=blue]Set[/color] @Job_Name = [COLOR=red]'Workin hard'[/color]
[COLOR=blue]Set[/color] @Job_Name = [COLOR=#FF00FF]NullIf[/color](@Job_Name, [COLOR=red]''[/color])
[COLOR=blue]Select[/color] @Job_Name

NullIf will return NULL if the first parameter (of the function) matches the second parameter.



-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top