using SELECT CASE

  • I have the following code and I am getting an 'Incorrect Syntax' error. I am using SQL Server 2005 to form a stored procedure and I am a novice at writing procedures. What I am trying to do is set a shift variable. Thanks for the information!

    SELECT thistime=Convert(varchar(10),getdate(),108)

    shift = CASE thistime

    WHEN thistime between '00:00:00' and '06:59:59' --THIRD

    'THIRD'

    WHEN thistime between '07:00:00' and '14:59:59' --FIRST

    'FIRST'

    ELSE

    'SECOND'

    END

  • Something like this?

    DECLARE @thistime AS VARCHAR(10)

    DECLARE @shift AS VARCHAR(10)

    SET @thistime = CONVERT(VARCHAR(10), Getdate(), 108)

    SELECT @thistime,

    shift = CASE

    WHEN @thistime BETWEEN '00:00:00' AND '06:59:59' --THIRD

    THEN 'THIRD'

    WHEN @thistime BETWEEN '07:00:00' AND '14:59:59' --FIRST

    THEN 'FIRST'

    ELSE 'SECOND'

    END


    Forever trying to learn
    My blog - http://www.cadavre.co.uk/
    For better, quicker answers on T-SQL questions, click on the following...http://www.sqlservercentral.com/articles/Best+Practices/61537/
    For better, quicker answers on SQL Server performance related questions, click on the following...http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

  • Yes, thanks! And I love your byline - Not a DBA, but trying to learn!

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply