Solved

SQL Update IsNull or Coalesce?

Posted on 2014-02-01
5
276 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
Comment Utility
>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 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
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 31

Expert Comment

by:awking00
Comment Utility
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
Comment Utility
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
Comment Utility
yea it's '' instead of just null, thx
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how the fundamental information of how to create a table.

763 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now