SQL join strings and coalesce()? [duplicate] - sql

What does || do in SQL?
SELECT 'a' || ',' || 'b' AS letter

|| represents string concatenation. Unfortunately, string concatenation is not completely portable across all sql dialects:
ansi sql: || (infix operator)
mysql: concat ( vararg function ). caution: || means 'logical or' (It's configurable, however; thanks to #hvd for pointing that out)
oracle: || (infix operator), concat ( caution: function of arity 2 only ! )
postgres: || (infix operator)
sql server: + (infix operator), concat ( vararg function )
sqlite: || (infix operator)
hopefully the confusion is complete ...

SELECT 'a' || ',' || 'b' AS letter
will combine a letter.
The result become 'a,b'

It is a concat statement. It will concatenate the two strings.
Here is a helpful post!
What is the difference between "||" operator and concat function in Oracle?

In Oracle, SQLite3, and MySQL, it concatenates strings. Please see the Oracle documentation. The MySQL documentation.
Also, it's part of ANSI SQL, but read this for more information.

It's a concatenation operator. So you would get 'a,b' from that.
I think || will work on most RDBMS's. SQL Server requires the + operator (thanks to HVD for setting me straight!).

in oracle its a shortcut for concatenate
http://docs.oracle.com/cd/B19306_01/server.102/b14200/operators003.htm

Related

How to use a column named as keyword in a DB2 concat function

So I got this table FOO which has a column of the type VARCHAR and is named COMMENT (which happens to be a reserved keyword).
When I am trying to use it in a CONCAT function in my select the result is NULL.
How can I fix this?
SELECT
CONCAT(CONCAT(CONCAT(CONCAT('{"NAME":"', NAME), '","COMMENT":"'), COMMENT),'"}')
FROM
SOMESCHEMA.FOO
I also tried to use " or ' around COMMENT, but then it is interpreted as a VARCHAR...
2nd I used ` but that happens to print me the following error.
[Code: -104, SQL State: 42601] ILLEGAL SYMBOL "`". SOME SYMBOLS THAT MIGHT BE LEGAL ARE:.
I also tried to add the SCHEMA and the TABLE name in front of the column like:
CONCAT(CONCAT(CONCAT(CONCAT('{"NAME":"', NAME), '","COMMENT":"'), SOMESCHEMA.FOO.COMMENT),'"}')
But no luck.
Did you try this?
SELECT CONCAT(CONCAT(CONCAT(CONCAT('{"NAME":"', NAME
), '","COMMENT":"'
), "COMMENT"
),
'"}')
FROM SOMESCHEMA.FOO
That is, double quotes only around the column name.
I would find this simpler to read using the infix operator:
SELECT '{"NAME":"' CONCAT NAME CONCAT '","COMMENT":"' CONCAT "COMMENT" CONCAT '"}'
FROM SOMESCHEMA.FOO
or:
SELECT '{"NAME":"' || NAME || '","COMMENT":"' || "COMMENT" || '"}'
FROM SOMESCHEMA.FOO
It seems like DB2 also accept the ANSI/ISO SQL || concatenation:
SELECT
'{"NAME":"' || NAME || '","COMMENT":"' || COMMENT || '"}'
FROM
SOMESCHEMA.FOO

How to select rows with only Numeric Characters in Oracle SQL

I would like to keep rows only with Numeric Character i.e. 0-9. My source data can have any type of character e.g. 2,%,( .
Input (postcode)
3453gds sdg3
454232
sdg(*d^
452
Expected Output (postcode)
454232
452
I have tried using WHERE REGEXP_LIKE(postcode, '^[[:digit:]]+$');
however in my version of Oracle I get an error saying
function regexp_like(character varying, "unknown") does not exist
You want regexp_like() and your version should work:
select t.*
from t
where regexp_like(t.postcode, '^[0-9]+$');
However, your error looks more like a Postgres error, so perhaps this will work:
t.postcode ~ '^[0-9]+$'
For Oracle 10 or higher you can use regexp functions. In earlier versions translate function will help you :
SELECT postcode
FROM table_name
WHERE length(translate(postcode,'0123456789','1')) is null
AND postcode IS NOT NULL;
OR
SELECT translate(postcode, '0123456789' || translate(postcode,'x123456789','x'),'0123456789') nums
FROM table_name ;
the above answer also works for me
SELECT translate('1234bsdfs3#23##PU', '0123456789' || translate('1234bsdfs3#23##PU','x123456789','x'),'0123456789') nums
FROM dual ;
Nums:
1234323
For an alternative to the Gordon Linoff answer, we can try using REGEXP_REPLACE:
SELECT *
FROM yourTable
WHERE REGEXP_REPLACE(postcode, '[0-9]+', '') IS NULL;
The idea here is to strip away all digit characters, and then assert that nothing were left behind. For a mixed digit-letter value, the regex replacement would result in a non-empty string.

oracle sql - query to find special chars

Is there any SQL SELECT query that can be done in oracle to detect ascii characters such as LF, CR in fields? Basically any characters people have known to cause trouble in a oracle db environment in terms of breaking jobs/procedures.etc
I doubt this would work: - happy to use regex if possible
select * from table
where column like '%chr(13)%'
select * from table
where regexp_like(column, '(' || chr(13) || '|' || chr(10) || ')')
The regex used here is a form of (a|b|c) which matches the string if it contains a OR b OR c

String concatenation does not work in SQLite

I am trying to execute a SQlite replace function, but use another field in the function.
select locationname + '<p>' from location;
In this snip, the result is a list of 0s. I would have expected a string with the text from locationname and the '<p>' literals.
Try using || in place of +
select locationname || '<p>' from location;
From SQLite documentation:
The || operator is "concatenate" - it joins together the two strings of its operands.
The || operator is the concatenation in SQLite. Use this code:
select locationname || '<p>' from location;
For comparison,
SQLite ||
Oracle CONCAT(string1, string2) or ||
MySQL CONCAT(string1, string2, string3...) or || if PIPES_AS_CONCAT enabled
Postgres CONCAT(string1, string2, string3...) or ||
Microsoft SQL Server 2012+ CONCAT(string1, string2, string3...) or +
Microsoft Access +
for Visual Studio 2010, using the Data Sources designer or wizard, you're in trouble using || operator. Create a view in the sqlite db and create your data source(s) from that.
See also this thread.

What is the string concatenation operator in Oracle?

What is the string concatenation operator in Oracle SQL?
Are there any "interesting" features I should be careful of?
(This seems obvious, but I couldn't find a previous question asking it).
It is ||, for example:
select 'Mr ' || ename from emp;
The only "interesting" feature I can think of is that 'x' || null returns 'x', not null as you might perhaps expect.
There's also concat, but it doesn't get used much
select concat('a','b') from dual;
I would suggest concat when dealing with 2 strings, and || when those strings are more than 2:
select concat(a,b)
from dual
or
select 'a'||'b'||'c'||'d'
from dual
DECLARE
a VARCHAR2(30);
b VARCHAR2(30);
c VARCHAR2(30);
BEGIN
a := ' Abc ';
b := ' def ';
c := a || b;
DBMS_OUTPUT.PUT_LINE(c);
END;
output:: Abc def
There are two ways to concatenate Strings in Oracle SQL. Either using CONCAT function or || operator.
CONCAT function allows you to concatenate two strings together
SELECT CONCAT( string1, string2 ) FROM dual;
Since CONCAT function will only allow you to concatenate two values together. If you want to concatenate more values than two, you can nest multiple CONCAT function calls.
SELECT CONCAT(CONCAT('A', 'B'),'C') FROM dual;
An alternative to using the CONCAT function would be to use the || operator
SELECT 'My Name' || 'My Age' FROM dual;
Using CONCAT(CONCAT(,),) worked for me when concatenating more than two strings.
My problem required working with date strings (only) and creating YYYYMMDD from YYYY-MM-DD as follows (i.e. without converting to date format):
CONCAT(CONCAT(SUBSTR(DATECOL,1,4),SUBSTR(DATECOL,6,2)),SUBSTR(DATECOL,9,2)) AS YYYYMMDD