Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.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 Server:更好的列解决方案应该具有固定长度的数字数据_Sql_Sql Server 2008_Sql Server 2008 R2 - Fatal编程技术网

SQL Server:更好的列解决方案应该具有固定长度的数字数据

SQL Server:更好的列解决方案应该具有固定长度的数字数据,sql,sql-server-2008,sql-server-2008-r2,Sql,Sql Server 2008,Sql Server 2008 R2,我试图提取在特定列中只有数字的行,但数字应该只有6位或12位。 我正在努力: select * from table where column like '[0-9][0-9][0-9][0-9][0-9][0-9]' 它管用,但我觉得有点不干净。 请看是否有人能提供更好的解决方案 非常感谢 这个怎么样 DECLARE @Table TABLE ( number bigint ) INSERT INTO @Table ( number ) VALUES (

我试图提取在特定列中只有数字的行,但数字应该只有6位或12位。 我正在努力:

select * from table where column like '[0-9][0-9][0-9][0-9][0-9][0-9]'
它管用,但我觉得有点不干净。 请看是否有人能提供更好的解决方案

非常感谢

这个怎么样

    DECLARE @Table TABLE
(
    number bigint

)

INSERT INTO @Table
        ( number )
VALUES  ( 3456157 ), (251236), (785412), (251236475821), (36546)

--Select All rows
SELECT *
FROM @Table

--Just the ones we want
SELECT *
FROM @Table
WHERE
    LEN(number) IN (6, 12)
AND
    number LIKE '%[0-9]%'
where len(column) in (6, 12) and
      column not like '%[^0-9]%'
这个怎么样

    DECLARE @Table TABLE
(
    number bigint

)

INSERT INTO @Table
        ( number )
VALUES  ( 3456157 ), (251236), (785412), (251236475821), (36546)

--Select All rows
SELECT *
FROM @Table

--Just the ones we want
SELECT *
FROM @Table
WHERE
    LEN(number) IN (6, 12)
AND
    number LIKE '%[0-9]%'
where len(column) in (6, 12) and
      column not like '%[^0-9]%'
第一个条件检查长度是否为6或12。第二个检查所有字符是否都是数字

我应该指出,这一点几乎同样清楚:

where column like '[0-9][0-9][0-9][0-9][0-9][0-9]' or
      column like '[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'