Select column, if blank select from another
How about combining COALESCE and NULLIF.
SELECT COALESCE(NULLIF(SomeColumn,''), ReplacementColumn)
FROM SomeTable
Sql: select another field if one field is blank or 0
Use CASE
.
SELECT CASE WHEN (special_selling_price IS NULL OR special_selling_price = 0)
THEN selling_price
ELSE special_selling_price
END AS SellPrice
FROM TableName
Check if a field is empty and get value from another column
You can use conditional IF
to check for empty stings
SELECT ti.*,
tt.text_en,
IF(tt.text_es = '', tt.text_en,tt.text_es) as text_es,
IF(tt.text_pt = '', tt.text_en,tt.text_pt) as text_pt,
IF(tt.text_fr = '', tt.text_en,tt.text_fr) as text_fr
FROM table1_items ti JOIN
table_translations tt
ON tt.name = ti.name AND tt.cat = ti.cat;
Select a column if other column is null
You need the ISNULL function.
SELECT ISNULL(a, b)
b
gets selected if a
is null.
Also, you can use the WHEN/THEN select option, lookup in BOL. Essentially: its c switch/case block meets SQL.
How to select value from second column if first column is blank/null in SQL (MS SQL)?
If you think you have empty strings you can try :
select
case when coalesce(PRICE1, '') <> '' then PRICE1
when coalesce(PRICE2, '') <> '' then PRICE2
when coalesce(PRICE3, '') <> '' then PRICE3
end AS PRICE
FROM A
Related Topics
Copy from One Database to Another Using Oracle SQL Developer - Connection Failed
MySQL "Create Table If Not Exists" -> Error 1050
How to Store Ordered Items Which Often Change Position in Db
Index for Multiple Columns in Activerecord
How to Do SQL Select Top N ... in As400
Postgresql Check Constraint for Foreign Key Condition
Kafka Connect Jdbc VS Debezium Cdc
Tsql Left Join and Only Last Row from Right
How to Get a View Table Query (Code) in SQL Server 2008 Management Studio
How to Quote Values Using Group_Concat
SQL Server Procedure Declare a List
SQL Query in Spark/Scala Size Exceeds Integer.Max_Value
Postgresql Insert from Select Returning Id
What's a Good Way (Or Tool) to Version Control a SQLite Database (Schema Only)