PositiveGuy
PositiveGuy

Reputation: 47783

Update with case statement syntax issue

I'm trying to figure out why this syntax is malformed, I do not it:

update Entry
set Name =  Case charindex('/', reverse(Name))
                when charindex('/', reverse(Name)) > 0 then right(Name, charindex('/', reverse(Name)) -1)
                when charindex('/', reverse(Name)) < 0 then Name
            End

Upvotes: 0

Views: 96

Answers (1)

JAQFrost
JAQFrost

Reputation: 1431

Try this:

update Entry 
set Name =  Case 
                when charindex('/', reverse(Name)) > 0 then right(Name, charindex('/', reverse(Name)) -1) 
                when charindex('/', reverse(Name)) < 0 then Name 
            End 

EDIT: or this:

update Entry 
set Name =  Case SIGN(charindex('/', reverse(Name)) )
                when 1 then right(Name, charindex('/', reverse(Name)) -1) 
                when -1 then Name 
            End 

CASE with an expression before WHEN compares that expressison to each expression following WHEN in turn until it finds a match. CASE immediately followed by WHEN checks each expression following WHEN until it finds one that evaluates to TRUE. In the first usage, the expressions must be comparable. In the second, the expressions must evaluate to T/F.

Upvotes: 2

Related Questions