Hallo Dear Stackoverflow users.
Working with sql on sql server
I have searched the forum and the internet, but haven't found information concerning what i want to achieve. Mainly because I don't know the correct terminology.
I have created a new view, and I would like to filter out different data from a column (ver_klass) to a newly created column.
In the below picture is how my view looks like at the moment.
This is how I would like to represent the view:
ver_rayon |ver_metier |ver_secteur
96 | |
96 | |
| M10 |
| M11 |
12 | |
I want to know if it is possible to do that, and how. Thanks a lot
This can be managed with a few
CASE statements. The idea is to check if the first character of
S and take a different action on each. So in the first case, if it is neither M,S then the value goes to
ver_rayon. The second and third cases look for
S respectively and store to those columns. Since the row matches only one of the three cases, the other two are NULLed.
SELECT ... ... CASE WHEN LEFT(ver_klass, 1) <> 'S' AND LEFT(ver_klass, 1) <> 'M' THEN ver_klass ELSE NULL END AS ver_rayon, CASE WHEN LEFT(ver_klass, 1) = 'M' THEN ver_klass ELSE NULL END AS ver_metier, CASE WHEN LEFT(ver_klass, 1) = 'S' THEN ver_klass ELSE NULL END AS ver_secteur,