My field in my SKU table

(BI.dbo.SKU.phl5) is varchar(15)

However below code returns just 3 characters 'Unc' for the null fields in my table while it should return 'Uncategorized'. How to solve that?

ISNULL(SUBSTRING(BI.dbo.SKU.phl5,0,3),'Uncategorized') AS phl1
有帮助吗?

解决方案

ISNULL(CAST(SUBSTRING(BI.dbo.SKU.phl5,0,3) AS VARCHAR(13)),'Uncategorized') AS phl1

The size of the return type of SUBSTRING isn't clearly documented that I can find, but the problem is that the type of ISNULL is the type of the first expression, which is clearly coming back as VARCHAR(3) since you are truncating it to 3 characters.

ISNULL docs

其他提示

Try this

CASE WHEN BI.dbo.SKU.phl5 IS NULL THEN 'Uncategorized' 
     ELSE SUBSTRING(BI.dbo.SKU.phl5,0,3) 
END AS phl1   
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top