How to convert this IF statement in SQL using CASE statement
if address='US\Madison Drive' then 'ZIP1234'
elseif address='US\123 Madison Dr' then 'ZIP1234'
elseif address'US\123 Madison-Dr' then 'ZIP1234'
ELSE 'ZIP9999' END
I need to rewrite this statement using CASE statement in where condition
Select a.ID, NAME, Address, ZIP from
table_A a left join table_B b
on a.ID = b.ID#
where {I need to put if condition mention above}
How to convert this IF statement in SQL using CASE statement
case address
when 'US\Madison Drive' then 'ZIP1234'
when 'US\123 Madison Dr' then 'ZIP1234'
when 'US\123 Madison-Dr' then 'ZIP1234'
else 'ZIP9999'
end
another option for this particular example
case when address in (
'US\Madison Drive',
'US\123 Madison Dr',
'US\123 Madison-Dr')
then 'ZIP1234'
else 'ZIP9999'
end
case address
when ('US' || CHR (92) || 'Madison Drive') then 'ZIP1234'
when ('US' || CHR (92) || '123 Madison Dr') then 'ZIP1234'
when ('US' || CHR (92) || '123 Madison-Dr') then 'ZIP1234'
else 'ZIP9999' end ```
**'\' was giving error**
Related
This an example I received but after research I still am stuck. He wants me to return the email address, if no email return telephone phone number, if no telephone number return cell (all one column). I have joined my tables but not sure if I should use a WHERE NOT EXISTS or an IF/ELSE.
SELECT *
FROM entity e
LEFT OUTER JOIN telephone t ON t.id_number = e.id_number
LEFT OUTER JOIN email eml ON eml.id_number = e.id_number
you can use coalesce function which returns first not null result, not sure what do you mean by returning the cell, but i think you got the idea
with xx as
(select 'email#mail.com' email, null telephone, null cell
from dual
union all
select null email, '123456' telephone, null cell
from dual
union all
select null email, null telephone, '5' cell
from dual)
select coalesce(email, telephone, cell) first_not_null from xx
You can use the COALESCE function that will return the first non-null value.
I ended up getting it to work like this:
CASE WHEN TRIM(eml.email_address) IS NOT NULL THEN eml.email_address
WHEN TRIM(t.telephone_number) IS NOT NULL THEN 'H: (' || t.area_code || ')' || SUBSTR(t.telephone_number, 1, 3) || '-' || SUBSTR(t.telephone_number, 4, 4)
WHEN TRIM(t2.telephone_number) IS NOT NULL THEN 'C: (' || t2.area_code || ')' || SUBSTR(t2.telephone_number, 1, 3) || '-' || SUBSTR(t2.telephone_number, 4, 4)
ELSE 'No Email/Phone Found'
END AS "EMAIL/PHONE"
This would be the answer :
SELECT Case When eml.emailaddress is not null then eml.emailaddress
When eml.emailaddress IS NULL
AND t.Phonenumber is NOT NULL THEN t.Phonenumber
Else t.cellNumber
End as ContactInfo
FROM entity e
LEFT OUTER JOIN telephone t ON t.id_number = e.id_number
LEFT OUTER JOIN email eml ON eml.id_number = e.id_number
I'm trying to pad to the left and right of a case statement with 3 asterisks. It runs but the values don't show up for the Null values. Any ideas?
SELECT p.patientfirstname || ' ' || p.patientlastname AS "Patient"
,CASE WHEN i.insuranceid IS NULL THEN RPAD(LPAD('No insurance', 3, '*'), 3, '*')
ELSE i.insurancename
END "Insurance Name"
FROM patient p
FULL OUTER JOIN insurance i ON (p.insuranceid = i.insuranceid);
As I wrote in my comment, if you want a constant string, just use one and do away with the padding:
SELECT p.patientfirstname || ' ' || p.patientlastname AS "Patient"
,CASE WHEN i.insuranceid IS NULL THEN '***No insurance***'
ELSE i.insurancename
END "Insurance Name"
FROM patient p
FULL OUTER JOIN insurance i ON (p.insuranceid = i.insuranceid);
Padding is used to ensure a fixed-size string when working with transforming strings that are of variable shorter sizes. See example:
select lpad('Hola',7, '*'), lpad('Namaste',7, '*'), lpad('Hello',7, '*') from dual;
***Hola |Namaste |**Hello
select length(lpad('Hola',7, '*')), length(lpad('Namaste',7, '*')), length(lpad('Hello',7, '*')) from dual;
7 |7 |7
I need to make to lower only 1st character of the string
SOME_FUNCTION('Switcher USA');
make it to lower:
select SOME_FUNCTION_1('Switcher USA') from dual;
sql -> switcher USA
Perhaps there is exist something like this function:
http://www.techonthenet.com/oracle/functions/lower.php
Also, at the same time - if the word starts with the abbreviation (two or more upper-case characters in a row) - I would like to nothing happened:
select SOME_UPGRADE_FUNCTION('USA switcher') from dual;
sql -> USA switcher
What is it for? I need to use this output string in middle of the sentence something like this:
Are you sure you want to enable the switch USA?
Not this:
Are you sure you want to enable the Switch USA?
Thanks
Something like this?:
select case when substr(text_value, 1,2) = upper(substr(text_value, 1,2)) then text_value
else lower(substr(text_value, 1, 1)) || substr(text_value, 2)
end
from dual
For example:
select case when substr('USA switch', 1,2) = upper(substr('USA switch', 1,2)) then 'USA switch'
else lower(substr('USA switch', 1, 1)) || substr('USA switch', 2)
end
from dual
union all
select case when substr('Switch USA', 1,2) = upper(substr('Switch USA', 1,2)) then 'Switch USA'
else lower(substr('Switch USA', 1, 1)) || substr('Switch USA', 2)
end
from dual
Here's another way using just a query. It could be put into a function for reuse. Compare the first two letters to the uppercased version of the first two letters. If the same (uppercase), use as-is. If not, lowercase it.
SQL> with tbl(str) as (
select 'USA test' from dual
union
select 'Test USA' from dual
union
select 'USAs team' from dual
)
select
case
when regexp_substr(str, '^(.){2}', 1, 1) =
UPPER(regexp_substr(str, '^(.){2}', 1, 1))
THEN str
else
LOWER(regexp_replace(str, '^(.*)( .*)$', '\1')) ||
regexp_replace(str, '^(.*)( .*)$', '\2')
end converted
from tbl;
CONVERTED
--------------------------------------------------------------------------------
test USA
USA test
USAs team
SQL>
This sql is working but the second substring, is the order num. For these we need to add a '0' at the end, to make it 8 pos now. Not sure how to do this in SQL.
SELECT
ALL SUBSTR(UANOTL,1,4) AS CODE1, SUBSTR(UANOTL,5,7) AS ORDER#,
UAATHN, UANOTD
FROM ASTDTA.NOTEH1 T01
WHERE SUBSTR(UANOTL,1,4) = 'REM '
You need the concatenation operator:
SELECT ALL
SUBSTR(UANOTL,1,4) AS CODE1,
SUBSTR(UANOTL,5,7) || '0' AS ORDER#,
UAATHN, UANOTD
FROM ASTDTA.NOTEH1 T01
WHERE SUBSTR(UANOTL,1,4) = 'REM '
you just put an & operator before the string and the 0 you want to add
so 0 in front would be
select ('0' & ORDER#)
and at the end would be
select (ORDER# & '0')
Hey all, how can i go about query off a made AS Tablename in a query of mine?
(its a long query so i have shortened it to the needed lines for this example)
SELECT
TO_CHAR(SYSDATE, 'YYYYMMDD') AS CURDATE,
TO_CHAR(A.DUE_DATE, 'YYYYMMDD') AS DUE,
UGRP.GROUP_CODE AS UGRPCODE,
A.DETAILS,
TRIM(STF.IDENTIFIER_1) || ', ' || TRIM(STF.IDENTIFIER_2) || ' ' || TRIM(STF.IDENTIFIER_3) AS STAFF,
FROM REC.CUSTOM_ATTRIBUTES E
INNER JOIN REC.SERVICE_REQUESTS SR
INNER JOIN REC.IDENTIFIERS STF ON A.ASSIGNED_STAFF_EID = STF.OWNER_EID ON E.OWNER_EID = SR.EID
WHERE (TYP.TYPE_CODE = 'SRSRTYPE')
AND (A.ASSIGNED_STAFF_EID <> 2000478)
AND STAFF = 'BARKER, BOB'
ORDER BY SR.SERVICE_REQUEST_NUM
Naturally the AND STAFF = 'BARKER, BOB" will not work for me.
My question is how can I query on that column?
Thanks!
David
Repeat the formula in your WHERE clause.
... AND TRIM(STF.IDENTIFIER_1) || ', ' || TRIM(STF.IDENTIFIER_2) || ' ' || TRIM(STF.IDENTIFIER_3) = 'BARKER, BOB' ...