Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing a null string to an empty string in select statment

Tags:

I have a SQL Server 2005 table that has a string column in which empty values are sometimes stored as NULL and other times as an empty string.

I am doing a SELECT DISTINCT on this column and I am getting all the distinct values + NULL + empty string. But what I would like is to check if the value is NULL and return an empty string instead. So the result set would be all the distinct values + empty string (if any values were null or an empty string).

But how can I do this in a SELECT statement?

like image 399
AJ. Avatar asked Jan 27 '10 09:01

AJ.


People also ask

How do you replace a NULL value in a string in SQL?

We can replace NULL values with a specific value using the SQL Server ISNULL Function. The syntax for the SQL ISNULL function is as follow. The SQL Server ISNULL function returns the replacement value if the first parameter expression evaluates to NULL.

How do you create an empty string in SQL?

You have to use a clause in SQL IS Null. On the other hand, an empty string is an actual value that can be compared to in a database. You simply use two ticks together.

Is NULL equal to empty string?

An empty string is a string instance of zero length, whereas a null string has no value at all. An empty string is represented as "" . It is a character sequence of zero characters. A null string is represented by null .


1 Answers

Check out ISNULL() in the SQL Server Books Online.

Syntax:

ISNULL ( check_expression , replacement_value ) 

Example:

Select ISNULL(myfield1,'') from mytable1 
like image 161
Frank Kalis Avatar answered Nov 15 '22 14:11

Frank Kalis