How to Substitute a String if record is NULL in T-SQL
Solution 1
You can use COALESCE
or ISNULL
. The former is standard and returns the first NOT NULL
argument (or NULL
if all arguments are NULL
)
SELECT COALESCE(micv.value,'Pending') as value
ISNULL
is restricted to only 2 arguments but is more efficient in SQL Server if the first value to be tested is expensive to evaluate (e.g. a subquery).
One potential "gotcha" with ISNULL
to be aware of is that it returns the datatype of the first parameter so if the string to be substituted is longer than the column datatype would allow you will need a cast.
E.g.
CREATE TABLE T(C VARCHAR(3) NULL);
INSERT T VALUES (NULL);
SELECT ISNULL(C,'Unknown')
FROM T
Would return Unk
But ISNULL(CAST(C as VARCHAR(7)),'Unknown')
or COALESCE
would both work as desired.
Solution 2
you can also use ISNULL('value', 'replacewithvalue')
Solution 3
SELECT
sr.sales_region_name AS SalesRegion
, ISNULL(micv.value,'Pending')
, COUNT(sr.sales_region_name)
FROM prospect p
--(...)
Go check ISNULL for further info.
Related videos on Youtube
dvanaria
I'm currently working for a telecommunications company in Colorado, doing some software development and systems integration work. I've been interested in programming from an early age, from about when I was 13 or so, programming on Apple II systems at school and eventually at home when my father bought me an Apple IIc. I loved picking up programming books from the public library and just picking out whatever interested me and trying it out first hand. I went on to learn C programming in college, then OpenGL graphics programming, Object Oriented Programming with Java, just about anything new to me I found interesting. Today I still work on my own programming projects (now usually in Python and C++), but I've always had great memories of how much fun it was to discover programming when I was a kid. I'm more interested these days in getting other people interested in computers and programming, maybe trying to inspire other people (especially kids) to get into it. I'm always going back to the basics and really trying to break concepts down so they are accessible and so I understand them better myself. My hope is to one day either write a programming book for kids, that recaptures some of that wonder and excitement, or develop a series of YouTube tutorials that may help newbies pick up programming in a way that is more accessible and easily understood. I think today's biggest barrier to entry in this field of interest is the complexity of today’s systems. It's not like the old days where you turned your computer on and it booted in a few seconds into a BASIC command prompt. Those systems were a lot of fun because you had to pick up programming right from the start in order to really do anything with them. Either way, this site (Stack Overflow) has been a tremendous help to me and a lot of fun to contribute to. Ideally, I would love to feel some kind of expertise with programming in general, and this site is a good step toward getting that kind of experience – the best way to learn anything, I’m convinced, is to teach others, to ask a lot of questions, and help out other people by answering their questions.
Updated on April 12, 2020Comments
-
dvanaria about 4 years
I'm writing a T-SQL report that shows the number of accounts that are in different statuses for different customers. The report results in something like:
Customer1 NoService 7 Customer1 IncompleteOrder 13 Customer1 NULL 9 Customer2 NoService 12 Customer2 Available 19 Customer2 NULL 3 ...
The 'NULL' status is valid data, but instead of displaying NULL, I want to display "Pending". Here is my SQL so far:
USE cdwCSP; SELECT sr.sales_region_name AS SalesRegion , micv.value , COUNT(sr.sales_region_name) FROM prospect p LEFT JOIN sales_region sr ON p.salesRegionId = sr.sales_region_number LEFT JOIN prospectOrder po ON po.prospectId = p.prospectId LEFT JOIN wo ON wo.prospectId = p.prospectId LEFT JOIN woTray wot ON wot.woId = wo.woId LEFT JOIN miscInformationCustomerCategory micc ON micc.prospectId = p.prospectId LEFT JOIN miscInformationCustomerValues micv ON micv.miscInformationCustomerCategoryId = micc.miscInformationCustomerCategoryId LEFT JOIN miscInformationCategory mic ON micc.miscInformationCategoryId = mic.miscInformationCategoryId WHERE wot.dateOut IS NULL AND mic.categoryName LIKE '%Serviceability%' GROUP BY sr.sales_region_name, micv.value ORDER BY sr.sales_region_name, micv.value;
Any help would be appreciated, I'm still learning T-SQL so this might be an easy question to answer.
-
dvanaria about 13 yearsPerfect, this works well and seems efficient. Thanks for your help.