King Chan
King Chan

Reputation: 4302

Is it possible to declare CASE statement within Replace (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'.

Upvotes: 5

Views: 29163

Answers (1)

Adam Robinson
Adam Robinson

Reputation: 185643

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
    )

Upvotes: 8

Related Questions