tags:

views:

534

answers:

5

Hi, I am trying to construct a sql select statement that does the following:

SELECT GetDate() Mon, GetDate() Tues

But I also want to be able to put WHERE restrictions on each column (Mon, Tues). So I want something like this, but it is not correct:

SELECT (GetDate() Mon WHERE (1=1)), (GetDate() TuesWHERE (1=1))

Please point me in the right direction, thanks.

A: 
SELECT (select GetDate() Mon WHERE (1=1)), (select GetDate() Tues WHERE (1=1))
ck
how come the column names arn't showing up now?
Oliver S
SELECT (select GetDate() WHERE (1=1)) Mon, (select GetDate() WHERE (1=1)) Tues
Spencer Ruport
+1  A: 

With column names:

select
    (select getDate() where (1=1)) Mon,
    (select getDate() where (2=2)) Tues
Terrapin
I'm gonna go out on a limb and guess that the GETDATE function and WHERE 1=1 are just examples, and that in reality his query is much more complex. In which case, Terrapin's answer is probably what he's looking for, to maintain the column names. Just substitute the real functions and where clauses.
BradC
+1  A: 

Do you perhaps want a CASE statement?

Or are you trying to actually get the most recent monday/tuesday/wednesday/etc?

Joel Coehoorn
+2  A: 

GetDate() just retrieves the current date/time.

So all your original query (and most of the "answers" so far) does is retrieving the current date and time twice, then putting a "Mon" and "Tue" column label on it.

Is this really what you want?

BradC
+1  A: 

Here is a function that takes a datetime and a day of the week as an input. It returns the most recent date with that day of the week.

It assumes a DATEFIRST of 7 (Making sunday day 1).

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[udf_getDateFromDayName]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[udf_getDateFromDayName]
GO

--Set Sunday to be day one
SET DATEFIRST 7
GO

CREATE FUNCTION [dbo].[udf_getDateFromDayName] 
(
    @dtsMaxDate DATETIME,
    @sDayName VARCHAR(8)
)
RETURNS DATETIME
AS
BEGIN

    DECLARE @iDayNumber INT, @dtsTemp DATETIME

    SET @sDayName = LEFT(UPPER(@sDayName),3)

    SET @iDayNumber =
     ( CASE
       WHEN @sDayName = 'SUN' THEN 1
       WHEN @sDayName = 'MON' THEN 2
       WHEN @sDayName = 'TUE' THEN 3
       WHEN @sDayName = 'WED' THEN 4
       WHEN @sDayName = 'THU' THEN 5
       WHEN @sDayName = 'FRI' THEN 6
       WHEN @sDayName = 'SAT' THEN 7
       ELSE 0 --Invalid day
        END
     )

    IF @iDayNumber = 0 RETURN NULL

    SET @dtsTemp = CAST(FLOOR(CAST(@dtsMaxDate AS FLOAT)) AS DATETIME)

    WHILE DATEPART(DW,@dtsTemp) <> @iDayNumber
     SET @dtsTemp = DATEADD (dd, -1, @dtsTemp)

     RETURN @dtsTemp
END

GO

Once you have the function, your query is simple.

You can do something like this:

SELECT  dbo.udf_getDateFromDayName(GETDATE(),'mon') [Mon],
     dbo.udf_getDateFromDayName(GETDATE(),'tue') [Tue],
     dbo.udf_getDateFromDayName(GETDATE(),'wed') [Wed],
     dbo.udf_getDateFromDayName(GETDATE(),'thu') [Thu],
     dbo.udf_getDateFromDayName(GETDATE(),'fri') [Fri],
     dbo.udf_getDateFromDayName(GETDATE(),'sat') [Sat],
     dbo.udf_getDateFromDayName(GETDATE(),'sun') [Sun]

Or ideally, put the max date in a variable first:

DECLARE @dtsNow DATETIME
SET @dtsNow = GETDATE()
SELECT  dbo.udf_getDateFromDayName(@dtsNow,'mon') [Mon],
     dbo.udf_getDateFromDayName(@dtsNow,'tue') [Tue],
     dbo.udf_getDateFromDayName(@dtsNow,'wed') [Wed],
     dbo.udf_getDateFromDayName(@dtsNow,'thu') [Thu],
     dbo.udf_getDateFromDayName(@dtsNow,'fri') [Fri],
     dbo.udf_getDateFromDayName(@dtsNow,'sat') [Sat],
     dbo.udf_getDateFromDayName(@dtsNow,'sun') [Sun]
John