SQL 2008 Split columns by condition

Source: Internet
Author: User
Tags sql 2008

in the process of reporting to the Personnel Bureau, a difficult problem was encountered, and the customer requested that a column of data in the database be divided into multiple columns according to the conditions.

For example, there are provinces in the database that have such a column of data

Customer requirements according to the province classification, Hebei province a column, Beijing City, a column of Tianjin, the rest for others, the target effect is as follows:

Manually-written SQL statements version Version1.0:

Select     Hebei = ParseName (Provice, (charindex (' Hebei ', Provice)),   Beijing = ParseName (Provice, (charindex (' Beijing ', Provice)) ),   Tianjin = ParseName (Provice, (charindex (' Tianjin ', Provice)),  other = (select Provice where charindex (' Hebei ', provice) = 0
   and charindex (' Beijing ', provice) = 0 and                            charindex (' Tianjin ', provice) = 0) from a

The following results are performed:


The user's needs just want to inquire whether this person is in this province, do not want to know exactly which city of this governorate, so use replace function to upgrade SQL statement, SQL statement Version2.0 version:

Select     Hebei =replace (ParseName (Provice, (charindex (' Hebei ', Provice)), ParseName (Provice, (charindex (' Hebei ', Provice)) ), ' √ '),   Beijing =replace (ParseName (Provice, (charindex (' Beijing '), Provice)), ParseName (Provice, (charindex (' Beijing '), Provice) ), ' √ '),   Tianjin =replace (ParseName (Provice, (charindex (' Tianjin ', Provice)), ParseName (Provice, (charindex (' Tianjin '), Provice )), ' √ '),   other =replace ((select Provice where charindex (' Hebei ', provice) = 0 and   charindex (' Beijing ', provice) = 0 and                            CHARINDEX (' Tianjin ', provice) = 0),                 (select Provice where charindex (' Hebei ', provice) = 0 and   charindex (' Beijing ', provice) = 0< C8/>and charindex (' Tianjin ', provice) = 0),                  ' √ ') from a

The following results are performed:


In the process of solving the problem, we found that there is no good solution to this kind of problem on the net, so we will share our research results with you, hoping to help you solve the problems in the development process.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.