CASE Statement within CASE Statement

  • Is it possible to do a CASE within a CASE statement? I'll explain:

    EnteredByUserName = Max(Case When [Index] = 1 Then [Value] Else '' End),

    If the value of Index is 1 and the DateEntered is before 6/27/09, the value of EnteredByUserName would be "jdoe", but if the DateEntered is from 6/27/09 on, then the value of EnteredByUserName would be "Domain\jdoe, then if the value of Index is not 1 then leave it blank.

    I'm testing several syntax.

    Thx,

    John

  • Yes

    by way of example

    select case when 10>11 then 1 else case when 8>9 then 2 else -9 end end



    Clear Sky SQL
    My Blog[/url]

  • Ok, I'll give that a try.

    On a somewhat different subject, if I need to check for the presence of character '\' in a field, will this work:

    Case When CharIndex('\',[Value]) = '\' Then

    Right([Value],CharIndex('\',[Value)+1) Else [Value] End

    Basically it means if there's a char '\' in the [Value] field then pass everyithing after the char '\' else pass the whole [Value] field.

  • This is what you need

    declare @User varchar(30)

    Select @User ='Domain\User'

    Select Case When CharIndex('\',@User) >0 Then

    Right(@User,DataLength(@User)-CharIndex('\',@User)) Else @User End

    Although this a better solution IMO

    declare @User varchar(30)

    Select @User ='Domain\User'

    Select substring(@User,CharIndex('\',@User)+1,999)

    Select @User ='User'

    Select substring(@User,CharIndex('\',@User)+1,999)



    Clear Sky SQL
    My Blog[/url]

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

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