Domanda

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
È stato utile?

Soluzione

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

Altri suggerimenti

Try this

CASE WHEN BI.dbo.SKU.phl5 IS NULL THEN 'Uncategorized' 
     ELSE SUBSTRING(BI.dbo.SKU.phl5,0,3) 
END AS phl1   
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top