Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
DB Engine
SQL ServerMSDESQL Server CE
Services
Analysis (Data Mining)Analysis (OLAP)DTSIntegration ServicesNotification ServicesReporting Services
Programming
CLRConnectivitySQLXML
Other Technologies
ClusteringEnglish QueryFull-Text SearchReplicationService Broker
General
Data WarehousingPerformanceSecuritySetupSQL Server ToolsOther SQL Server Topics
DirectoryUser Groups
Related Topics
MS AccessOther DB ProductsMS Server Products.NET DevelopmentVB DevelopmentJava DevelopmentMore Topics ...

SQL Server Forum / General / Other SQL Server Topics / March 2008

Tip: Looking for answers? Try searching our database.

testing if value is null

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
laredotornado - 31 Mar 2008 16:12 GMT
Hi,

How do I write a query where if a column, "value," is NULL I return
the phrase "No value entered" but otherwise return the column's value,
even if it is the empty string?  I'm tried to modify this simple query

SELECT value FROM meta_data

Thanks, - Dave
Plamen Ratchev - 31 Mar 2008 16:43 GMT
You can use COALESCE:

SELECT COALESCE(value, 'No value entered')
FROM meta_data

It is important to note that COALESCE returns the higher precedence data
type from the parameters expressions, so this will work fine with character
columns but you will get conversion errors with numeric data types that have
higher precedence. See the example below:

SELECT COALESCE(value, 'No value entered')
FROM (SELECT 10.5
         UNION ALL
         SELECT NULL) AS T(value)

To fix you can cast the numeric value to character data type:

SELECT COALESCE(CAST(value AS VARCHAR(10)), 'No value entered')
FROM (SELECT 10.5
         UNION ALL
         SELECT NULL) AS T(value)

HTH,

Plamen Ratchev
http://www.SQLStudio.com
christopher.secord@gmail.com - 31 Mar 2008 19:47 GMT
> How do I write a query where if a column, "value," is NULL I return
> the phrase "No value entered" but otherwise return the column's value,
> even if it is the empty string?  I'm tried to modify this simple query

You can also use a case statement.

select column1, column2,
value = case when value is null then 'No Value Entered' else value
end,
column4
from meta_data
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.