无法将值NULL插入列'col' - SQL Server

时间:2018-01-31 06:19:25

标签: sql-server sql-server-2008-r2 sql-server-2016

我的一栏中有这样的数据:我有大约100k这样的记录。

            tele
            ---------

            C12345
            75784329899(c)
            75678934729(cell)
            ygasd786782399
            guisahkl#57812897
            5476783-6779834

我已编写此代码以删除所有字母并仅保留数字。哪个工作正常。

            create TABLE #TEMP
            (
            id [int] NOT NULL,
            tele [char](14),
            col [bigint] NOT NULL DEFAULT ''
            )
            ;with cte as (select top (100) N = row_number() over

            (order by @@spid) from sys.all_columns),

            data as(

            select id,tele,col

                from table1` 
                cross apply (
                    select (select C + ''
                        from (select N, substring( tele,N,1) C from cte where N <= datalength(tele)) [1]
                        where C between '0' and '9'
                        order by N
                        for xml path(''))
                        ) P(col)

                    where p.col is not null 

----------------插入临时表--------------------------- ------------

            INSERT INTO #TEMP (id,tele,col) 
            SELECT id,tele,cast(col as bigint) 
            FROM data 



            tele
            ---------

            12345
            75784329899
            75678934729
            786782399
            57812897
            54767836779834  

然而,现在我想保留所有的单元格号并仅删除垃圾(或未知的字符) 我的结果应该是这样的:

                tele                           col
            -----------------------------------------------------

            C12345                          C12345
            75784329899(c)                  75784329899(c)
            75678934729(cell)               75678934729(cell)
            ygasd786782399                  786782399
            guisahkl#57812897               57812897
            5476783-6779834                 54767836779834

我已将代码修改为beloew:

            create TABLE #TEMP
            (
            id [int] NOT NULL,
            tele [char](14),
            col [bigint] NOT NULL DEFAULT ''
            )
            ;with cte as (select top (100) N = row_number() over

            (order by @@spid) from sys.all_columns),

            data as(

            select id,tele,col

                from table1` 
                cross apply (
                    select (select C + ''
                        from (select N, substring( tele,N,1) C from cte where N <= datalength(tele)) [1]
                        where C between '0' and '9'
                        order by N
                        for xml path(''))
                        ) P(col)

                    where p.col is not null AND

                      col NOT LIKE '%(CELL)%' 
                      OR col NOT LIKE  '%CELL%' OR col NOT LIKE '%(C)%'
                      OR col NOT LIKE '%C%' OR col NOT LIKE  '%MOBILE%'
                      OR col NOT LIKE  '%X%' OR col NOT LIKE '%EXT%'
                      OR col NOT LIKE '%XT%' OR col NOT LIKE '%EX%' 
                      OR col NOT LIKE '%/%' OR col NOT LIKE '%,%'
                               )

----------------插入临时表--------------------------- ------------

            INSERT INTO #TEMP (id,tele,col) 
            SELECT id,tele,cast(col as bigint) 
            FROM data 

然而,它向我显示了这个错误:

        Cannot insert the value NULL into column 'col', table 'tempdb.dbo.#TEMP_______________________________________________________________________________________________________________000000000125';
        column does not allow nulls. INSERT fails.

我该如何纠正?  请帮忙。感谢。

3 个答案:

答案 0 :(得分:0)

删除列col。

的临时表的CREATE语句中的NOT NULL约束

答案 1 :(得分:0)

  1. 从表创建中删除NOT NULL约束。
  2. 如果您不想允许NULL,请按以下方式更改INSERT ISNULL(cast(col as bigint), '')
  3. 查询:

      INSERT INTO #TEMP (id,tele,col) 
      SELECT id,tele,ISNULL(cast(col as bigint), '')
      FROM data 
    

答案 2 :(得分:0)

你能尝试这样吗?  INSERT INTO #TEMP(id,tele,col)             SELECT id,tele,CAST(ISNULL(col,'')作为BigInt)             来自数据