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.
| 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)) thendate2 as datetempelsedate1 as datetempI 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 |
 |
|
|
jen
Master Smack Fu Yak Hacker
4110 Posts |
Posted - 2006-06-15 : 01:42:12
|
| you can also use coalesce--------------------keeping it simple... |
 |
|
|
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? |
 |
|
|
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 queryThat returns the not null value if there is one. Thanks again |
 |
|
|
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 COALESCEMadhivananFailing to plan is Planning to fail |
 |
|
|
|
|
|