1

I have to update a table (say table name is Venues in SQL Server conditionally i.e. if a column (say column name is Venue_Name) in Venues CONTAINS sub-string 'C_' then SET a column (say column name is Venue_Type) in Venues = Conference.

I have tried this query as:

IF Venue_Name CONTAINS 'C_'
    UPDATE Venues SET Venue_Type = 'Conference'
ELSE IF Venue_Name CONTAINS 'J_'
    UPDATE Venues SET Venue_Type = 'Journal'
ELSE
    UPDATE Venues SET Venue_Type = 'Other'  

But found error at using the keyword CONTAINS.

1 Answer 1

2

What you need is a CASE expression:

UPDATE Venues
    SET Venue_Type = CASE 
                        WHEN Venue_Name LIKE '%C[_]%' THEN 'Conference'
                        WHEN Venue_Name LIKE '%J[_]%' THEN 'Journal'
                        ELSE 'Other'
                     END
Sign up to request clarification or add additional context in comments.

1 Comment

I think you forgot to use a ESCAPE Character, the underscore will work as wildcard for any character in LIKE. It should look like this for example: Venue_Name LIKE '%C#_%' ESCAPE '#'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.