Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In SQL Server, how to have select return empty string and not null when null

Tags:

sql-server

I have a nullable varchar and even when it is null, I want my select to return an empty string and not null. That is

SELECT FIRST, LAST 
FROM CUSTOMER

What I want is if FIRST is null, I want the return to be ""

like image 429
Peter Kellner Avatar asked Oct 15 '25 17:10

Peter Kellner


2 Answers

You can also use

SELECT COALESCE(FIRST, ''), LAST FROM CUSTOMER

This has the advantage of being more portable (COALESCE is part of the SQL Standard, ISNULL isn't)

You can also use an arbitrary number of arguments, like

SELECT COALESCE(FIRST, SECOND, THIRD, '')...

And COALESCE will use the first non-null value encountered

like image 148
AlexCuse Avatar answered Oct 18 '25 08:10

AlexCuse


SELECT isnull(FIRST,'') AS FIRST, LAST FROM CUSTOMER
like image 41
Blorgbeard Avatar answered Oct 18 '25 06:10

Blorgbeard



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!