I wish to create a table with many fields. And I need to do multiple case statement on each field. Please help me with a clean way (Function/Stored Proc) to do it, instead of writing case statement over and over.
Below is an example SQL.
SELECT
COALESCE(CASE WHEN Col1 = 'XXX' THEN 'YYY' ELSE NULL END,
CASE WHEN Col1 IN ('AAA','BBB') AND Col2 IS NULL THEN 'ZZZ' ELSE NULL END,
CASE WHEN Col1 = 'CCC' THEN 'DDD' ELSE Col2 END ) AS col_i
.
.
.
FROM table
Within a single query, you can use apply (or a subquery or CTE):
SELECT v.col_i
FROM table t CROSS APPLY
(VALUES (CASE . . . )
) v(col_i)
If you want this generally available, then don't use a function, use a computed column:
alter table t add col_i as
(case . . . );
This would then be available to anyone using the table.
I think we can rewrite your logic into a single CASE expression:
CASE WHEN Col1 = 'XXX'
THEN 'YYY'
WHEN Col1 IN ('AAA', 'BBB') AND Col2 IS NULL
THEN 'ZZZ'
WHEN Col1 = 'CCC'
THEN 'DDD'
ELSE Col2 END
What makes this work is that your ELSE values are NULL (except for the very last CASE expression), which logically means that COALESCE would just rollover to the next CASE expression.
Im sure this is an easy one.
How would I do a conditional value select on a column.
Basically if column1 ='Y' then display as "FOO" else if 'n' display "foobar"
select column1 from table1;
With a simple case expression:
select case column1 when 'Y' then 'FOO' else 'foorbar' end
from table1;
That assume a simple if/else. Your question specifies two values, and you can check both:
select case column1 when 'Y' then 'FOO' when 'n' then 'foorbar' end
from table1;
If you have any column1 values other than Y and n you'd get null; you can still specify a different value with an else even if you're testing for more than one explicit value:
select case column1 when 'Y' then 'FOO' when 'n' then 'foorbar' else 'bar' end
from table1;
An alternative syntax in Oracle is decode, I like this because it's nice and concise, but basically does the same as the case statement suggested above:
select decode(column1,'Y','FOO','foobar') from table1;
The syntax is as follows: decode( expression , search , result [, search , result]... [, default] ).
Further examples are in Oracles docs: http://www.techonthenet.com/oracle/functions/decode.php
Hello I have a query where I want to select the value of one of two fields depending if one is empty.
field1 and field2
I want to select them as complete_field
IF field1 is empty, then complete_field is field2
ELSE complete_field is field1
In php it would be done like:
$complete_field = $field1 == '' ? $field2 : $field1;
How would I do this in PostgreSQL?
I tried:
SELECT
(IF field1 = '' THEN field2 ELSE field1) AS complete_field
FROM
table
But it doesnt work.
Please help me :) Thanks
Use CASE WHEN .. THEN .. ELSE .. END, e.g.:
SELECT
(CASE WHEN (field1 IS NULL OR field1 = '') THEN field2 ELSE field1 END)
FROM table;
Check it out at the official docs.
Try COALESCE with NULLIF:
SELECT COALESCE(NULLIF(field1, ''), field2) AS the_field FROM my_table;
The ANSI SQL function Coalesce() is what you want.
select Coalesce(field1,field2)
from table;
Is it possible in an SQL query to only show a field if another field has data? For example, if Field1 <> '', then show the value in Field2 else don't show the value?
It can be done using a case statement. (At least in SQL Server)
select case when Field1 <> ''
then Field2
end as Field2
from YourTable
Sure (this works in Oracle and SQLite):
select
field1,
(case
when field1 is null then null
else field2
end) field2_wrapped
from my_table
if 'has no data' means the empty string (''), you need to use this statement:
SELECT Filed2 FROM Table1 WHERE Filed1<>''
If 'no data' means NULL value, you need use
SELECT Filed2 FROM Table1 WHERE NOT (Filed2 IS NULL)
Take a look at Standard SQL functions COALESCE() and NULLIF():
COALESCE(NULLIF(Field1, ''), Field2)
Is it possible to alias a column name and then use that in a CASE statement? For example,
SELECT col1 as a, CASE WHEN a = 'test' THEN 'yes' END as value FROM table;
I am trying to alias the column because actually my CASE statement would be generated programmatically, and I want the column that the case statement uses to be specified in the SQL instead of having to pass another parameter to the program.
This:
SELECT col1 as a,
CASE WHEN a = 'test' THEN 'yes' END as value
FROM table;
...will not work. This will:
SELECT CASE WHEN a = 'test' THEN 'yes' END as value
FROM (SELECT col1 AS a
FROM TABLE)
Why you wouldn't use:
SELECT t.col1 as a,
CASE WHEN t.col1 = 'test' THEN 'yes' END as value
FROM TABLE t;
...I don't know.
I think that MySql and MsSql won't allow this because they will try to find all columns in the CASE clause as columns of the tables in the WHERE clause.
I don't know what DBMS you are talking about, but I guess you could do something like this in any DBMS:
SELECT *, CASE WHEN a = 'test' THEN 'yes' END as value FROM (
SELECT col1 as a FROM table
) q
#OMG Ponies - One of my reasons of not using the following code
SELECT t.col1 as a,
CASE WHEN t.col1 = 'test' THEN 'yes' END as value
FROM TABLE t;
can be that the t.col1 is not an actual column in the table. For example, it can be a value from a XML column like
Select XMLColumnName.value('(XMLPathOfTag)[1]', 'varchar(max)')
as XMLTagAlias from Table
It should work. Try this
Select * from
(select col1, col2, case when 1=1 then 'ok' end as alias_col
from table)
as tmp_table
order by
case when #sortBy = 1 then tmp_table.alias_col end asc
I use CTEs to help compose complicated SQL queries but not all RDBMS' support them. You can think of them as query scope views. Here is an example in t-sql on SQL server.
With localView1 as (
select c1,
c2,
c3,
c4,
((c2-c4)*(3))+c1 as "complex"
from realTable1)
, localView2 as (
select case complex WHEN 0 THEN 'Empty' ELSE 'Not Empty' end as formula1,
complex * complex as formula2
from localView1)
select *
from localView2
Nor in MsSql
SELECT col1 AS o, e = CASE WHEN o < GETDATE() THEN o ELSE GETDATE() END
FROM Table1
Returns:
Msg 207, Level 16, State 3, Line 1
Invalid column name 'o'.
Msg 207, Level 16, State 3, Line 1
Invalid column name 'o'.
However if I change to CASE WHEN col1... THEN col1 it works
If you write only equal condition just:
Select Case columns1 When 0 then 'Value1'
when 1 then 'Value2' else 'Unknown' End
If you want to write greater , Less then or equal you must do like this:
Select Case When [ColumnsName] >0 then 'value1' When [ColumnsName]=0 Or [ColumnsName]<0 then
'value2'
Else
'Unkownvalue' End
From tablename
Thanks
Mr.Buntha Khin
SELECT
a AS [blabla a],
b [blabla b],
CASE c
WHEN 1 THEN 'aaa'
WHEN 2 THEN 'bbb'
ELSE 'unknown'
END AS [my alias],
d AS [blabla d]
FROM mytable
Not in MySQL. I tried it and I get the following error:
ERROR 1054 (42S22): Unknown column 'a' in 'field list'
In MySql, alice name may not work, therefore put the original column name in the CASE statement
SELECT col1 as a, CASE WHEN col1 = 'test' THEN 'yes' END as value FROM table;
Sometimes above query also may return error, I don`t know why (I faced this problem in my two different development machine). Therefore put the CASE statement into the "(...)" as below:
SELECT col1 as a, (CASE WHEN col1 = 'test' THEN 'yes' END) as value FROM table;
Yes, you just need to add a parenthesis :
SELECT col1 as a, (CASE WHEN a = 'test' THEN 'yes' END) as value FROM table;
make it so easy.
select columnnameshow = (CASE tipoventa
when 'CONTADO' then 'contadito'
when 'CREDITO' then 'cred'
else 'no result'
end) from Promocion.Promocion