0

I have the following code below to select a column from my table and add 'abcd' string to all the values in my column. However, how would I go about ignoring the empty values so rows 10-14 would be empty and while rows 1-9 would remain the same?

SELECT 

vendor + 'abcd'AS [vendor_key]

FROM [table]

enter image description here

2 Answers 2

1

Use a CASE expression.

Query

select case when len(ltrim(rtrim([vendor]))) > 0 then [vendor] + 'abcd'
            else [vendor] end as [vendor_key]
from [your_table_name];
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the response however, I am getting the same values as the original column without the added string.
1

Presumably, vendor is an empty string. Just convert it to NULL in this case:

SELECT NULLIF(vendor, '') + 'abcd' AS vendor_key
FROM [table]

Note that + returns NULL if any of the arguments are NULL. This contrasts with CONCAT() which ignores NULL values.

You don't seem to have any spaces in the results, so you don't seem to have spaces in the original data -- and trim() does not seem necessary.

Comments

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.