TRY_CONVERT 在 SQL Server 2012 上失败

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22310486/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 01:19:01  来源:igfitidea点击:

TRY_CONVERT fails on SQL Server 2012

sqlsql-serversql-server-2012

提问by jpw

The following SQL statement fails on SQL Server 2012(SP1) 11.0.3401.0 Version

以下 SQL 语句在 SQL Server 2012(SP1) 11.0.3401.0 版本上失败

DECLARE @b VARCHAR(10) = '12312.2'
SELECT TRY_CONVERT(DECIMAL(10,2),@b)

error message

错误信息

Msg 195, Level 15, State 10, Line 2
'DECIMAL' is not a recognized built-in function name.

But works fine on SQL Server 2012(SP1) 11.0.3393.0

但在 SQL Server 2012(SP1) 11.0.3393.0 上运行良好

Both servers have no problem with TRY_PARSE()

两台服务器都没有问题 TRY_PARSE()

DECLARE @b VARCHAR(10) = '12312.2'
SELECT TRY_PARSE(@b AS DECIMAL(10,2))

Update:

更新:

after further testing and trying conversion to different types got another different error message

在进一步测试并尝试转换为不同类型后,得到了另一个不同的错误消息

DECLARE @b VARCHAR(10) = '12312'
SELECT TRY_CONVERT(INT,@b)

Error:

错误:

error: Msg 195, Level 15, State 10, Line 2
'TRY_CONVERT' is not a recognized built-in function name.

So original error message was miss leading, but now I'm even more confused why it is not there.

所以最初的错误消息是错过引导,但现在我更加困惑为什么它不存在。

回答by jpw

Check that the database compatibility level for the particular database you are using the function in is set to SQL Server 2012 (110)under properties/options/compatibility levelfor the database.

检查您正在使用该函数的特定数据库的数据库兼容性级别是否设置为SQL Server 2012 (110)低于properties/options/compatibility level该数据库。

回答by Zapnologica

Make sure that you have specified on which database you want to run the query on.

确保您已指定要在其上运行查询的数据库。

use [Database_Name]
go    
>>>>query here <<<
go

In my case I had a number of different databases on the server with different compatibility levels.

就我而言,我在服务器上有许多具有不同兼容性级别的不同数据库。