这是一个SQL Server 2012后引进的新函数。作用就如同它名字的意思。它对NULL值得处理是空字符串。当然它能做的不仅是对字符的支持。它支持N个列输入,列的类型支持更加完善。不过其实它的原理不过是把传入的数据用CONVERT函数转换成NVARCHAR类型。不信你传个XML或者SQL_VARIANT类型的数据进去,你会看到马上报错。错误提示是:
Msg 257, Level 16, State 3, Line 35 Implicit conversion from data type xml to nvarchar is not allowed. Use the CONVERT function to run this query.
至少我测试结果显示它对INT, FLOAT, DATETIME, NVARCHAR都是支持的。
测试脚本
WITH T AS ( SELECT TOP 10 CAST(Num AS INT) AS int_fld, CAST(Num AS NVARCHAR) AS str_fld, CAST(Num AS FLOAT) AS flt_fld, CAST(NULL AS NVARCHAR) AS NULL_FLD, CAST(Num AS datetime) AS dt_fld --CAST(Num AS sql_variant) AS var_fld, --CAST('<a>aa</a>'AS xml) AS xml_fld FROM dbo.Numbers ) SELECT CONCAT(int_fld,str_fld,flt_fld,NULL_FLD, dt_fld) FROM T