简体   繁体   English

SQL Server-如何在ISNULL中使用转换

[英]SQL Server - How to use convert with ISNULL

Something like the example below: 类似于下面的示例:

SELECT SSN, Name, CONVERT(VARCHAR(50), Hours) ISNULL(Hours, '')

where I want to convert an int to varchar and at the same time set the null values as an empty string. 我想将int转换为varchar,同时将null值设置为空字符串。 How can I do to make this possible? 我该怎么做才能做到这一点?

ISNULL(CONVERT(VARCHAR(50), Hours), '')

An alternative option if you are on 2012+ would be to use 如果您使用的是2012年以上的产品,则可以使用

SELECT CONCAT(Hours,'')

which has the same end result of converting to string and returning empty string instead of NULL. 转换为字符串并返回空字符串而不是NULL的最终结果相同。

Wrap your convert in the isnull ; 把你的isnull ;

SELECT 
SSN
,Name
,ISNULL(CONVERT(VARCHAR(50), Hours), '')

This way it will allow for the nulls to be pulled on the conversion, you won't be able to do the isnull inside the convert as a zero length character won't be compatible with an int field (or other number only field). 这样,它将允许在转换中提取空值,您将无法在转换内执行isull,因为零长度字符将与int字段(或其他仅数字字段)不兼容。

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

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