I have to concatenate 2 columns (ex. FIRSTANME and LASTNAME).
I do it this way:
FIRSTNAME || ' ' || LASTNAME`.
If one of them is null, but the other one is not null, I get null as concatenation result.
And I want following behavior
FIRSTNAME = null and LASTNAME = "Smith" ==>
FIRSTANME || ' ' || LASTNAME == ' Smith'.
How to solve this in DB2?
Use coalesce
...
CONCAT( COALESCE(firstname,'') , COALESCE(lastname,'') )
Or using the || concat operator
...
COALESCE(firstname,'') || COALESCE(lastname,'')
Note that IBM recomments using the keyword concat and not the || operator.
Concat: http://publib.boulder.ibm.com/infocenter/dzichelp/v2r2/index.jsp?topic=%2Fcom.ibm.db2.doc.sqlref%2Ffconc.htm
Coalesce: http://publib.boulder.ibm.com/infocenter/dzichelp/v2r2/index.jsp?topic=%2Fcom.ibm.db2.doc.sqlref%2Ffcoal.htm
Related
SELECT *
FROM employee
WHERE is_deleted != true
AND (:lastname IS NULL
OR lastname ILIKE '%'||lastname||'%'
OR :firstname IS NULL
OR firstname ILIKE '%'||:firstname||'%'
OR :middlename IS NULL
OR middlename ILIKE '%'||:middlename||'%');
I have a full name column and I need to filter by first name, last name or patronymic, depending on what the user enters (or last name and first name together) b tell me how to implement
Your logic is correct except the the various name criteria should be ANDed together:
SELECT *
FROM employee
WHERE is_deleted != true AND
(:lastname IS NULL OR lastname ILIKE '%' || lastname || '%') AND
(:firstname IS NULL OR firstname ILIKE '%' || :firstname || '%') AND
(:middlename IS NULL OR middlename ILIKE '%' || :middlename || '%');
SELECT *
FROM employee
WHERE is_deleted != true
AND (:search_term IS NULL OR
(split_part(full_name, ' ', 1) ILIKE '%' || :search_term || '%' OR
split_part(full_name, ' ', 2) ILIKE '%' || :search_term || '%' OR
split_part(full_name, ' ', 3) ILIKE '%' || :search_term || '%'));
This query will use the split_part() function to extract the first, second, and third parts of the full name, and then use them in the filtering. The ILIKE operator is used for case-insensitive matching. If the search_term parameter is null, the filtering will be skipped, and all the rows will be returned.
So I basically wanna display this (whole row in ONE column):
I like [type column] cake with [icing column] and a [fruit column].
The result should be:
Cake_Column
----------------
I like chocolate cake with whipped_cream and a cherry.
I like strawberry cake with vanilla_cream and a lemon_slice.
etc.
etc.
I need some sort of TO_CHAR statement that does ([column] "some text" [column]) "new_column_name";
What am I supposed to know?
You have two options for concatenating strings in Oracle:
CONCAT
Using ||
CONCAT example:
CONCAT(
CONCAT(
CONCAT(
CONCAT(
CONCAT('I like ', t.type_desc_column),
' cake with '),
t.icing_desc_column),
' and a '),
t.fruit_desc_column)
Using || example:
'I like ' || t.type_desc_column || ' cake with ' || t.icing_desc_column || ' and a ' || t.fruit_desc_column
Did you try the || operator ?
Concatenation Operator Documentation from Oracle >>>
select 'i like' || type_column || ' with' ect....
Below query works for me #Oracle 10G ----
select PHONE, CONTACT, (ADDR1 || '-' || ADDR2 || '-' || ADDR3) as Address
from CUSTOMER_DETAILS
where Code='341';
O/P -
1111 abc#gmail.com 4th street-capetown-sa
The Oracle/PLSQL CONCAT function allows to concatenate two strings together.
CONCAT( string1, string2 )
string1
The first string to concatenate.
string2
The second string to concatenate.
E.g.
SELECT 'I like ' || type_column_name || ' cake with ' ||
icing_column_name || ' and a ' fruit_column_name || '.'
AS Cake FROM table;
Try this:
SELECT 'I like ' || type_column_name || ' cake with ' ||
icing_column_name || ' and a ' fruit_column_name || '.'
AS Cake_Column FROM your_table_name;
It should concatenate all that data as a single column entry named "Cake_Column".
I have the following CREATE View statement where I am concatenating a couple fields. It grabs the 1st 100 characters of the first field, then appends an integer field
CREATE OR REPLACE VIEW $"schema_1"."tbl_225_customid" AS
SELECT tbl_225.id,
("substring"((field_2)::text, 0, 100)::text) || ' (' || "field_1" || ')' as fullname
FROM schema_1.tbl_225;
This works fine when there is data in field_2, however, when field_2 is null, the view shows NULL instead of just the integer part of the expression.
I tried wrapping this in a coalesce statement like this, but it throws an error.
CREATE OR REPLACE VIEW $"schema_1"."tbl_225_customid" AS
SELECT tbl_225.id,
COALESCE(("substring"((field_2)::text, 0, 100)::text),'') || ' (' || "field_1" || ')' as fullname
FROM schema_1.tbl_225;
How do I write the concat string such that, if field_2 is null, it will use '' for that and still result in value?
I would recommend simply using string function concat_ws() for this: it ignores null value by design, so you just don't need to worry about them:
select
tbl_225.id,
concat_ws('', substring(field_2::text, 0, 100), ' (' || field_1 || ')') as fullname
from schema_1.tbl_225;
Use coalesce():
SELECT tbl_225.id,
COALESCE(substring(field_2::text, 0, 100) , '') || coalesce(' (' || "field_1" || ')', '') as fullname
FROM schema_1.tbl_225;
So I basically wanna display this (whole row in ONE column):
I like [type column] cake with [icing column] and a [fruit column].
The result should be:
Cake_Column
----------------
I like chocolate cake with whipped_cream and a cherry.
I like strawberry cake with vanilla_cream and a lemon_slice.
etc.
etc.
I need some sort of TO_CHAR statement that does ([column] "some text" [column]) "new_column_name";
What am I supposed to know?
You have two options for concatenating strings in Oracle:
CONCAT
Using ||
CONCAT example:
CONCAT(
CONCAT(
CONCAT(
CONCAT(
CONCAT('I like ', t.type_desc_column),
' cake with '),
t.icing_desc_column),
' and a '),
t.fruit_desc_column)
Using || example:
'I like ' || t.type_desc_column || ' cake with ' || t.icing_desc_column || ' and a ' || t.fruit_desc_column
Did you try the || operator ?
Concatenation Operator Documentation from Oracle >>>
select 'i like' || type_column || ' with' ect....
Below query works for me #Oracle 10G ----
select PHONE, CONTACT, (ADDR1 || '-' || ADDR2 || '-' || ADDR3) as Address
from CUSTOMER_DETAILS
where Code='341';
O/P -
1111 abc#gmail.com 4th street-capetown-sa
The Oracle/PLSQL CONCAT function allows to concatenate two strings together.
CONCAT( string1, string2 )
string1
The first string to concatenate.
string2
The second string to concatenate.
E.g.
SELECT 'I like ' || type_column_name || ' cake with ' ||
icing_column_name || ' and a ' fruit_column_name || '.'
AS Cake FROM table;
Try this:
SELECT 'I like ' || type_column_name || ' cake with ' ||
icing_column_name || ' and a ' fruit_column_name || '.'
AS Cake_Column FROM your_table_name;
It should concatenate all that data as a single column entry named "Cake_Column".
I have a set of table as following
customer(cus_id,cus_first_name,cus_last_name);
insert into customer values ('c001', 'tan', 'wah khang');
I want to create a select statement to display the first name join with the last name.
Example :
tan wah khang
is that possible?
You can use the (this is not called "join" but) concatenation *|| (double pipe)* operator:
SELECT (cus_first_name || ' ' || cus_last_name) AS full_name
FROM customer
|| isn't quite equivalent to MySQL's CONCAT_WS. CONCAT_WS eliminates the delimiter if one of the operands is NULL. So if firstname is NULL and lastname is 'Smith', in MySQL:
CONCAT_WS(' ', firstname, lastname) returns "Smith"
whereas, in Oracle:
firstname || ' ' || lastname returns " Smith" (prepended with a space)
I'd love to know if there's a true equivalent, or if you'd have to write a stored procedure to emulate CONCAT_WS. It's terribly useful.
select cus_first_name || ' ' || cus_last_name from customer
Yes:
select cus_first || ' ' || cus_last from your_table;
In Oracle, PostgreSQL, DB2, Informix:
select cus_first_name || ' ' || cus_last_name from customer
In SQL-Server:
select cus_first_name + ' ' + cus_last_name from customer
In MS-Access:
select cus_first_name & ' ' & cus_last_name from customer
In MySQL:
select concat(cus_first_name , ' ', cus_last_name) from customer
In Informix:
select concatenate(cus_first_name,concatenate(' ',cus_last_name)) from customer