Adrian Reszka Adrian Reszka - 4 months ago 7
SQL Question

Change columns string value inside a strore procedure based on condition

I'am using Microsoft SQL Server, and was wandering if its possible to create a condition inside a procedure that will check for a string value, and replace it.

For example something like,
if [column name] = 'Adam'
then [column name] = 'BoB'


Also if this condition could be inside another select Case

eq:

SELECT
a.projectName,
a.user
Case
when a.TYPEID = 2 THEN
-- condition / change name
concat(a.[MESSAGE] ,a.Name)
From some_table a

Answer

case constructs can be nested, see here:

SELECT
    a.projectName,
    a.user,
    Case when a.TYPEID = 2 THEN
      concat(a.[MESSAGE] ,
       Case a.Name when 'Adam' Then 'Bob' else a.Name end)
    End msg
From some_table a

This is assuming that you want to concat the optionally changed name to the MESSAGE. Concatenation can also be done by using the + operator in sql-server.

It is not quite clear from your question what you want to happen, if TYPEID != 2. Currently nothing (null) gets output.