String concatenation does not work in SQLite - sql

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.

Related

SQL join strings and coalesce()? [duplicate]

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

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

Update Concat statement SQL Server 2008 [duplicate]

I was looking for a CONCAT function in SQL Server 2008 R2. I found the link for this function. But when I use this function, it gives the following error:
Msg 195, Level 15, State 10, Line 7
'CONCAT' is not a recognized built-in function name.
Does the CONCAT function exists in SQL Server 2008 R2?
If not, how do I concatenate strings in SQL Server 2008 R2?
Just for completeness - in SQL 2008 you would use the plus + operator to perform string concatenation.
Take a look at the MSDN reference with sample code. Starting with SQL 2012, you may wish to use the new CONCAT function.
CONCAT is new to SQL Server 2012. The link you gave makes this clear, it is not a function on Previous Versions, including 2008 R2.
That it is part of SQL Server 2012 can be seen in the document tree:
SQL Server 2012
Product Documentation
Books Online for SQL Server 2012
Database Engine
Transact-SQL Reference (Database Engine)
Built-in Functions (Transact-SQL)
String Functions (Transact-SQL)
EDIT Martin Smith helpfully points out that SQL Server provides an implementation of ODBC's CONCAT function.
I suggest you cast all columns before you concat them
cast('data1' as varchar) + cast('data2' as varchar) + cast('data3' as varchar)
This should work for you.
CONCAT, as stated, is not supported prior to SQL Server 2012. However you can concatenate simply using the + operator as suggested. But beware, this operator will throw an error if the first operand is a number since it thinks will be adding and not concatenating. To resolve this issue just add '' in front. For example
someNumber + 'someString' + .... + lastVariableToConcatenate
will raise an error BUT '' + someNumber + 'someString' + ...... will work just fine.
Also, if there are two numbers to be concatenated make sure you add a '' between them, like so
.... + someNumber + '' + someOtherNumber + .....
NULL safe drop in replacement approximations for SQL Server 2012 CONCAT function
SQL Server 2012:
SELECT CONCAT(data1, data2)
PRE SQL 2012 (Two Solutions):
SELECT {fn CONCAT(ISNULL(data1, ''), ISNULL(data2, ''))}
SELECT ISNULL(CAST(data1 AS varchar(MAX)), '') + ISNULL(CAST(data2 AS varchar(MAX)), '')
These two solutions collate several excellent answers and caveats raised by other posters including #Martin Smith, #Svish and #vasin1987.
These options add NULL to '' (empty string) casting for safe NULL handling while accounting for the varying behaviour of the + operator pertaining to specific operands.
Note the ODBC Scaler Function solution is limited to 2 arguments whereas the + operator approach is scalable to many arguments as needed.
Note also the potential issue identified by #Swifty regarding the default varchar size here remedied by varchar(MAX).
(city + ', ' + state + ' ' + zip) as ctstzip for select
(city + ', ' + state + ' ' + zip) for insert
Only cast or convert if any field type is different from others.
On insert the value needs to be in the correct spot you need it be inserted. Using "as" will give you an error.
i.e.
Insert into testtable (ctstzip) Values ((city + ', ' + state + ' ' + zip))
Yes the function is not in sql 2008. You can use the cast operation to do that.
For example we have employee table and you want name with applydate.
so you can use
Select cast(name as varchar) + cast(applydate as varchar) from employee
It will work where concat function is not working.
You can use '+' between the strings that you want to concat like
SELECT string1 + string2
If one of those give conversion error like if one of the columns is an int column you should cast it before concatenating the columns like
SELECT (CONVERT(nvarchar, intColumn) + string2

Concatenation in SQL select query on Excel sheet

I have upload an Excel file now I want to read data from that sheet with a select query, and I want to concatenate 4 columns with separator _ as Type_name
The query I used is
select
*, '1' as fileid, 'Size' + '_' + 'Material' +'_' & 'Finishing' + '_' + 'Sides' as Type_Name
from [sheet1$]
where [Current Code] is not null or [Current Code] <> ''
All data is correctly returned by query except Type_Name
This query return Type_Name as Size_Material_Finishing_Sides
This depends on the type of SQL you are using.
I assume you are querying from within Excel or from MS Access. In this case you should use String1 & String2 & ....
However, here are other options:
SQL Server uses String1 + String2 + ....
Oracle, DB2, and some versions of MySQL use String1 || String2 || ....
The method supported across the most versions of MySQL is CONCAT(String1, String2, ...).
CONCAT() exists in most versions of SQL, but beware: you can't always use it to combine an arbitrary number of strings. In Oracle and DB2, for example, this function can only combine two strings.
The example you posted includes a mixture of + and &, which definitely won't work.
You are using & and + to concatenate? Why are you concatenating in the first place? Why not just use:
'Size_Material_Finishing_Sides' as Type_Name
If this is not the result you expected what result do you expect?

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