2013-07-24 121 views
1

但是,我建立了一個查詢(見下文),而不是返回「大於20或30」,我希望我的查詢實際顯示計數(例如31,35,50等)。我似乎無法弄清楚如何去做。謝謝您的幫助。字符長度SQL Server 2005

select 
cc_units_of_measure.description as UOM_Description, 
case when LEN(cc_units_of_measure.description) < 20 then 'OK' 
else 'Greater than 20' 
end as Results_UOM_description, 
cc_units_of_measure.company_abbreviation as UOM_Company_Abbreviation, 
case when LEN(cc_units_of_measure.company_abbreviation) < 20 then 'OK' 
else 'Greater than 20' 
end as Results_UOM_company_abbreviation, 
cc_frequencies.description as Freq_Description, 
case when LEN(cc_frequencies.description) < 30 then 'OK' 
else 'Greater than 30' 
end as Results_Frequency_description 
from cc_units_of_measure, cc_frequencies 
+0

通過計數你的意思是字符的長度? – Vasanth

+0

嘗試從'select Sum(Len(cc_units_of_measure.description)<20 then 1 else 0 end)的情況下工作爲[UOM_LT20],...'。 – HABO

+0

它是長度還是數字?另外,你的「else」應該是「等於或大於xx」 – Elmer

回答

1

如果你改變了case語句的其他部分返回長,你會得到你正在尋找像這樣的值:

SELECT cc_units_of_measure.description AS UOM_Description , 
     CASE WHEN LEN(cc_units_of_measure.description) < 20 THEN 'OK' 
      ELSE LEN(cc_units_of_measure.description) 
     END AS Results_UOM_description , 
     cc_units_of_measure.company_abbreviation AS UOM_Company_Abbreviation , 
     CASE WHEN LEN(cc_units_of_measure.company_abbreviation) < 20 THEN 'OK' 
      ELSE LEN(cc_units_of_measure.company_abbreviation) 
     END AS Results_UOM_company_abbreviation , 
     cc_frequencies.description AS Freq_Description , 
     CASE WHEN LEN(cc_frequencies.description) < 30 THEN 'OK' 
      ELSE LEN(cc_frequencies.description) 
     END AS Results_Frequency_description 
FROM cc_units_of_measure , 
     cc_frequencies 
+0

我試過了,但最終出現了一個錯誤:Msg 245,Level 16,State 1,Line 1 將varchar值'OK'轉換爲數據類型int時轉換失敗。 – user2612714