26

I'm running a really simple query, however for some of the results the value in one field is null. How can I set that value to "a string" if its value is null?

Something like

SELECT RegName,
    RegEmail,
    RegPhone,
    RegOrg,
    RegCountry,
    DateReg,
    (Website IS NULL? 'no website' : Website) AS WebSite
FROM RegTakePart
WHERE Reject IS NULL

It will be running on a sql server 2005

thanks

2
  • 1
    Look at the ISNULL or COALESCE functions. Commented Oct 9, 2013 at 10:00
  • Thanks everyone I'll accept the answer as soon as it will allow me Commented Oct 9, 2013 at 10:05

6 Answers 6

45

Use the following:

SELECT RegName,
       RegEmail,
       RegPhone,
       RegOrg,
       RegCountry,
       DateReg,
       ISNULL(Website,'no website')  AS WebSite 
FROM   RegTakePart 
WHERE  Reject IS NULL

or as, @Lieven noted:

SELECT RegName,
       RegEmail,
       RegPhone,
       RegOrg,
       RegCountry,
       DateReg,
       COALESCE(Website,'no website')  AS WebSite 
FROM   RegTakePart 
WHERE  Reject IS NULL

The dynamic of COALESCE is that you may define more arguments, so if the first is null then get the second, if the second is null get the third etc etc...

Sign up to request clarification or add additional context in comments.

2 Comments

take with ISNULL if you migrate to MySQL. The sintax there is different w3schools.com/sql/sql_isnull.asp
Not relevant to this example, but still noteworthy, the downside of COALESCE is that it expands to a case statement, so COALESCE((SELECT Col FROM T), 'No Value'), expands to CASE WHEN (SELECT Col FROM T) IS NOT NULL THEN (SELECT Something FROM Table) ELSE 'No Value' END meaning the subquery is executed twice if the result is not null. ISNULL does not exhibit this behaviour. An upside of coalesce is that it is in the ANSII Standard, ISNULL() is not.
11

As noted above, the coalesce solution is preferred. As an added benefit, you can use the coalesce against a "derived" value vs. a selected value as in:

SELECT
       {stuff},
       COALESCE( (select count(*) from tbl where {stuff} ), 0 )  AS countofstuff
FROM
       tbl
WHERE
       {something}

Using "iif" or "case" you would need to repeat the inline whereas with coalesce you do not and it allows you to avoid a "null" result in that return...

1 Comment

Wrapping the first argument of the COALESCE with () is very important, or the query will not compile. Thank you for this
9

Use CASE:

SELECT regname, 
       regemail, 
       regphone, 
       regorg, 
       regcountry, 
       datereg, 
       CASE 
         WHEN website IS NULL THEN 'no website' 
         ELSE website 
       END AS WebSite 
FROM   regtakepart 
WHERE  reject IS NULL 

or COALESCE:

....
COALESCE(website, 'no website') AS WebSite 
....

Comments

2

You just use ISNULL(website, 'yourstring').

So, your query will be like:

SELECT RegName,
    RegEmail,
    RegPhone,
    RegOrg,
    RegCountry,
    DateReg,
    ISNULL(website, 'no website') AS WebSite
FROM RegTakePart
WHERE Reject IS NULL

1 Comment

No matter what answer you choose, they will all work. COALESCE and ISNULL are both solutions.
2

To avoid some problems to other users :

Problem 1 + Solution :

If you are planning to use the ISNULL function with an Access Database, be aware of the fact that the ISNULL function implemented in Access is differrent from the SQL Server implementation.

Access ISNULL function always returns TRUE or FALSE. So you have to use a custom IIF function around your ISNULLusage.

Problem 2 + Solution :

Moreover, if you wish to use the same field name as Alias for your column, this could lead the Access Database Engine to inform you that there is a "circular reference" to the field.

So if you need to use the same field name as Alias, you just have to add the table name before the field name. (example : You have to use RegTakePart.Website instead of simply Website). This way, you can freely use Website as Alias name for your column.

The global working (and tested) SQL query to avoid these 2 problems is the following one :

SELECT RegName,
    RegEmail,
    RegPhone,
    RegOrg,
    RegCountry,
    DateReg,
    IIF(RegTakePart.Website IS NULL, 'no website, RegTakePart.Website) AS Website
FROM RegTakePart
WHERE Reject IS NULL

Comments

0

Check the COALESCE function

http://msdn.microsoft.com/en-us/library/ms190349.aspx

SELECT RegName,RegEmail,RegPhone,RegOrg,RegCountry,DateReg,COALESCE(Website, 'no website') AS WebSite FROM RegTakePart WHERE Reject IS NULL

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.