Home > database >  CASE Expression in T-SQL changing other column-value
CASE Expression in T-SQL changing other column-value

Time:03-22

I'm working with T-SQL and I want to write a CASE expression. I do have a table with columns: Username and Usertype. If the Username is for example 'Demo' then I want to have the Usertype filled with 'special'. Else the Usertype should be 'normal'. I tried following:

SELECT Username, Usertype, 
    CASE Username WHEN 'Demo' THEN Usertype = 'Special' 
    END
    FROM Table1

maybe someone could help me as it doesn't work.

CodePudding user response:

You were on the right track:

SELECT Username, Usertype, 
       CASE Username WHEN 'Demo'
                     THEN 'special' ELSE 'normal' END AS UsertypeNew
FROM Table1;

If you actually want to update your table, then use:

UPDATE Table1
SET Usertype = CASE Username WHEN 'Demo' THEN 'special' ELSE 'normal' END;
  • Related