Skip to content
Advertisement

SQL computed column based on a special character on another column

I am not good with SQL at all, barely have an idea on how to do basic scripts suck as delete, drop, add.

I have this data with about 12 columns, I want to add a calculated column which will change depending if a special character shows up in another column.

lets say

A                 C   
Money$            YES
Money             NO

that is the idea, I want to create a column C where it says yes if there is a $ sign on the column A. Is this possible? I am assuming you can use something similar to an if condition but I have no experience with SQL scripting.

Advertisement

Answer

You would use a case expression and like:

select t.*,
       (case when a like '%$%' then 'YES' else 'NO' end) as c
from t;

The following is just commentary.

This is very basic syntax for SQL. I would recommend that you spend some time to learn the basics. Learning-as-you-go is an okay approach — assuming you have some fundamentals to build on. Otherwise, you are likely to spend a lot of time to learn a few things, and you may not learn the best way to do things.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement