Results 1 to 6 of 6
  1. #1
    Join Date
    Dec 2003
    Posts
    46

    Question Unanswered: ?? If-Then in a select statement ??

    I'm pulling a recordset for the last 8 weeks of data, but need to add a true/false flag in the recordset to indicate if the record is within the last 12 hours.

    tblData.StartTime is the column that has the time for the record. I'm familiar with datediff() - but how do I check the value returned from that function to get a true/false (or 1/0) returned as a column in the select statement.

    Select *, [What do I put here] FROM tblData

    Thanks for your help,

    Alex

  2. #2
    Join Date
    Nov 2002
    Location
    Jersey
    Posts
    10,322
    Code:
    DECLARE @myDateCol datetime
    SELECT @myDateCol = '1/19/2005'
    SELECT  CASE WHEN DateDiff(hh,@myDateCol,GetDate()) < 12 THEN 1 ELSE 0 END AS TwelveHour_Ind
    Brett
    8-)

    It's a Great Day for America everybody!

    dbforums Yak CorralRadio 'Rita
    dbForums Member List
    I'm Good Once as I ever was

    The physical order of data in a database has no meaning.

  3. #3
    Join Date
    Dec 2003
    Posts
    46

    Any way to do this in a view

    I need to use this in a view...which doesn't support the case statement.

    Any other way you can think of?

    Thanks,

    Alex

  4. #4
    Join Date
    Jan 2003
    Location
    Massachusetts
    Posts
    5,800
    Provided Answers: 11
    Case statements are only not supported by Enterprise Manager. If you use Query Analyzer, you will be able to use Case statements.

  5. #5
    Join Date
    Jun 2003
    Location
    Ohio
    Posts
    12,592
    Provided Answers: 1
    Use CASE for clarity, but if you like, here is an alternative method in the spirit of that famous Computer Scientist, Dr. Rube Goldberg:

    Code:
    declare	@TestDate datetime
    set	@TestDate = '2005-01-19 14:39:29.530'
    SET	@TestDate = dateadd(hh, -12, getdate())
    
    select	cast(sign(DateDiff(hh, GetDate(), @TestDate) + 12) + 1 as bit)
    If it's not practically useful, then it's practically useless.

    blindman
    www.chess.com: "sqlblindman"
    www.LobsterShot.blogspot.com

  6. #6
    Join Date
    Nov 2002
    Location
    Jersey
    Posts
    10,322
    Open EM, and use it for refernece purposes...

    NEVER Develop in EM, use QA.

    Code:
    CREATE VIEW myView99 AS
    SELECT  CASE WHEN DateDiff(hh,@myDateCol,GetDate()) < 12 THEN 1 ELSE 0 END AS TwelveHour_Ind
    , Col2
    , Col3
    FROM myTable99
    Brett
    8-)

    It's a Great Day for America everybody!

    dbforums Yak CorralRadio 'Rita
    dbForums Member List
    I'm Good Once as I ever was

    The physical order of data in a database has no meaning.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •