简体   繁体   English

T-SQL:: 将 1 更改为 0.5 时将数据类型 varchar 转换为数字时出错

[英]T-SQL :: Error converting data type varchar to numeric when changing 1 to 0.5

I'm writing a complex T-SQL query with CASE that will help me calculate how much time it will take me to migrate databases based on the size of each database:我正在使用CASE编写一个复杂的 T-SQL 查询,这将帮助我根据每个数据库的大小计算迁移数据库需要多长时间:

SELECT 
    ProductVersion
    ,CompatibilityLevel
    ,convert(numeric(10,2), substring([Size], 1, (len([Size]) - 3))) as [Size in MB]
    ,CASE
        when ProductVersion < '11%' THEN 1
        when ProductVersion = NULL then 1
        ELSE ''    
    END  + 
    CASE
        when CompatibilityLevel < '110' then 1
        when CompatibilityLevel = NULL then 1
        ELSE ''
    END  + 
    CASE
        when  convert(numeric(10,2), substring([Size], 1, (len([Size]) - 3))) < 100.00 THEN 1 -- Here is the problem
        ELSE ''
    END AS [Hours of work]
FROM MyDatabaseList

All good, it works when I set 1 hour of work for every database which has less then 100.00 MB.很好,当我为每个小于 100.00 MB 的数据库设置 1 小时的工作时,它就可以工作。

在此处输入图像描述

Also all other values are summed and in the Hours of work column you can see numbers like 0 , 1 , 3 , 2 ... This means SQL Server is calculating values.此外,所有其他值都被求和,在“工作时间”列中,您可以看到诸如0132之类的数字……这意味着 SQL 服务器正在计算值。 All good.都好。

But well, telling to my manager that I have to work 1 hour for a database that only has 100.00 MB of size is ridiculous.但是,告诉我的经理我必须为一个只有 100.00 MB 大小的数据库工作 1 小时是荒谬的。

Let's change the line:让我们换行:

    when  convert(numeric(10,2), substring([Size], 1, (len([Size]) - 3))) < 100.00 THEN 1

and let's put 0.5 instead of 1 :让我们把0.5而不是1

    when  convert(numeric(10,2), substring([Size], 1, (len([Size]) - 3))) < 100.00 THEN 0.5

...and bang - I get this error: ...然后砰 - 我收到这个错误:

在此处输入图像描述

Msg 8114, Level 16, State 5, Line 1消息 8114,第 16 层,State 5,第 1 行
Error converting data type varchar to numeric.将数据类型 varchar 转换为数字时出错。

Questions:问题:

  1. Why this error now?为什么现在出现这个错误?
  2. Why it was successfully calculating before and now needs a datatype somewhere?为什么它之前计算成功,现在需要某个数据类型?
  3. What change do I have to add to my query?我必须在查询中添加哪些更改?
  4. Why it says on Line 1 ?为什么它在Line 1上显示? The error must be on Line 16 , right?错误必须在Line 16 ,对吗?

The error is telling you the conversion is happening in the statement that starts in line 1.该错误告诉您转换发生在从第 1 行开始的语句中。

As for why, it's because '' cannot be converted to a numeric but it can be converted to an int .至于为什么,这是因为''不能转换为numeric ,但可以转换为int

SELECT CONVERT(decimal(2,1),'')
--Error converting data type varchar to numeric.
GO
SELECT CONVERT(int,'')
--
GO

You were previously converting it to an int (the literal 1 is an int ).您之前将其转换为int (文字1int )。 Get out of the habit of declaring '' for a number;改掉用''表示数字的习惯; it is by definition not a number, it is a zero length string.根据定义,它不是数字,它是零长度字符串。 If you want zero, then use 0 .如果您想要零,请使用0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM