开发者

Problem in converting the varchar value '1.33' to data type int

开发者 https://www.devze.com 2023-02-01 21:42 出处:网络
I have 5 case statements (just showing 2 case statements only). SELECT DISTINCT CASE [GRADE1] when 1 then 1

I have 5 case statements (just showing 2 case statements only).

SELECT DISTINCT 
       CASE [GRADE1]
         when 1 then 1
         when 2 then 3
         when 3 then 5
         when 4 then 7
         when 5 then 9
       End as Q1,
       CASE [GRADE2]
         when 1 then 1
       开发者_C百科  when 2.3 then 3
         when 3.33 then 5
         when 4.67 then 7
         when 1 then 9
       End as Q2
  FROM abcd

Here I am trying to put the values in temp table which I need to map in later select syntax

SELECT a,b     
  INTO #Temp_Q
  FROM xyz 
 WHERE [ds]= 'Data'

   select distinct t.[b] as [Q]
     from abcd s
left join #Temp_Q t on s.[GRADE1]  = t.[a]
                       s.[GRADE2]  = T.[a]

Instead of hardcoding all these values i am asked to write code.

Now i am getting error.. And don't know how to convert. [GRADE2] (FLOAT,NULL) is the datatype assinged in the column.

Msg 245, Level 16, State 1, Line 14 Conversion failed when converting the varchar value '1.33' to data type int.


1.33 isn't an integer, it's a DECIMAL. There is a decimal and numbers after the decimal, so it can't be represented as an integer i.e. 1, 2, 3, 4. You need to use either CAST or CONVERT before processing that field.


Try a SELECT CAST(a AS INT) or SELECT CONVERT(INT, a) and let me know how that works for you.


@JNK is correct. However, I'd change the data type in the temp table to decimal. The rule of thumb is to keep the data type consistent throughout all stages of processing. Since "1.01" is decimal, keep it decimal in storage too.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号