SQL, how to convert the int to varchar for the null value

tanj1035
tanj1035 used Ask the Experts™
on
hi Expert,

For the column 'mtd', the content is number or null value. I want to convert null value to the varchar. Can you help me on it? Thank  you.


select d.Name,s.dealerid,
case when cast(dr.mtd as varchar) is null then 'NotActive' else mtd end as MTDDeal
from [MMD_Feed].[dbo].[SegmentID] s
inner join [MMD_Feed].[dbo].[Dealer] d on d.dealerid=s.dealerid
left join [MMD_Feed].[dbo].[dart] dr on s.dealerid=dr.dealerid
where Segment =@Segment
order by name asc
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Contract ERP Admin/Consultant
Commented:
The ISNULL function will also remove the need for that CASE statement:

select  d.Name,
        s.dealerid,
        ISNULL(CONVERT(varchar, dr.mtd), 'NotActive') AS MTDDEal,
from    [MMD_Feed].[dbo].[SegmentID] s
inner join [MMD_Feed].[dbo].[Dealer] d on d.dealerid=s.dealerid
left join  [MMD_Feed].[dbo].[dart] dr on s.dealerid=dr.dealerid
where   Segment =@Segment
order by name asc

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial