Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to declare CASE statement within Replace (SQL Server)?

Tags:

sql

sql-server

Just as titled, is it possible?

So is like the following:

SELECT 
REPLACE ('Hello', 'e', '!')
REPLACE(
    CASE 
        WHEN 1 = 1 THEN 'Hello'
        ELSE 'Bye'
    END AS MyStr, 'e', '!'
    )

Because it returns:

Msg 156, Level 15, State 1, Line 4 Incorrect syntax near the keyword 'CASE'.

like image 381
King Chan Avatar asked Mar 05 '12 22:03

King Chan


1 Answers

You have syntax errors in your query, but apart from that things look correct. It should be

SELECT 
REPLACE ('Hello', 'e', '!'), -- missing comma in the original query
REPLACE(
    CASE 
        WHEN 1 = 1 THEN 'Hello'
        ELSE 'Bye'
    END, 'e', '!' -- removed the AS clause
    )
like image 132
Adam Robinson Avatar answered Nov 15 '22 03:11

Adam Robinson