Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
DB Engine
SQL ServerMSDESQL Server CE
Services
Analysis (Data Mining)Analysis (OLAP)DTSIntegration ServicesNotification ServicesReporting Services
Programming
CLRConnectivitySQLXML
Other Technologies
ClusteringEnglish QueryFull-Text SearchReplicationService Broker
General
Data WarehousingPerformanceSecuritySetupSQL Server ToolsOther SQL Server Topics
DirectoryUser Groups
Related Topics
MS AccessOther DB ProductsMS Server Products.NET DevelopmentVB DevelopmentJava DevelopmentMore Topics ...

SQL Server Forum / General / Other SQL Server Topics / January 2008

Tip: Looking for answers? Try searching our database.

Select statement query

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jules - 29 Jan 2008 14:35 GMT
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
Plamen Ratchev - 29 Jan 2008 14:48 GMT
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
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.