replace an empty value with null using sql - sql

i have some empty values in my table named partner(city,...) and when i want to change them to null values using replace(city,'',null) it returns all the values of city as null not only those who was empty

You could use the CASE statement.
SELECT
CITY = CASE WHEN city ='' then NULL else city end
FROM TABLE

Or, just another way to get there, SQL Server offers a "shorthand" version of the CASE expression for situations like this; the NULLIF() function.
SELECT
NULLIF(city,'') AS CITY
FROM TABLE
If the first argument for NULLIF matches the second argument, the function returns NULL. So here, if your city column is an empty string, the function will return NULL for you.
Note that NULLIF is also available in SSIS expressions, if you prefer to do your data transformations there.

Use coalesce():
select coalesce(city, '') as city

Related

Can I know the issue of this SQL query

I have this SQL query:
but I'm getting an error:
If I remove the comma-separated value from the variable, it is working fine. As well as if I remove the NULL checking feature it is working fine. Can I know the issue of this
It's because a CASE WHEN can only return 1 value.
And a STRING_SPLIT returns a resultset.
I assume something like this is what you want.
SELECT *
FROM Facility f
WHERE (#Facility IS NULL OR f.facilityCode IN (SELECT value FROM string_split(#Facility,',')))
This will get all records if the variable is null.
The function dbo.split will split the string in more than one value. This will confuse your subquery and the error you are receiving will be thrown.
In case you need what goes before the comma consider using:
select top 1 value
from dbo.split(#Facility, ','))
You want to say if the variable is NULL so ignore the WHERE statement, if so your query would be:
SELECT *
FROM Facility f
WHERE #Facility IN (select value from dbo.split(#Facility, ',')) OR #Facility IS NULL

How to check a column values is null or not in select statement

I am new to SQL Server, and I want to validate whether a column value is NULL or empty in the select statement .
The select statement is :
SELECT COM_TYPE, COM_IN_CHARGE_POSITION
FROM TABLE_01
If the COM_TYPE value is null or empty, I want to take the COM_TYPE_OTHERS value as COM_TYPE for the same way to COM_IN_CHARGE_POSITION.
It's simply if the COM_TYPE and COM_IN_CHARGE_POSITION is null or empty I want to take the values from other two columns.
Can anyone help me to solve this?
Let me assume that blank is NULL. In that case, you simply want coalesce():
select coalesce(COM_TYPE, COM_TYPE_OTHERS, COM_IN_CHARGE_POSITION, COM_IN_CHARGE_POSITION_OTHERS) as effective_COM_TYPE
The coalesce() function takes the first non-NULL argument. If the values could be empty strings or strings with spaces in addition to NULL, then a case expression is recommended.
COALESCE function can check which one column is first non-NULL, but you, if you want to check, is null or empty you can try to use CASE WHEN
SELECT CASE WHEN COM_TYPE IS NULL OR COM_TYPE = '' THEN COM_TYPE_OTHERS
ELSE COM_TYPE END AS COM_TYPE,
CASE WHEN COM_IN_CHARGE_POSITION IS NULL OR COM_IN_CHARGE_POSITION = '' THEN COM_IN_CHARGE_POSITION_OTHERS
ELSE COM_IN_CHARGE_POSITION END AS COM_IN_CHARGE_POSITION,
FROM TABLE_01
Simplest way to check for null or empty is to use ISNULL and NULLIF :
SELECT ISNULL(NULLIF(COM_TYPE, ''), COM_TYPE_OTHERS)
FROM TABLE_01
You can also use COALESCE but it does not check for empty string (only null), so you will still have to use NULLIF.
Also note that ISNULL is faster than COALESCE even if the difference is pretty negligible.
You can use COALESCE() or ISNULL() OR CASE WHEN statement
SELECT COALESCE( COM_TYPE, COM_TYPE_OTHERS ) AS COM_TYPE,
COALESCE (COM_IN_CHARGE_POSITION , COM_IN_CHARGE_POSITION_OTHERS) AS COM_IN_CHARGE_POSITION
FROM TABLE_01

Returning varchars that are not null or empty SQL

I have a column in SQL that is varchar. I need it to return anything with a value.
Example...
select * from students where StudentID <> ''
Is this the correct way of doing it? I've tried is not null but then it returns anything that is empty as well.
Thanks
I would suggest using coalesce:
select * from students where coalesce(StudentID, '') <> ''
This will turn nulls into empty strings and disallow them. This has the added bonus of restricting empty strings as well.
A null is not equal to anything, not even another null, so a simple <> doesnt work.
select * from students where StudentID <> '' AND StudentID IS NOT NULL
You can target both white space and null.
There's something call NOT NULL
SELECT LastName,FirstName,Address FROM Persons WHERE Address IS NOT NULL
Is that helping ?
try this:
select * from students where StudentID is not null
You have to handle the case that it's not null separately because you cannot compare with null-values. They are neither equal nor unequal to any other value(incl. null). NULL means "unknown value", so any comparison with any actual value makes no sense
....
WHERE StudentID IS NOT NULL AND StudentID <> ''
You could use ISNULL(StudentID,'') <> '' (or COALESCE). But i think this is more efficient.
Try using ISNULL()
select * from student where isnull(studentid,'') <> ''

SQL Query with if statement

I have a table which has a `department` column (allows null) but when I select that table and the field is null I don't want it to show Null but "-".
I'm told to put the if statement inside the select statement but I can't figure it out. How can I do this?
You want to use the function coalesce():
select coalesce(department, '-')
from table t
This is an ANSI standard function available in most databases.
You can use two methods:
1. Using CASE:
SELECT CASE WHEN department IS NULL
THEN '-'
ELSE department
END AS department FROM TableName
CASE evaluates a list of conditions and returns one of multiple possible result expressions. Read more here.
2. Using COALESCE:
SELECT COALESCE (department,'-') FROM TableName
COALESCE returns first parameter which is not null. Read more here.
You need to use the 'CASE WHEN' statement in your select query. Like this:
SELECT CASE WHEN Department IS NULL THEN Department = '-'
END AS DEPARTMENT
FROM Table_Name
You can use following code:
select ISNULL(department, '-') AS DEPARTM
from dbo.tbl_Department

Replacing NULL and empty string within Select statement

I have a column that can have either NULL or empty space (i.e. '') values. I would like to replace both of those values with a valid value like 'UNKNOWN'. The various solutions I have found suggest modifying the value within the table itself. However, this is not an option in this case in that the database is for a 3rd party application that is developed and/or patched very poorly (in reality I think my Rottweiler could have done a better job). I am concerned that modifying the underlying data could cause the application to melt into a smoking hole.
I have attempted variations of the following commands:
COALESCE(Address.COUNTRY, 'United States') -- Won't replace empty string as it is not NULL
REPLACE(Address.COUNTRY, '', 'United States') -- Doesn't replace empty string
ISNULL(Address.COUNTRY, 'United States') -- Works for NULL but not empty string
I know I could use the CASE statement but am hoping there is a much more elegant/efficient solution.
You'll have to trust me when I say I have looked for a solution to my specific issue and have not found an answer. If I have overlooked something, though, kindly show me the lighted path.
Try this
COALESCE(NULLIF(Address.COUNTRY,''), 'United States')
Sounds like you want a view instead of altering actual table data.
Coalesce(NullIf(rtrim(Address.Country),''),'United States')
This will force your column to be null if it is actually an empty string (or blank string) and then the coalesce will have a null to work with.
An alternative way can be this: - recommended as using just one expression -
case when address.country <> '' then address.country
else 'United States'
end as country
Note: Result of checking null by <> operator will return false.
And as documented: NULLIF is equivalent to a searched CASE expression
and COALESCE expression is a syntactic shortcut for the CASE expression.
So, combination of those are using two time of case expression.
For an example data in your table such as combinations of
'', null and as well as actual value than if you want to only actual value and replace to '' and null value by # symbol than execute this query
SELECT Column_Name = (CASE WHEN (Column_Name IS NULL OR Column_Name = '') THEN '#' ELSE Column_Name END) FROM Table_Name
and another way you can use it but this is little bit lengthy and instead of this you can also use IsNull function but here only i am mentioning IIF function
SELECT IIF(Column_Name IS NULL, '#', Column_Name) FROM Table_Name
SELECT IIF(Column_Name = '', '#', Column_Name) FROM Table_Name
-- and syntax of this query
SELECT IIF(Column_Name IS NULL, 'True Value', 'False Value') FROM Table_Name