当前位置: 动力学知识库 > 问答 > 编程问答 >

sql server - How to filter data into new column? sql

问题描述:

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 |

| |S1

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 ver_klass is M or 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 M and 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,
分享给朋友:
您可能感兴趣的文章:
随机阅读: