question

BobbyP-1695 avatar image
0 Votes"
BobbyP-1695 asked LiHongMSFT-3908 commented

Convert a NUMERIC Data Column to VARCHAR when it is Null

So I have a NUMERIC field and if it is NULL I want to actually pass back the value '<NULL>'. This is for a user with a before and after image of data columns.

I tried ISNULL with CONVERT but I cannot seem to get the syntax down.

Can anyone please help me out with the syntax?

Thanks for your review and am hopeful for a reply.

sql-server-transact-sql
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.


Did you try isnull(cast(field as varchar(max)), '<NULL>') too?


0 Votes 0 ·

varchar(p+2), where p is the precision of the column should be sufficient. Using MAX when it is not needed, since MAX data comes with a bit of overhead, even if the actual length is short..

By the way, Viorel, why do you post that as a comment? That should be ans Answer.

0 Votes 0 ·

Hi @BobbyP-1695
Is there any update about this issue?
Please don't forget to accept the answers if they helped. Your action would be helpful to other users who encounter the same issue and read this thread.
Thanks for your understanding!

Best regards,
LiHong

0 Votes 0 ·
LiHongMSFT-3908 avatar image
0 Votes"
LiHongMSFT-3908 answered

Hi @BobbyP-1695

I tried ISNULL with CONVERT but I cannot seem to get the syntax down.

When converting from Numeric to Varchar without specify the length,an error will return when the result length is too long to display.
Did you try ISNULL(CONVERT(VARCHAR(50),NUMERIC_Field),'<NULL>') ? If it doesn't work, try replacing '50' with 'max'.

Best regards,
LiHong


If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

TomPhillips-1744 avatar image
0 Votes"
TomPhillips-1744 answered

Just to be clear.

A SELECT statement cannot return 2 different data types for the same column. Every record must conform to the same data type for the column. It is either NUMERIC or VARCHAR, it cannot be sometimes VARCHAR.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.