Solved

SQL Update IsNull or Coalesce?

Posted on 2014-02-01
5
285 Views
Last Modified: 2014-02-01
I have @Price which returns null some times so I would like to use 'na' or @Reason.
I'm Not having any success so far with IsNull or Coalesce.

Declare @Price            varchar(100)
Declare @Reason            varchar(100)

Set @Price = IsNull(@Price, 'na')
-or-
Set @Price = IsNull(@Price, @Reason)
-or-
Set @Price = Coalesce(@Price, @Reason)

Exec ('Update #Temp Set [' + @LName + ' - ' + @FName + '] = ''' + @Price + '''
           Where ID = 23 ');

I'm using 'Exec' because I have dynamic column names and I cannot get @ColumnName to work, the Update is simply ignored.

Set @ColumnName = '['+ @LName +' - ' + @FName + ']'
Update #Temp Set @ColumnName = IsNull(@Price, null)
Where ID = 23
0
Comment
Question by:WorknHardr
5 Comments
 
LVL 65

Accepted Solution

by:
Jim Horn earned 500 total points
ID: 39826497
>I'm Not having any success so far with IsNull or Coalesce.
Your T-SQL looks correct.  Are you sure that there's not an empty value '', instead of NULL?

Set @Price = CASE WHEN ISNULL(@Price, '') = '' THEN 'na' ELSE @Price END

In the above T-SQL, @reason has not been assigned a value, so not sure what you mean by that.
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 39826527
if  @price is numerical, isnull/coalesce will complain about data type conversion.
so, you need to cast @price to varchar also:
isnull( cast(@price as varchar(100)), 'N/A') 

Open in new window

0
 
LVL 32

Expert Comment

by:awking00
ID: 39826583
I would assume price is a numeric field, so you really can't set it to "n/a" unless you cast it to varchar as Guy Hengel stated. However, if you need to do any type of math with price (e.g. price * quantity = total_cost), you won't be able to. Perhaps you should just consider that, if the price is null, to be 0 instead, then you could use coalesce(@price,0).
0
 
LVL 16

Expert Comment

by:Surendra Nath
ID: 39826949
I think the @FNAME, @LNAME also containing the nulls, it would be better to add the isnull for them and put a default column name over there.
that way the update might still work
0
 

Author Closing Comment

by:WorknHardr
ID: 39827128
yea it's '' instead of just null, thx
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question