Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SQL Update IsNull or Coalesce?

Posted on 2014-02-01
5
Medium Priority
?
320 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 66

Accepted Solution

by:
Jim Horn earned 2000 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

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

715 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