字符串拆分基于条件

时间:2018-03-19 18:40:58

标签: sql-server sql-server-2016

我的字符串很少,就像这样;及其约3000条记录。

            Column
            ------------
            Cell 233567-3455
            Cell123-4567
            Cell#123-7449
            Local 456-0987
            1 616 468-7796
            1234567-5x2345
            234/625-1234
            (C)755-7442
            5732878-2
            5721899-23
            6712909-3
            7894200-234
            2144-57238
            5673893/588218
            437-4737-5772

我如何找到如下记录:

            Column
            -------------
            5732878-2
            5721899-23
            6712909-3
            7894200-234

一旦找到了这个,我需要将它们分成两部分

            1st Column.    |  2nd column
            -------------  |
            5732878        |   5732872      
            5721899        |   5721823
            6712909        |   6712903
            7894200        |   7894234

我尝试使用PARINDEX和CHARINDEX解决这个问题 但不知怎的,它不起作用。请帮助。

4 个答案:

答案 0 :(得分:1)

我不知道你的过滤逻辑到达你的中间集,但这应该得到你期望的最终结果集。我假设你只想要连字符左边的字符串长度大于右边长度的记录,并且还要排除多于1个连字符的记录。

SELECT LEFT(telephone, CHARINDEX('-', telephone)-1) AS [1stTelephone],
STUFF(
--get the string before the hyphen
LEFT(telephone, CHARINDEX('-', telephone)-1), 
--get the starting location of chars we are going to replace
LEN(LEFT(telephone, CHARINDEX('-', telephone)))-LEN(RIGHT(telephone, CHARINDEX('-', REVERSE(telephone))-1)), 
--get the length of the section we are replacing
LEN(RIGHT(telephone, CHARINDEX('-', REVERSE(telephone))-1)),
--replace that section with the string after the hyphen
RIGHT(telephone, CHARINDEX('-', REVERSE(telephone))-1)
) AS [2nd telephone]

FROM your_table

WHERE LEN(LEFT(telephone, CHARINDEX('-', telephone))) > LEN(RIGHT(telephone, CHARINDEX('-', REVERSE(telephone))))
AND len(telephone) - len(REPLACE(telephone, '-', '')) = 1

答案 1 :(得分:0)

您可以使用以下内容:

DDL

use tempdb

create table TelNo (
    Tel varchar(30)
)

insert TelNo(Tel)
values ('5732878-2'),
('5721899-23'),
('6712909-3'),
('7894200-234'),
('2144-57238'),
('5673893/588218'),
('437-4737-5772')

代码

select Tel,
    case 
        when Tel like '%_-[0-9]' then left(Tel, len(Tel)-2) 
        when Tel like '%__-[0-9][0-9]' then left(Tel, len(Tel)-3) 
        when Tel like '%___-[0-9][0-9][0-9]' then left(Tel, len(Tel)-4) 
        else Tel 
    end Tel1,
    case 
        when Tel like '%_-[0-9]' then left(Tel, len(Tel)-3) + right(Tel, 1)
        when Tel like '%__-[0-9][0-9]' then left(Tel, len(Tel)-5) + right(Tel, 2)
        when Tel like '%___-[0-9][0-9][0-9]' then left(Tel, len(Tel)-7) + right(Tel, 3)
        else NULL 
    end Tel2
from TelNo

答案 2 :(得分:0)

有些脏的方法(专门针对7位数字后跟连字符后跟任意数字的数字):

SELECT BasePhone AS Phone1, LEFT(BasePhone, 7-LEN(OtherPhoneEnd)) + OtherPhoneEnd AS Phone2
FROM (
    SELECT LEFT(Telephone, 7) AS BasePhone, SUBSTRING(Telephone,9,7) AS OtherPhoneEnd 
    FROM Telephones 
    WHERE Telephone LIKE '[0-9][0-9][0-9][0-9][0-9][0-9][0-9]-%'
    )

答案 3 :(得分:0)

我根据您给出的信息假设您想要在第8个位置使用带连字符( - )的数字。试试这个:

create table #TelNo (
    Tel varchar(30)
)

insert #TelNo(Tel)
values ('5732878-2'),
('5721899-23'),
('6712909-3'),
('7894200-234'),
('2144-57238'),
('5673893/588218'),
('437-4737-5772')

select Tel, LEFT(Tel, Len(tel) - len(suffix)) + suffix [SecondTel] from (
    select substring(Tel, 1, 7) [Tel], substring(Tel, 9, 10) [suffix] from #TelNo
    where CHARINDEX('-', Tel) = 8
)a