Unix Technical Forum

Select statement query

This is a discussion on Select statement query within the SQL Server forums, part of the Microsoft SQL Server category; --> In my select statement, I return a column for 'datediff' using a CASE query. I call this column 'Elapsed_days' ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 03-01-2008, 03:48 PM
Jules
 
Posts: n/a
Default Select statement query

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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 03-01-2008, 03:48 PM
Plamen Ratchev
 
Posts: n/a
Default Re: Select statement query

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

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 08:13 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com