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 John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I replace an empty string with a NULL value? 2

Status
Not open for further replies.

RebLazer

Programmer
Jun 7, 2002
438
US
I have a varchar variable: [tt]@NAME[/tt]. Is there a very concise way to check if it contains an empty string - and, if it does, replace it with a NULL?

I'd think it's something like this:
replace (@NAME, '', NULL)
but, alas, this always returns a NULL, even if the string has a regular (i.e. non-empty) value...

Thanks very much,
Lazer
 
if @name = ''
set @name = NULL


Something like this????
 
Checkai,

Thanks for the lighting fast response(s)! :)

Well, not exactly...

[tt]declare @name varchar(50)
set @name = ''
select isnull(@name, '')[/tt]

I need the above to return a NULL - and it returns an empty string...

Thanks,
Lazer
 
declare @name varchar(50)
set @name = NULL
select @name as Name

DLC
 
Checkai,

I know how to set a variable to null. :) This is the issue: I have a @name variable - it could contain any varchar value. I, therefore, don't want to indiscriminately set it to null. I only want it set to null if the value is an empty string ('').

Lazer
 
If isnull(@Name,'') = ''
Set @Name = NULL
else
--Do Nothing, right?

or

Select case when isnull(@Name,'') = '' then NULL else @name end as Name

dlc
 
Checkai,

[tt]Select case when isnull(@Name,'') = '' then NULL else @name end as Name[/tt]

Of course - a case statement! Thanks so much for all of your efforts!

Lazer
 
Code:
select nullif(@name,'') as Name

nullif is a shorthand for the case expression used in the previous post.
 
SwampBoogie - that is exactly what I was looking for![star]!

[thumbsup] [thumbsup] [thumbsup] [thumbsup] [thumbsup]
Lazer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top