Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将SQL 2005表的多个列值拆分为新临时表的新列_Sql_Sql Server_Sql Server 2005 - Fatal编程技术网

将SQL 2005表的多个列值拆分为新临时表的新列

将SQL 2005表的多个列值拆分为新临时表的新列,sql,sql-server,sql-server-2005,Sql,Sql Server,Sql Server 2005,我面临的情况是,一个记录超过150000条的customers表有一个包含电话号码的列,其中包含多个值(电话),这些值由空格、逗号、破折号、点等分隔。原始列值是varchar type max 30。我需要一种方法来检查这些值,并将它们拆分为新表的相等列,然后通过删除其中的任何特殊字符来规范化它们。新列值的位数不应超过10位 下面是当前表的select查询结果,清楚地描述了当前的混乱情况。列号(Εxxxxxx)是客户唯一标识符。列电话号码是混乱的一列 - List item -**No_**

我面临的情况是,一个记录超过150000条的customers表有一个包含电话号码的列,其中包含多个值(电话),这些值由空格、逗号、破折号、点等分隔。原始列值是varchar type max 30。我需要一种方法来检查这些值,并将它们拆分为新表的相等列,然后通过删除其中的任何特殊字符来规范化它们。新列值的位数不应超过10位

下面是当前表的select查询结果,清楚地描述了当前的混乱情况。列号(
Εxxxxxx
)是
客户唯一标识符。列
电话号码
是混乱的一列

 - List item

-**No_**        **Phone No_**
-ΠΕ000586   2310836590
-ΠΕ000589   2310.443602/6977.226818
-ΠΕ000591   2310740215
-ΠΕ000593   2310228976
-ΠΕ000598   2310444604
-ΠΕ000606   2310265616/6939686560
-ΠΕ000611   2310.227932(AΔΕΡΦΗ ΚΟΚΚΑΛΑ)
-ΠΕ000621   2310826921/6979552442
-ΠΕ000626   2310846216
-ΠΕ000629   2310931574
-ΠΕ000630   6977629688, 2310320441
-ΠΕ000631   2310.260886/6973.999840
-ΠΕ000633   2310.288408/342456/6944.503637
-ΠΕ000636   2310440143/6978008313
-ΠΕ000637   2310425655/6945365400
-ΠΕ000646   944111072
-ΠΕ000652   2310.201923,6942.693372
-ΠΕ000667   2310.482194/6977394456
-ΠΕ000675   6949199051
每个由/、-或空格分隔的数字必须分隔为新列

必须删除任何文本

任何少于
10
位数的数字序列,如果序列有
6位数,则必须添加
2310前缀,如果序列有
9位数,且序列的第一位数以9开头,则必须将数字
6
作为前缀添加。 比如说

the number 342456 must become 2310342456 and the number 944111072 must become 694411072
the number 231.282414 must be 231282414 or 6942.693372 must be 6942693372
必须删除10位数字序列之间的任何点(.)才能有一个唯一的数字 比如说

the number 342456 must become 2310342456 and the number 944111072 must become 694411072
the number 231.282414 must be 231282414 or 6942.693372 must be 6942693372
任何帮助都是非常感谢的

例如

if object_id('splitNums') is not null
    drop function splitNums
go

create function splitNums(@nums varchar(30))
returns @tr table (n1 varchar(10), n2 varchar(10), n3 varchar(10), n4 varchar(10), n5 varchar(10)) as
begin
    declare @v varchar(10)
    declare @i int
    insert into @tr values (null, null, null, null, null)
    declare @ci int = 1
    while (@ci <= 5 ) begin
        if ( LEN(@nums) > 0 ) begin
            set @i = charindex('/', @nums)
            if ( @i <= 0 )
                set @i = charindex(' ', @nums)
            if ( @i <= 0 )
                set @i = charindex('-', @nums)
            if ( @i <= 0) begin
                set @v = @nums 
                set @nums = ''
            end else begin
                set @v = SUBSTRING(@nums, 1, @i - 1)
                set @nums = SUBSTRING(@nums, @i + 1, len(@nums) - @i)
            end
        end else set @v = ''
        if ( @v = '' ) break
        set @v = replace(@v, '.', '') -- more code needed here to remove text
        if ( len(@v) = 6 ) set @v = '2310' + @v
        else if (len(@v) = 9 and CHARINDEX('9', @v) = 1) set @v = '6' + @v
        -- impossible to do dynamic sql in function so...
        if (@ci = 1)
            update @tr set n1 = @v
        else if (@ci = 2)
            update @tr set n2 = @v
        else if (@ci = 3)
            update @tr set n3 = @v
        else if (@ci = 4)
            update @tr set n4 = @v
        else if (@ci = 5)
            update @tr set n5 = @v
        set @ci = @ci + 1
    end
    return
end
go

declare @t table (
    id nvarchar(10),
    number nvarchar(30)
)

insert into @t (id, number) values
   (N'ΠΕ000586', '2310836590'),
   (N'ΠΕ000589', '2310.443602/6977.226818'),
   (N'ΠΕ000633', '2310.288408/342456/6944.503637')

select * from @t cross apply dbo.splitNums(number)
go
如果对象id('splitNums')不为空
删除函数splitNums
去
创建函数splitNums(@nums varchar(30))
将@tr表(n1 varchar(10)、n2 varchar(10)、n3 varchar(10)、n4 varchar(10)、n5 varchar(10))返回为
开始
声明@v varchar(10)
声明@i int
插入@tr值(null,null,null,null,null)
声明@ci int=1
当(@ci 0)开始时
设置@i=charindex('/',@nums)

如果(@i你试过什么?为什么你的问题读起来像一个需求规范文档?太好了!为了节省我一百万泰铢的时间,如果阅读的内容不正确,我很抱歉。可能在发布之前我不得不重新措辞。我是这个领域的新手,我一定会尝试以更可接受的方式表达我的问题。我什么都没试过就像你的代码。只是简单的更新-替换查询,每次我都必须更改搜索字符、符号等;你的代码给了我一个更大的解决方案。再一次thx了很多。