2013-10-25 82 views
1

checksum,配置在程序返回。而且還當我嘗試只執行查詢..我得到ABS和在TSQL

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(校驗和(」審計C記錄'))%250作爲TinyInt) 我得到一個適當的整數值。

+0

我得到'當轉換varchar值'Audit C recording'轉換爲數據類型tinyint時轉換失敗。'ANSI_WARNINGS'關閉了嗎?你爲什麼試圖將明顯的非數字數據轉換爲'tinyint'呢? –

+0

我想獲得基於字符的唯一ID ... – user2919277

回答

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) 
0

我認爲她正在嘗試獲取此查詢的id和名稱度量。我也得到了數據類型的錯誤.. CAST(ABS(校驗和(指標))%450 TINYINT)的ID號 如果我沒看錯的

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 

UPDATE:如果你真的想要一個唯一的字符串,那麼你可以使用HASHBYTES,像這樣:

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

但是如果你需要一個TINYINT,那麼你的建議的解決方案可能是更好的。

+0

我們可以得到tinyint值嗎?我不介意使用250作爲範圍值來調製 – user2919277

+0

是的,只需使用'%256',它給出一個範圍爲0-255的數字,這將起作用。我改變了我的答案。 –

+0

雅得到了..謝謝你.. – user2919277