T-SQL将数字与填充数字子字符串进行比较

时间:2012-03-15 11:47:14

标签: sql tsql

问题是COLA填充了0s

我要做的是用COLA验证COLB,COLC,COLD,COLE,COLF

或者我以错误的方式解决了这个问题!

如何使用COLB,COLD将Number(int)与填充数字子字符串进行比较?


实施例 这是正确的

COLB    COLC    COLD    COLE    COLF               COLA

1        DD      5       1       0                     should = 01DD000510

不是这些中的任何一个

COLB   COLC   COLD  COLE   COLF    COLA

50     AA     230   2       0        0
50     AC     600   3       0
50     AG     740   2       0        50AE074020
50     AS     240   3       0        *0AS024030

 -- select that does not work statement.

 SELECT COLA ,
        COLB ,
        SUBSTRING(COLA, 1, 2) AS 'BLC_COLB' ,
        COLC ,
        SUBSTRING(COLA, 3, 2) AS 'BLC_COLC' ,
        COLD ,
        SUBSTRING(COLA, 5, 4) AS 'BLC_COLD' ,
        COLE ,
        SUBSTRING(COLA, 9, 1) AS 'BLC_COLE' ,
        COLF ,
        SUBSTRING(COLA, 10, 1) AS 'BLC_COLF'
 FROM   ATABLE
 WHERE  SUBSTRING(COLA, 1, 2) NOT LIKE COLB
        OR SUBSTRING(COLA, 3, 2) NOT LIKE COLC
        OR SUBSTRING(COLA, 5, 4) NOT LIKE COLD
        OR SUBSTRING(COLA, 9, 1) NOT LIKE COLE
        OR SUBSTRING(COLA, 10, 1) NOT LIKE COLF

--create table and data script

 CREATE TABLE [dbo].[ATABLE]
    (
      [ROWVERSION] [bigint] NULL ,
      [ROWDATE] [datetime] NULL ,
      [COLB] [int] NOT NULL ,
      [COLC] [nvarchar](2) NOT NULL ,
      [COLD] [int] NOT NULL ,
      [COLE] [nvarchar](1) NOT NULL ,
      [COLF] [int] NOT NULL ,
      [COLA] [nvarchar](14) NULL ,
      CONSTRAINT [CON1_0] PRIMARY KEY CLUSTERED
        ( [COLB] ASC, [COLC] ASC, [COLD] ASC, [COLE] ASC, [COLF] ASC )
        WITH ( PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,
               IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON,
               ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 70 ) ON [PRIMARY]
    )
 ON [PRIMARY]

    GO
 SET NUMERIC_ROUNDABORT OFF
GO
 SET ANSI_PADDING, ANSI_WARNINGS, CONCAT_NULL_YIELDS_NULL, ARITHABORT, QUOTED_IDENTIFIER, ANSI_NULLS, NOCOUNT ON
GO
 SET DATEFORMAT YMD
GO
 SET XACT_ABORT ON
GO
 SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO
 BEGIN TRANSACTION

-- Add 5 rows to [dbo].[ATABLE] with non-unique comparison key
 SET ROWCOUNT 1
 INSERT INTO [dbo].[ATABLE]
        ( [COLB] ,
          [COLC] ,
          [COLD] ,
          [COLE] ,
          [COLF] ,
          [ROWVERSION] ,
          [ROWDATE] ,
          [COLA]
        )
 VALUES ( 1 ,
          N'DD' ,
          5 ,
          N'1' ,
          0 ,
          327520493236000002 ,
          '2011-07-04 10:21:33.227' ,
          N'01DD000510'
        )
 INSERT INTO [dbo].[ATABLE]
        ( [COLB] ,
          [COLC] ,
          [COLD] ,
          [COLE] ,
          [COLF] ,
          [ROWVERSION] ,
          [ROWDATE] ,
          [COLA]
        )
 VALUES ( 50 ,
          N'AA' ,
          230 ,
          N'2' ,
          0 ,
          327520514140000004 ,
          '2011-07-28 09:47:41.013' ,
          N'0'
        )
 INSERT INTO [dbo].[ATABLE]
        ( [COLB] ,
          [COLC] ,
          [COLD] ,
          [COLE] ,
          [COLF] ,
          [ROWVERSION] ,
          [ROWDATE] ,
          [COLA]
        )
 VALUES ( 50 ,
          N'AC' ,
          600 ,
          N'3' ,
          0 ,
          327520523360000007 ,
          '2011-07-28 09:48:09.577' ,
          N''
        )
 INSERT INTO [dbo].[ATABLE]
        ( [COLB] ,
          [COLC] ,
          [COLD] ,
          [COLE] ,
          [COLF] ,
          [ROWVERSION] ,
          [ROWDATE] ,
          [COLA]
        )
 VALUES ( 50 ,
          N'AG' ,
          740 ,
          N'2' ,
          0 ,
          327520543671000002 ,
          '2011-07-28 09:47:03.773' ,
          N'50AE074020'
        )
 INSERT INTO [dbo].[ATABLE]
        ( [COLB] ,
          [COLC] ,
          [COLD] ,
          [COLE] ,
          [COLF] ,
          [ROWVERSION] ,
          [ROWDATE] ,
          [COLA]
        )
 VALUES ( 50 ,
          N'AS' ,
          240 ,
          N'3' ,
          0 ,
          327520586618000007 ,
          '2011-07-28 09:48:08.453' ,
          N'*0AS024030'
        )
 SET ROWCOUNT 0
 COMMIT TRANSACTION

2 个答案:

答案 0 :(得分:2)

执行此操作以选择"未经验证的所有行":

SELECT  *
FROM    ATABLE
WHERE   RIGHT('0' + CAST(COLB AS VARCHAR), 2) +
        COLC +
        RIGHT('000' + CAST(COLD AS VARCHAR), 4) +
        COLE +
        CAST(COLF AS VARCHAR) <> COLA

答案 1 :(得分:0)

在进行比较之前,将零填充数转换为整数。

CASE
   WHEN ZeroPadded NOT LIKE '%[^0-9]%'
   THEN CAST(ZeroPadded AS int)
END