Creating temp column upon logic - sql

How can I create a temp (empty) column in which values will be inserted upon logic:
SELECT ..., *temp_col*
FROM *table*
IF *table.column_1* is not null, THEN *temp_col=table.column_1*
else temp_col=table.column_2
Note: Colume_1 is a CLOB field which will be transformed to TEXT.
someone advised me to use the EXIST condition.

In SQL, you use the CASE expression or the COALESCE() function:
SELECT ...,
COALESCE(table.column_1, table.column_2) as temp_col
FROM table;

Using Case :
SELECT ...,
temp_col= CASE WHEN table.column_1 is not nul THEN table.column_1
ELSE
table.column_2 END
FROM table;

Related

How to link multiple SELECT statements wth an OR operator

I would need a SELECT statement which returns a Boolean value based on the result of two independent Boolean columns (connected with 'OR'). Does anybody have the syntax for this?
If your database supports boolean, then you can simply put the or expression in the select:
select t.*, (bool_col1 or bool_col2) as new_bool_col
from t;
Select case when (boolean condition) then column name from table name.
You can add multiple case condition to fetch the columns of conditional basis
in MySQL you can use IF() Function, like this:
SELECT IF(bool1 = 'true' OR bool2 = 'true', 'true', 'false')
FROM Yourtable;
Thanks, the comment of jarlh solved it: "Simply select col1 or col2 from tablename"

Spark SQL: Specify column name resulting from UDF in WHERE clause

I have written a UDF function which will return a column (0 or 1) after processing 2 columns. I need to have my select query such that it returns those records for which this value is 1.
I wrote the query as below:
SELECT number, myUDF(col1, col2) as result
FROM mytable
WHERE result is not null
However it doesn't recognize the column name 'result'. Is there any special syntax needed so that it recognizes this new output column? Thanks.
CASE statement should solve the problem here:
SELECT number,
CASE when myUDF(col1, col2) = 1 then myUDF(col1, col2) END as result
FROM mytable

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

SQL - is it possible to have an if inside the values of an insert

I need to write a script to insert data selected from one table into another. The old table stores a value 'Yes' in one column but I want to insert 1 instead of 'Yes'
Is there any way to do this. In my head this is what I want to do:
insert into new_table (new_col1, new_col2)
values (select from old_table(old_col1, (if old_col2='Yes' then 1 else 0))
First: if you base your insert on a SELECT, the must not use the VALUES clause.
To get a conditional value, use the (ANSI standard) CASE statement:
insert into new_table (new_col1, new_col2)
select old_col1,
case
when old_col2 = 'Yes' then 1
else 0
end
from old_table
An Oracle only more compact form would be the decode() statement (but I'd recommend to use the CASE, because it's more readable and portable to other DBMS as well)
insert into new_table (new_col1, new_col2)
select old_col1,
decode(old_col2, 'Yes', 1, 0)
from old_table
What you are looking for is a CASE statement.
CASE
WHEN old_col2='Yes' then 1
ELSE 0
END

How can I limit columns returned based on their value?

I have a table which looks like
index customer_number ABC CWD ROE BEE
1 1 0 0 0 1
and I want to return only the field names that have value 1 in this case 'BEE'
I found that by SHOW FIELDS I can get the names of the fields but how I can say show the field names where field value = 1?
I would use CASE statement here.
SELECT
index, customer_number,
CASE
WHEN abc=0 THEN 'abc'
WHEN cwd=0 THEN 'cwd'
END
FROM
table_name
You can't do this in a general way.
What you can do is write a sql statement like this:
select index, customer_number, decode (ABC, 1, "ABC", null) || decode (CWD, 1, "CWD", null) || decode (ROE, 1, "ROE", null) || decode (BEE, 1, "BEE", null) from aTable
It will display the column names for each entry where the value equals to one. It is oracle sql, so if you use a different rdbms the syntax will vary.
The beat it to death answer is to use CASE statements, one for each column. Something like:
SELECT CASE WHEN index=1 THEN "index" ELSE "no_index" END as i,
CASE WHEN customer=1 THEN "customer" ELSE "no_customer" END as c,
CASE WHEN ...
This is not something that SQL was really meant to do, and would be better done with application logic.
That said, if you really wanted to do it, you would probably need to involve a temp table and a SPROC:
Get the row and determine which fields are set.
Use that information to create a temp table with only the set fields.
Insert the data into that temp table, then select the rows from there.
It would be a huge mess of SQL to replace what would amount to only a few lines of application code. Probably not worth it.