concatenation of decimal and varchar in SQL - 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;

Related

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,%'

Combining a CASE WHEN statement & a column containing strings of type "Column 1 || '_' || Column 2" in Oracle SQL

I have a table consisting of three columns, which are called the following:
1) Month
2) Store_Type
3) City
I need this table to be expanded to contain five columns and the two columns that I wish to be added are detailed below.
Firstly, the query needs to create a new column called Store_Code. The Store_Code columns job is to store a numerical value which corresponds to what type of store it is.
I presume this would done using a CASE WHEN statement of the type:
SELECT Month,Store_Type,City,
CASE
WHEN Store_Type = 'Corner Shop' THEN '1'
WHEN Store_Type = 'Megastore' THEN '2'
WHEN Store_Type = 'Petrol Station' THEN '3'
....
ELSE '10'
END Store_Code
FROM My_Table
After this is complete, I need to create a column known as "Store_Key". The values contained within the Store_Key column need to be of the following form:
"The Month For That Row""The Store Type For That Row""The City associated with that row"_"The Store Code for that row"
I imagine the best way to create this column would be to use a query similar to the following:
SELECT (My_Table.Month || '_' || My_Table.Store_Type || '_' || My_Table.City || '_' ||
My_Table.Store_Code)
FROM My_Table
What I need is for these two separate queries to be combined into one query. I imagine this could be done by sub-setting the different SELECT queries but I am open to and grateful for any alternative solutions.
Thank you for taking the time to read through this problem and all solutions are greatly appreciated.
Do the case expression part inside a derived table (the subquery):
SELECT (My_Table2.Month || '_' || My_Table2.Store_Type || '_' || My_Table2.City || '_' ||
My_Table2.Store_Code)
FROM
(
SELECT Month,Store_Type,City,
CASE
WHEN Store_Type = 'Corner Shop' THEN '1'
WHEN Store_Type = 'Megastore' THEN '2'
WHEN Store_Type = 'Petrol Station' THEN '3'
....
ELSE '10'
END Store_Code
FROM My_Table
) My_Table2
If this is you trying to populate your new columns, then you need an update statement. I would use two updates to ensure you get the store_case committed for your store_code. Otherwise if you're deriving it in real time, the subquery select answer would be the way to go.
update my_table
set store_case =
case store_type
when 'Corner Shop' then 1
when 'Megastore' THEN 2
when 'Petrol Station' THEN 3
...
else 10
end case;
commit;
update my_table
set store_code = Month || '_' || to_char(Store_Type) || '_' || City || '_' || Store_Code;
commit;
Why to use sub query? It can be done within single query as following:
SELECT My_Table.Month || '_' || My_Table.Store_Type || '_' || My_Table.City || '_' ||
CASE
WHEN Store_Type = 'Corner Shop' THEN '1'
WHEN Store_Type = 'Megastore' THEN '2'
WHEN Store_Type = 'Petrol Station' THEN '3'
....
ELSE '10'
END as result
FROM My_Table
or you can use DECODE function as following:
SELECT My_Table.Month || '_' || My_Table.Store_Type || '_' || My_Table.City || '_' ||
DECODE(Store_Type,
'Corner Shop', '1',
'Megastore', '2',
'Petrol Station', '3'
....,
'10') -- this is default value same as else part of the case statement
as result
FROM My_Table
Cheers!!

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

SQL insert query with values like JUM'A ABDUL FATTAH causing missing , error

I am trying to insert data into a table which is derived dynamically.
I am selecting data from MASTER_TABLE which has col1 and col2. Depending on the values in col1 and col2, I am inserting the data in table SLAVE_col1value_col2value.
my sql query looks like:
insert_query_str :='INSERT INTO SLAVE_'
|| Col1
|| '_'
|| Col2
|| ' VALUES ( '''
|| CUST_NAME
|| ''','''
|| APPLICATION_DATA
|| ''')';
EXECUTE IMMEDIATE insert_query_str;
However, name like JUM'A ABDUL FATTAH cause the insert query string to look like this
INSERT INTO SLAVE_XYZ_ABC VALUES ( 'JUM'A ABDUL FATTAH','APPSPECIFICDATA');
and I get a missing comma error on the line.
I know if I double quote the name then this error can be sorted out but when I try that it hardcodes the || too and I don't get the real name in the query.
I am using Oracle SQL 11g and Toad.
Thank you in advance
Use placeholders together with USING clause:
insert_query_str :='INSERT INTO SLAVE_'
|| Col1
|| '_'
|| Col2
|| ' VALUES ( :cust_name, :app_data )';
EXECUTE IMMEDIATE insert_query_str USING CUST_NAME, APPLICATION_DATA;

IN clause not working in oracle

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.