SQL:将数据类型varchar转换为numeric时出错。 - 我想添加百分号"%"在数据中

时间:2018-01-05 06:26:26

标签: sql sql-server sql-server-2012

将数据类型varchar转换为数字时出错。 - 我想添加百分号"%"在数据中

 SUM (Convert(Varchar, CASE 
        WHEN  e.complexity = 'Simple'  AND datediff(DAY, e.entry_date, a.datetime_stamp)
        = 0  THEN 1
        WHEN e.complexity = 'Simple'  AND datediff(DAY, e.entry_date, a.datetime_stamp)
        >= 29 THEN 2
        WHEN  e.complexity = 'Simple'  AND datediff(DAY, e.entry_date, a.datetime_stamp)
        >= 14 THEN 3
        WHEN  e.complexity = 'Simple'  AND datediff(DAY, e.entry_date, a.datetime_stamp)
        >= 8 THEN 4
        WHEN  e.complexity = 'Simple'  AND datediff(DAY, e.entry_date, a.datetime_stamp)
        <= 7  THEN 5


        ELSE 1

     END)*.05 + '%') as total

样品输出:2.5%

2 个答案:

答案 0 :(得分:0)

我认为你需要这样的东西

select  Convert(Varchar(50), SUM (CASE 
        WHEN  e.complexity = 'Simple'  AND datediff(DAY, e.entry_date, a.datetime_stamp)
        = 0  THEN 1
        WHEN e.complexity = 'Simple'  AND datediff(DAY, e.entry_date, a.datetime_stamp)
        >= 29 THEN 2
        WHEN  e.complexity = 'Simple'  AND datediff(DAY, e.entry_date, a.datetime_stamp)
        >= 14 THEN 3
        WHEN  e.complexity = 'Simple'  AND datediff(DAY, e.entry_date, a.datetime_stamp)
        >= 8 THEN 4
        WHEN  e.complexity = 'Simple'  AND datediff(DAY, e.entry_date, a.datetime_stamp)
        <= 7  THEN 5


        ELSE 1

     END) * 0.5) + '%' as total

答案 1 :(得分:0)

您需要使用如下(2012+)

CONCAT (

        CAST(

            SUM (  

                CASE 
                    WHEN  e.complexity = 'Simple'  AND datediff(DAY, e.entry_date, a.datetime_stamp)
                    = 0  THEN 1
                    WHEN e.complexity = 'Simple'  AND datediff(DAY, e.entry_date, a.datetime_stamp)
                    >= 29 THEN 2
                    WHEN  e.complexity = 'Simple'  AND datediff(DAY, e.entry_date, a.datetime_stamp)
                    >= 14 THEN 3
                    WHEN  e.complexity = 'Simple'  AND datediff(DAY, e.entry_date, a.datetime_stamp)
                    >= 8 THEN 4
                    WHEN  e.complexity = 'Simple'  AND datediff(DAY, e.entry_date, a.datetime_stamp)
                    <= 7  THEN 5
                    ELSE 1
                END *.05 

                ) AS VARCHAR(MAX)

            ) , '%') as total

或者 从SQL 2012下面使用此

 CAST(

            SUM (  

                CASE 
                    WHEN  e.complexity = 'Simple'  AND datediff(DAY, e.entry_date, a.datetime_stamp)
                    = 0  THEN 1
                    WHEN e.complexity = 'Simple'  AND datediff(DAY, e.entry_date, a.datetime_stamp)
                    >= 29 THEN 2
                    WHEN  e.complexity = 'Simple'  AND datediff(DAY, e.entry_date, a.datetime_stamp)
                    >= 14 THEN 3
                    WHEN  e.complexity = 'Simple'  AND datediff(DAY, e.entry_date, a.datetime_stamp)
                    >= 8 THEN 4
                    WHEN  e.complexity = 'Simple'  AND datediff(DAY, e.entry_date, a.datetime_stamp)
                    <= 7  THEN 5
                    ELSE 1
                END *.05 

                ) AS VARCHAR(MAX)

            ) + '%' total