Convert a NUMERIC Data Column to VARCHAR when it is Null

Bobby P 221 Reputation points
2022-05-04T21:05:18.5+00:00

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.

Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,552 questions
{count} votes

2 answers

Sort by: Most helpful
  1. LiHong-MSFT 10,046 Reputation points
    2022-05-05T01:57:57.897+00:00

    Hi @Bobby P

    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.

    0 comments No comments

  2. Tom Phillips 17,716 Reputation points
    2022-05-05T16:27:50.207+00:00

    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.

    0 comments No comments