Separate sql value from one field - sql

I have this:
**value**
S:581930640 | P:581930640
And I would like to get the value as in Oracle:
**valuaA ValueB**
581930640 581930640

select
case
when field like 's:%'
then substr(field,3,13)
else null
end as A,
case
when field like 's:%'
then substr(field,17)
else null
end as B
from table;
both the case condition are same, but the substring is different, this should do what you are trying to do.

Related

How to handle NULL values in WHERE clause and change target column based upon its encounter

I need the WHERE clause to change what column it is evaluating when NULL is encountered. For instance I'm trying to do something like this:
SELECT *
FROM customer c
WHERE CASE WHEN c.cust_id_1(#variable) IS NOT NULL THEN c.cust_id_1 = #variable
ELSE CASE WHEN c.cust_id_2(#variable) IS NOT NULL THEN c.cust_id_2 = #variable
ELSE c.cust_id_3 = #variable
Is something like this possible? One of the 3 cust_id's will not be NULL.
That seems like less than optimal table design, but isn't a simple COALESCE where you're after?
WHERE #variable = COALESCE(cust_id_1, cust_id_2, cust_id_3);
You don't need a CASE expression for this, you just need logical operators
SELECT *
FROM customer c
WHERE
(c.cust_id_1 IS NOT NULL AND c.cust_id_1 = #variable)
OR
(c.cust_id_2 IS NOT NULL AND c.cust_id_2 = #variable)
OR
(c.cust_id_3 IS NOT NULL AND c.cust_id_3 = #variable)

SQL CASE Statement Change the Column name in THEN clause

I'm beginner in SQL,is it possible to change column name in CASE statement.
I've a column name as "Inbound" the value for it can be '1' or '2'. If it's 1 the column name should be AS "IN" ELSE "OUT".
Any help is greatly appreciated.
You can not generate dynamic column names. Queries should be static. But what you could do is for instance:
select case when inbound = 1 then 'yes' end as in_result,
case when inbound = 2 then 'yes' end as out_result
from your_table

SQL , SQL QUERIES

I have the following condition:
if column is NULL, or have value like Unassessed or ABC then the result should be I.
I need help how to put three in one condition like below. Just confused with NULL value. And it is string in column.
JobAssessStage ='Unassessed','','ABC'
JobAssessStage in ('Unassessed','','ABC')
Use below condition with is null
JobAssessStage in ('Unassessed','ABC') or JobAssessStage is null
use case when for multiple condition
select case when JobAssessStage in ('Unassessed','ABC') or
JobAssessStage is null then 'I' else 'You' End
from yourtable
In some cases you can use also use COALESCE function (it returns the first non-null expression in a list) on JobAssessStage field:
COALESCE(JobAssessStage, 'X') in ('Unassessed','ABC','X')
If JobAssessStage is NULL with COALESCE the value you're testing becomes X.
I told you "in some cases" because you have to be sure that the value you have chosen (X) is not a value that your field can take.
You can formulate your like below:
SELECT *
FROM table
WHERE 1 = 1
AND (JobAssessStage IN ('Unassessed', 'ABC')
OR JobAssessStage IS NULL);

CASE WHEN NULL makes wrong result in SQLite?

I have a table with a column of image type, the table has some rows but all the rows haven't had any image yet, they are all null. To test the CASE WHEN NULL, I've tried this and it gave a strange result:
SELECT CASE myImageColumn WHEN NULL THEN 0 ELSE 1 END FROM myTable
All the returned rows were in a column of 1's (I thought 0's). What is wrong here?
Your help would be highly appreciated!
Thank you!
You can't compare with NULL like that, you should try:
SELECT CASE WHEN myImageColumn IS NULL THEN 0 ELSE 1 END
FROM myTable
Use a different form of CASE instead:
SELECT CASE WHEN myImageColumn IS NULL THEN 0 ELSE 1 END FROM myTable
Two useful links:
http://www.sqlite.org/nulls.html
http://www.sqlite.org/lang_expr.html
There's a bypass:
CASE ifnull(myValue, 'someUniqueStringOrValue')
WHEN 'someUniqueStringOrValue' THEN 0 -- this means null
WHEN 'someNormalValue' THEN 1
END

SQL Server Inline CASE WHEN ISNULL and multiple checks

I have a column with some nulls. If that column is null, I want to condition output for it based on values in another column.
So if case when null (if c=80 then 'planb'; else if c=90 then 'planc')
How would you code that in an inline T-SQL statement?
thanks.
COALESCE(YourColumn, CASE c WHEN 80 then 'planb' WHEN 90 THEN 'planc' END)
You can also use the nested case statement. Assuming that the first column is called DataColumn.
CASE
WHEN DataColumn IS NULL THEN
CASE c
WHEN 80 THEN 'planb'
WHEN 90 THEN 'planc'
ELSE 'no plan'
END
ELSE DataColumn
END