By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
460,044 Members | 1,172 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 460,044 IT Pros & Developers. It's quick & easy.

Select statement query

P: n/a
In my select statement, I return a column for 'datediff' using a CASE
query. I call this column 'Elapsed_days'

is there anyway I can use this result later on in the same select? IE
I want to refer to 'elasped days' in another CASE query rather than
have to re-write something which incorporates the original one.

Simpler the better - I'm new!

Make any sense?
Hope so
Jan 29 '08 #1
Share this Question
Share on Google+
1 Reply


P: n/a
You cannot directly reference an aliased column in the same SELECT
statement, you would have to repeat the expression. However, you can use a
derived table (or a Common Table Expression if on SQL Server 2005 -
http://msdn2.microsoft.com/en-us/library/ms190766.aspx). Here is an example
using derived table:

SELECT elapsed_days,
CASE WHEN elapsed_days 60
THEN 'Over 60 days'
ELSE 'Less than 60 days'
END AS msg
FROM (SELECT CASE
WHEN 1 = 1
THEN DATEDIFF(dd, '20080118', '20080120')
END
) AS T (elapsed_days)
HTH,

Plamen Ratchev
http://www.SQLStudio.com

Jan 29 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.