Impala: LIKE does not capture CONCAT output - sql

Hi I'm not sure why CASE does not work when I run this on Impala.
So this query should check if at the end of account number there is exactly the same sort code as in other column in that database - but it does not do the job. The outcome of CONCAT is exactly as expected eg '%18002' but does not find the match.
SELECT
account_no
,sort_code
,CASE WHEN account_no LIKE CONCAT("'",'%',CAST(sort_code AS STRING),"'") THEN 1 ELSE 0 END AS CHECK
,CONCAT("'",'%',CAST(sort_code AS STRING),"'") AS CONCAT_OUTPUT
FROM bddaoao01p.aml_alerts_control_flags;
the output of the query:
any idea what's the problem here?

You can try the below -
SELECT account_no,sort_code,
CASE WHEN account_no LIKE CONCAT('%',CAST(sort_code AS STRING)) THEN 1 ELSE 0 END AS CHECK
CONCAT("'",'%',CAST(sort_code AS STRING),"'") AS CONCAT_OUTPUT
FROM bddaoao01p.aml_alerts_control_flags

Related

SQL CONCAT drops zeros from expression

I am trying different ways to put a 0 in front of month less than 10.
I tried the following expression but the 0 get dropped.
What am I doing wrong?
CASE
WHEN month([Transact_Date]) < 10
THEN CONCAT(str(0),STR(month([Transact_Date]),1))
ELSE month([Transact_Date])
END AS month_w_0
Thanks!
Tom
I think a left padding trick is what you want here. Assuming your database be SQL Server:
SELECT RIGHT('00' + STR(MONTH([Transact_Date])), 2) AS month_w_0
FROM yourTable;
You don't need a CASE expression for this. In more recent versions of SQL Server, the FORMAT function might also be able to handle this.
I have tested this and it returns the result you want:
select CASE WHEN mnt < 10 THEN
concat('0' , mnt)
ELSE mnt
END AS month_w_0
from ( select month([Transact_Date]) mnt
from test_t) A
I have realised what your problem is. YOu can do it this way too:
SELECT
CASE
WHEN month([Transact_Date]) < 10
THEN CONCAT(str(0),STR(month([Transact_Date]),1))
ELSE STR(month([Transact_Date]),1)
END AS month_w_0
from test_t
The problem is only the else part and I believe that is because case when clause returns only one type od data. In your then part you have tryed to retunr string and in the else part number.
Hope this helps...
Here is a demo

Separate sql value from one field

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.

Dynamic Label in Select

I was wondering if we can change the label of the select statement like we do for data in sql select using CASE
SELECT CASE column1 = 1 THEN 1 ELSE 0 END AS [Available]
But can we have a dynamic header something like
SELECT column1 AS <-- Available when 1 or Not Available when 0
This can be handled on the front end but its wise if we have it on backend. Any help or useful link is appreciated
You can do it with dynamic sql and if...else instruction but it not make sense for me. In relational database value in the cell tells you if something is available or not. If header tells you the same as cell it's duplicate information. If you want to description of the value you can use case syntax instead of 0/1 value
SELECT CASE when column1 = 1 THEN 'Available'
ELSE 'Not available'
END AS [Available]
Well, that would not make sense, as what would you expect the column name to be if you hade 2 rows, one with 1 (being available) and the other with 0(being Not Available)?
You would have to stick to something like
SELECT
CASE
WHEN column1 = 1
THEN 'Available'
ELSE 'Not available'
END as Availability
FROM YourTable

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:printing my own messages depending on query output

how can i print my own messages depending on the output of a SQL quesry.
Eg:
print "got it" when select * from emp wherempno=10; return atlest one record.
else " not presnet" when the above quesry returns 0 records
i just one one sql quesry and not a Pl/SQL code.I am using oracle 9 db.
You could try grabbing the total in a sub-query and then selectively returning the result in a case statement.
I don't have access to oracle at the moment, so the syntax may not be perfect, but something like below should work
select
case t.c
when 0 then 'not presnet'
else 'got it'
end as result
from
(select count(*) as c from emp wherempno=10) t
This works (tested on OracleXE):
SELECT CASE WHEN COUNT(1) = 0 THEN 'not present' ELSE 'got it' END
FROM emp
WHERE mpno = 10
Can you not use a ROWCOUNT to determine the number of rows affected, and then combine that with an IF statement to achieve what you are looking for?