IN clause not working in oracle - sql

I have a query which uses IN clause and it is not working for below case:
Select *
from table1
where
Rollno || '/' || UserId IN ('1/001,2/002')

It is not working because you haven't wrapped each value in single quotes ' :
SELECT *
FROM table1
WHERE Rollno || '/' || UserId IN ('1/001','2/002')

Notulysses has the right syntax for in. But, if you have to deal with a string, you can rephrase this as like:
where ',' || Rollno || '/' || UserId || ',' like '%,' || '1/001,2/002' || ',%'
like is a better approach. Sometimes in the real world, you might have to deal with comma-delimited strings.

Related

How to concatenate multiple columns where value may be be NULL

I need to pull a query from multiple columns from a table. Some rows will have data in one column, others will have two, three, and even four.
I tried to use this construct:
SELECT person_uid,('(' || major || NVL((',' ||second_major), '') || NVL((',' ||third_major), '') || NVL(',' ||fourth_major, '') || ')' ) AS MAJORS FROM academic_study
But the result would be like this:
6231 (BUMG,BUMK,,)
19091 (TDST,TDPG,,)
I need the parentheses, but not the trailing commas.
I could potentially strip out the extra commas in post processing, but I would prefer to do it in the SQL. I am using ORACLE.
You should fix your data model! Storing multiple columns with parallel data is awkward.
One method is:
select person_uid,
( '(' || major ||
(case when second_major is not null then ',' || second_major end) ||
(case when third_major is not null then ',' || third_major end) ||
(case when fourth_major is not null then ',' || fourth_major end) ||
')'
)

comma separated list oracle sql

I have a list of comma separated values in a single field and need to find all rows which meet a certain criteria. The code I am using is:
select * from table where column_name in
(
select regexp_substr('A+','A-','aabn','[^,]+', 1,level) from table
connect by regexp_substr('A+','A-','aabn', '[^,]+', 1,level) is not null
);
so I want to find all the rows which contain A+, A- and aabn
Why not just use a single regular expression?
where regexp_like(column_name, '(^|,)(A[+]|A[-]|aabn)($|,)')
Here is a DB<>fiddle.
Also, as powerful as regular expressions are, you should not be storing multiple values in a single string. Oracle has many other options for storing multiple values, including the traditional association/junction table.
You don't need regular expressions, just use LIKE to check that the value you require is a sub-string of the column (both surrounded by delimiter characters):
If you want all 3 in the list then use AND:
SELECT *
FROM table_name
WHERE ',' || column_name || ',' LIKE '%,A+,%'
AND ',' || column_name || ',' LIKE '%,A-,%'
AND ',' || column_name || ',' LIKE '%,aabn,%'
If you want any 1 in the list then use OR:
SELECT *
FROM table_name
WHERE ',' || column_name || ',' LIKE '%,A+,%'
OR ',' || column_name || ',' LIKE '%,A-,%'
OR ',' || column_name || ',' LIKE '%,aabn,%'

Concate columns using oracle SQL

C.ADDRESS1 || ',' || C.ADDRESS2 || ',' || C.ADDRESS3
pa.address_line_1,
pa.address_line_2,
pa.address_line_3 from per_addresses_f pa ;
how to join three column as single address column with oracle SQL concatenate
How to write query for joining 3 columns as single column?
You can write the concatenation as you've written it, but you have to make sure that all the components are strings. If some of them isn't, put it in TO_CHAR function. It's also good to put an alias to your result of concatenated columns:
SELECT C.ADDRESS1 || ',' || C.ADDRESS2 || ',' || C.ADDRESS3 as concatenated_adress
FROM your_table c
EDIT : After question was edited with concrete columns that are used, table name and with desired alias stated in question columns:
SELECT pa.address_line_1 || ',' || pa.address_line_2 || ',' || pa.address_line_3
as "Emergency Address"
FROM per_addresses_f pa
You can do this:
CONCAT(C.ADDRESS1, ',', C.ADDRESS2, ',', C.ADDRESS3) AS newColumnName
Try this :
SELECT firstname ||' '|| lastname AS employee_name FROM employee;
You can also try nested CONCAT assuming all address_line fields have char datatype:
SELECT CONCAT(CONCAT(CONCAT(pa.address_line_1,','),CONCAT(pa.address_line_2,',')),pa.address_line_3) AS address FROM per_addresses_f pa
But clearly pipe operator as suggested by #Goran Kutlaca is much simpler.
There are multiple ways to Concate the column in BIP.
1.
SELECT pa.address_line_1 || ',' || pa.address_line_2 || ',' ||
pa.address_line_3
as "Emergency Address" FROM per_addresses_f pa
2.
SELECT
CONCAT(CONCAT(CONCAT(pa.address_line_1,','),CONCAT(pa.address_line_2,',')),pa.address_line_3)
AS address FROM per_addresses_f pa
3.
select
CONCAT(pa.address_line_1,',',pa.address_line_2,',',pa.address_line_3)
as address FROM per_addresses_f pa

format column headers during concat,oracle

I need to format column headers in the output of sql while using concat
Eg:
SELECT '' || to_char(sysdate,'ddmmyyyy') as DATE || ',' || ENO|| ',' || NAME|| ''
FROM EMP;
would retrieve me
ORA-00923: FROM keyword not found where expected.
Need the output as:
DATE ENO NAME
-----------------
251013 7560 RAM
251013 7561 ROSS
This format works
SELECT to_char(sysdate,'ddmmyyyy') || ',' || ENO || ',' || NAME as "DATE,ENO,NAME"
FROM EMP
but I have an issue with
ORA-00972: identifier is too long
when the length of column names inside as "" exceeds 30 characters
Eg:
SELECT to_char(sysdate,'ddmmyyyy') || ',' || ENO || ',' || NAME ||
',' || EMPLOYEE_IDENTIFICATION_NUMBER as "DATE,ENO,NAME,EMPLOYEE_IDENTIFICATION_NUMBER"
FROM EMP;
To achieve this output you have to build your query like this
SELECT to_char(sysdate,'ddmmyyyy') || ',' || ENO || ',' || NAME as "DATE,ENO,NAME" FROM EMP
You need to move the alias, if you really need it, at the end of the SELECT clause. Also the empty strings ('') can be removed:
SELECT to_char(sysdate,'ddmmyyyy') || ',' || ENO || ',' || NAME as DATE FROM EMP;

concatenation of decimal and varchar in SQL

I have 2 columns in my table :
PER_DAY (DECIMAL(10,2))
DESCP (VARCHAR)
I want to concat the two columns in my query...
For Ex:
PER_DAY=0.5 DESCP='ABCDEFGHIJ'
I want them to be displayed as
0.5-ABCDEFGHIJ
I'm using IBM DB2.
have you tried
select char(per_day) || DESCP from table;
EDIT:
select CAST(per_day AS VARCHAR(13)) || '-' || DESCP from table;
How about this:
select trim(varchar(per_day)) || '-' || DESCP from table;