sql中的Abs和校验和

时间:2013-10-25 09:16:43

标签: sql tsql

校验和在程序中返回 null 。而且当我尝试只执行查询时...我得到了

Msg 245, Level 16, State 1, Line 1
Conversion failed when converting the varchar value 'Audit C recorded' to data type tinyint.

..你可以帮我解决这个问题吗?

SELECT CAST(ABS(CHECKSUM(Indicator)) % 450 AS TINYINT)   AS Indicator,
       CAST(CIndicator AS VARCHAR(100))                  AS CIndicator,
       CAST(SK_IndicatorL2 AS TINYINT)                   AS SK_IndicatorL2,
       CAST(ABS(CHECKSUM(IndicatorL2)) % 450 AS TINYINT) AS IndicatorL2
FROM   ( VALUES ('Alcohol',
       'Alcohol',
       'Audit C recorded',
       'Audit C recorded  (excluding screen in 3y prior to start of quarter)'),
                ('Alcohol',
       'Alcohol',
       'Community Detox and TH CAT',
       'Community Detox and TH CAT'),
                ('Alcohol',
       'Alcohol',
       'Follow Up appointment',
       'Follow Up appointment'),
                ('Healthy Lifestyles',
       'Healthy Lifestyles',
       'HealthyLifestyle-Aged 19-39',
       'HealthyLifestyle-Aged 19-39'),
                ('Healthy Lifestyles',
       'Healthy Lifestyles',
       'Aged 19-39 - BMI recorded',
       'Aged 19-39 - BMI recorded') ) AS Nis (Indicator, 
                                              CIndicator, 
                                              SK_IndicatorL2, 
                                              IndicatorL2)

我试过这样做: SELECT CAST(ABS(CHECKSUM('审计C记录'))%250作为TinyInt) 我得到一个正确的整数值。

3 个答案:

答案 0 :(得分:0)

我认为她正在尝试获取此查询的ID和名称指标。我也得到数据类型错误.. CAST(ABS(CHECKSUM(指标))%450作为TinyInt)作为id号 如果我没错?

答案 1 :(得分:0)

我不确定你想要什么,但这很有效。

您需要从TINYINT更改为SMALLINT,因为您的值最多可达450.

编辑:如果您需要TINYINT,只需使用% 256代替% 450

SELECT CAST(ABS(CHECKSUM(Indicator)) % 256 AS TINYINT)      AS Indicator,
       CAST(CIndicator AS VARCHAR(100))                     AS CIndicator,
       CAST(ABS(CHECKSUM(SK_IndicatorL2)) % 256 AS TINYINT) AS SK_IndicatorL2,
       CAST(ABS(CHECKSUM(IndicatorL2)) % 256 AS TINYINT)    AS IndicatorL2
FROM   ( VALUES ('Alcohol',
       'Alcohol',
       'Audit C recorded',
       'Audit C recorded  (excluding screen in 3y prior to start of quarter)'),
                ('Alcohol',
       'Alcohol',
       'Community Detox and TH CAT',
       'Community Detox and TH CAT'),
                ('Alcohol',
       'Alcohol',
       'Follow Up appointment',
       'Follow Up appointment'),
                ('Healthy Lifestyles',
       'Healthy Lifestyles',
       'HealthyLifestyle-Aged 19-39',
       'HealthyLifestyle-Aged 19-39'),
                ('Healthy Lifestyles',
       'Healthy Lifestyles',
       'Aged 19-39 - BMI recorded',
       'Aged 19-39 - BMI recorded') ) AS Nis (Indicator, 
                                              CIndicator, 
                                              SK_IndicatorL2, 
                                              IndicatorL2)

它给出了这个:(使用% 256时编辑更新值

Indicator CIndicator                                                                                           SK_IndicatorL2                                                                                       IndicatorL2
--------- ---------------------------------------------------------------------------------------------------- ---------------------------------------------------------------------------------------------------- -----------
167       Alcohol                                                                                              Audit C recorded                                                                                     110
167       Alcohol                                                                                              Community Detox and TH CAT                                                                           17
167       Alcohol                                                                                              Follow Up appointment                                                                                83
187       Healthy Lifestyles                                                                                   HealthyLifestyle-Aged 19-39                                                                          143
187       Healthy Lifestyles                                                                                   Aged 19-39 - BMI recorded                                                                            32

更新:如果您真的想要一个唯一的字符串,那么您可以使用HASHBYTES,如下所示:

HASHBYTES('SHA', IndicatorL2) AS [HASHVAL]

但如果您需要TINYINT,那么您建议的解决方案可能会更好。

答案 2 :(得分:0)

SELECT CAST(ABS(CHECKSUM(Indicator)) % 220 AS TINYINT)   AS Indicator,
       CAST(CIndicator AS VARCHAR(100))                  AS CIndicator,
       CAST(ABS(CHECKSUM(SK_IndicatorL2)) % 220 AS TINYINT) AS SK_IndicatorL2,
       CAST(IndicatorL2 AS varchar(100))                   AS IndicatorL2

FROM   ( VALUES ('Alcohol',
       'Alcohol',
       'Audit C recorded',
       'Audit C recorded  (excluding screen in 3y prior to start of quarter)'),
                ('Alcohol',
       'Alcohol',
       'Community Detox and TH CAT',
       'Community Detox and TH CAT'),
                ('Alcohol',
       'Alcohol',
       'Follow Up appointment',
       'Follow Up appointment'),
                ('Healthy Lifestyles',
       'Healthy Lifestyles',
       'HealthyLifestyle-Aged 19-39',
       'HealthyLifestyle-Aged 19-39'),
                ('Healthy Lifestyles',
       'Healthy Lifestyles',
       'Aged 19-39 - BMI recorded',
       'Aged 19-39 - BMI recorded') ) AS Nis (Indicator, 
                                              CIndicator, 
                                              SK_IndicatorL2, 
                                              IndicatorL2)