简体   繁体   中英

Find and specific pattern and extract data from a column having string values

Using MSSQL Server 2012

I have a column name LocationName with the following data in it.

LocationName
C1-Highland
C687-I-10 & 51st
C74-Bossier
C0716-South Broadway & Cluff
Las Vegas

I want to find only those records which is having pattern like {CXXXX} where XXXX can be any number between 0-9.

SELECT
     CASE 
        WHEN LocationName like 'C%' THEN SUBSTRING(LocationName, 0, charindex('-', LocationName, 
        0))
        ELSE 'Unknown'+ '-' + LocationName
    END AS storebusinesskey,* 
FROM [DBO].[Store]

The problem with this code is if I have location name start with C but not having pattern {CXXXX} then also I'm getting this record,which I didn't want.

You can use [] to use regex that matches what you want

You said that XXXX can be any number between 0-9

If you want the first 4 digit is number 0-9, then you need to repeat [] 4 times

SELECT * FROM [DBO].[Store]
WHERE LocationName like '[C]'+ REPLICATE('[0-9]', 4) + '%'

If you just want first digit is number 0-9, then you can simply to the following:

SELECT * FROM [DBO].[Store]
WHERE LocationName like '[C][0-9]%'

If you want four or more digits, you can use:

SELECT (CASE WHEN LocationName like 'C[0-9][0-9][0-9][0-9]%'
             THEN SUBSTRING(LocationName, 1, charindex('-', LocationName, 0))
             ELSE 'Unknown'+ '-' + LocationName
        END) AS storebusinesskey,* 
FROM [DBO].[Store];

Note: This logic is a little strange because the WHEN condition is not guaranteeing that there is a hyphen.

If you want exactly four digits -- and no more -- then you need to tweak the logic a bit:

SELECT (CASE WHEN LocationName + 'x' like 'C[0-9][0-9][0-9][0-9][^0-9]%'
             THEN SUBSTRING(LocationName, 1, charindex('-', LocationName, 0))
             ELSE 'Unknown'+ '-' + LocationName
        END) AS storebusinesskey,* 
FROM [DBO].[Store];

This checks that the following character is not a digit. The + 'x' simply ensures that the check works if the C#### value is at the end of the string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM