Question

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
Was it helpful?

Solution

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

OTHER TIPS

Try this

CASE WHEN BI.dbo.SKU.phl5 IS NULL THEN 'Uncategorized' 
     ELSE SUBSTRING(BI.dbo.SKU.phl5,0,3) 
END AS phl1   
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top