Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Conditional Statements in SQL

Author  Topic 

durilai
Starting Member

13 Posts

Posted - 2006-06-15 : 00:45:58
I am trying to apply some logic to a sql query. I have a table with two date fields (date1 and date2) I want to select the value of date2 if it is not null, but if it is I want to use date1. I would like to place either value as an alias. Something similar to

if (NOT(date2 IS NULL)) then
date2 as datetemp
else
date1 as datetemp

I have been looking as CASE statement, but have had no luck, I would also like to aply this within the AND part of the query, if possible.

timmy
Master Smack Fu Yak Hacker

1242 Posts

Posted - 2006-06-15 : 01:38:32
Why not use ISNULL?

SELECT ISNULL(date2, date1) as datetemp.

HTH,

Tim
Go to Top of Page

jen
Master Smack Fu Yak Hacker

4110 Posts

Posted - 2006-06-15 : 01:42:12
you can also use coalesce



--------------------
keeping it simple...
Go to Top of Page

durilai
Starting Member

13 Posts

Posted - 2006-06-15 : 02:25:36
WIth the ISNULL would I do something like this

...AND (NOT(ISNULL(date2, date1) as datetemp...

Would that place the not null field as datetemp?
Go to Top of Page

durilai
Starting Member

13 Posts

Posted - 2006-06-15 : 02:50:41
Thanks for the COALESCE tip. This solved my problem:

SELECT *, COALESCE (date2, date1) AS datetemp FROM query

That returns the not null value if there is one. Thanks again
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-06-15 : 02:58:37
As you use only two parameters, ISNULL also works in place of COALESCE

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -