SQL Query with if statement - sql

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

Related

replace an empty value with null using 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

How to use a select statment inside a case statement

I will do a select statement inside my case statement like this:
CASE
WHEN d.dependent_speed_type = 4 THEN (SELECT column FROM tablename)
END
But this is not working.
Can I realize a select statement inside my case statement? If the value 4 is inside my column "d.dependent_speed_type" then it should be select a special value from another table. How is the right syntax?
You have to use something that will always return one value, like SELECT MAX(column) FROM tablename) for example.

Replace null values with just a blank

I currently have a sql statement that outputs data into an excel document. However, every time there is a cell that doesn't have data, it outputs NULL into the cell. I was wondering if there was a way that I could replace NULL with just a blank space? Someone suggested using coalesce to do this, however I haven't ever had the chance to use it so I will have to do some reading on it. Anyone have any other suggestions?
In your select you can put an IsNull/IfNull round the column. Not particularly efficient but does what you want.
MS SQL
Select IsNull(ColName, '') As ColName From TableName
MySQL
Select IfNull(ColName, '') As ColName From TableName
IFNULL is an Oracle extension (adopted by many other platforms). The "standard" SQL function is coalesce():
SELECT COALESCE(columnName, ' ') AS ColumnName
FROM tableName;
Microsoft SQL
UPDATE TABLE_NAME
SET COLUMN_NAME = ''
WHERE COLUMN_NAME IS NULL
NOTE: this will SET, not select the result in your TABLE
MySQL
select IFNULL(columnName, '') from tableName
IFNULL(expr1,expr2)
If expr1 is not NULL, IFNULL() returns expr1; otherwise it returns expr2. IFNULL() returns a numeric or string value, depending on the context in which it is used.
SELECT teacher.name, IfNull(dept.name, '') as Dept
FROM teacher left outer JOIN dept
ON teacher.dept=dept.id

Remove NULL from VARCHAR2 in ORACLE

There is a VARCHAR2 type column in my database table for store invoice_number. and there is a query to get max invoice_number as below.
select max(invoice_number) from invoice;
but there are no data in invoice table, above query return value as null. but in this case i need to replace this value to 0 instead of null. how could i do this ?
Using NVL:
SELECT NVL(MAX(invoice_number), '0')
Using the ANSI COALESCE:
SELECT COALESCE(MAX(invoice_number), '0')
Using DECODE:
SELECT DECODE(MAX(invoice_number), NULL, '0')
Using the ANSI CASE:
SELECT CASE
WHEN MAX(invoice_number) IS NULL THEN '0'
ELSE MAX(invoice_number)
END
Verdict
All work, I'd probably use NVL because COALESCE is ANSI but not known to necessarily work as fast as native alternatives.
You could use COALESCE
For instance:
select COALESCE(max(invoice_number),'0') from invoice;
select coalesce(max(invoice_number),0)
can also use Decode.
select decode(max(invoice_number),null, 0)

TSQL - ISNULL over multiple columns

I have a simple SQL query (SQL Server 2005) where I'm selecting from a table that contains multiple columns that have BIT values.
These columns are nullable so can contain NULL, 0 or 1.
There are a fair number of these columns and in my query I want to return zero if the value is NULL.
I'm currently using ISNULL like so:
SELECT Name, Age, ISNULL(LikesOranges,0), ISNULL(LikesApples,0), ISNULL(LikesPears,0)
FROM FoodPreferences
As I've mentioned, there are a lot of these BIT columns (much more than in the simple example above).
Is there a way I can use ISNULL over multiple columns like this:
SELECT ISNULL(*,0) FROM FoodPreferences
The above query doesn't work but you get what I'm trying to do - so I can avoid having to write an ISNULL statement for each column,
Thanks.
Try this:
SELECT COALESCE(LikesOranges, LikesApples, LikesPears) AS MyBit FROM FoodPreferences
This will return the first non-null value. If all fields are NULL the result is NULL.
UPDATE:
And the conclusion is:
SELECT ISNULL(COALESCE(LikesOranges, LikesApples, LikesPears),0) AS MyBit FROM FoodPreferences
so I can avoid having to write an
ISNULL statement for each column,
Run this query and copy the result to your select statement. system_type_id = 104 filters the result on bit columns.
select stuff((select ', isnull('+name+', 0)'
from sys.columns
where object_id = object_id('FoodPreferences') and
system_type_id = 104
for xml path('')), 1, 1, '')
Result:
-------------------------------------------------------------------------
isnull(LikesOranges, 0), isnull(LikesApples, 0), isnull(LikesPears, 0)
I don't think so. But an option might be to create a view onto that table and put all the ISNULL statements in the view. At least then you won't have to do it every time
eg.
CREATE VIEW vwFoodPreferences
AS
SELECT Name,
Age,
ISNULL(LikesOranges,0) AS LikesOranges,
ISNULL(LikesApples,0) AS LikesApples,
ISNULL(LikesPears,0) AS LikesPears
FROM FoodPreferences
Unfortunately, the simple answer is no.
You could write sql dynamically, but whatever happens, the final resulting sql would have to be ISNULL(a,0), ISNULL(b,0), ISNULL(c,0), ISNULL(d,0), etc
i think you can write a simple program and generate select clause by reading all columns and generating the select
while not this :
SELECT COALESCE(LikesOranges, LikesApples, LikesPears, 0) AS MyBit FROM FoodPreferences
https://learn.microsoft.com/en-us/sql/t-sql/language-elements/coalesce-transact-sql?view=sql-server-2017