1

校验和在程序中返回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('Audit Crecorded')) % 250 as TinyInt) 我得到一个正确的整数值。

4

3 回答 3

0

我认为她正在尝试获取此查询的 id 和 name 指标。我也收到数据类型错误。如果我没记错,CAST(ABS(CHECKSUM(Indicator)) % 450 as TinyInt) 作为 id 号

于 2013-10-25T09:24:54.030 回答
0

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

您需要从更改为TINYINTSMALLINT因为您的值可能高达 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]

但是,如果您需要 a TINYINT,那么您提出的解决方案可能会更好。

于 2013-10-25T09:27:32.980 回答
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)
于 2013-10-25T09:34:39.797 回答