SQL help (numpty alert)
Author
Discussion

aldi

Original Poster:

9,292 posts

267 months

Monday 11th September 2006
quotequote all
As part of a larger query I need to do this:


IF that field is not blank
select an entry from a varchar field in table 1
ELSE
Select a date from a date field in table 2 (which is never blank)



This task has beaten my barely mentionable SQL skills, can anyone point me in the right direction?

Thanks!

Don

28,378 posts

314 months

Monday 11th September 2006
quotequote all
SELECT
CASE
WHEN ISNULL(TABLE1.VARCHARFIELD,'') = '' THEN CONVERT(varchar(50),TABLE2.DATEFIELD)
ELSE VARCHARFIELD
END AS THEFIELDIWANTED
FROM TABLE1
INNER JOIN TABLE2 on TABLE2.JOINFIELD = TABLE1.JOINFIELD


This sort of thing. You need the "CASE" clause. You can always shove subqueries in if you need.

aldi

Original Poster:

9,292 posts

267 months

Wednesday 13th September 2006
quotequote all
That worked a treat, thankyou very much!!

Neil

Don

28,378 posts

314 months

Wednesday 13th September 2006
quotequote all
aldi said:
That worked a treat, thankyou very much!!

Neil


You're welcome!