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?
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.
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.
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 .
Check out ISNULL() in the SQL Server Books Online.
Syntax:
ISNULL ( check_expression , replacement_value )
Example:
Select ISNULL(myfield1,'') from mytable1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With