Sunday, March 11, 2012

displaying null dates

how to display NA in a date field if the date is NULL? Please Help.

select case when <Col Name> is null then 'NA'

else cast(<Col Name> as varchar(12))end <Col Name>

from <Table Name>

Mat

|||

You can use the following query..

Select Isnull(Convert(Varchar,DateColumnName,101),'NA') From YourTable

Here the date value will be converted as US format..

If you need both time & date use the following query..

Select Isnull(Convert(Varchar,DateColumnName,101) + ' ' + Convert(Varchar,DateColumnName,114),'NA') From YourTable

You can find all the date format convertion on Books Online under the title CAST and CONVERT

No comments:

Post a Comment