Concatenation in SQL select query on Excel sheet - sql

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?

Related

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

How do I concat strings from different columns and search using this expression

I have a lot of values like '1#1#15'
I need to build sql for DB (legasy FoxPro) request like this:
SELECT ba1 AS mest, bd1 AS Ukpg, be1 AS well, be12 AS probur, be13 AS iskust, be6 AS burDate
FROM ksmest
WHERE ((ba1 + '#' + bd1 + '#' + be1) IN ('1#1#15', '3#1#15'))
ba1, bd1 and be1 is numbers (not string).
As you see, I need to concat strings from different columns and search using this expression. My request is not works, it is just a sample.
Is it possible to solve? Or may be another ways?
You can combine the relevant columns into an aggregate field in the SELECT statement, converting them to string in the process - see 'mycolumn' in this example. Also your syntax there looks more like T-SQL, for VFP you would use INLIST()
select ;
ba1 AS mest, bd1 AS Ukpg, be1 AS well, be12 AS probur, be13 AS iskust, be6 AS burDate ;
alltrim(str(ba1, 12, 0)) + "#" + alltrim(str(bd1, 12, 0)) ;
+ "#" + alltrim(str(be1, 12, 0)) as mycolumn;
from ksmest ;
where inlist(mycolumn, "1#1#15", "3#1#15")

comparing two strings with a different format but same meaning

Is there a way i can use to compare the two strings 12 AD E4 9F and 12:ad:E4:9f and get a result which says they are similar. There are stored in different tables and i would like to create a view by joining the tables using strings as a joining criteria
You can convert the second string to upper case and then remove all ' ' and ':' before comparing the strings
Select UPPER(REPLACE('12:ad:E4:9f',':',' ') from dual;
One way would be to eliminate the characters before doing the comparison:
where replace(col1, ' ', '') = replace(col2, ':', '')
Or do the replacement of one separator to the other:
where replace(col1, ' ', ':') = col2
In general, SQL is case-insensitive in string comparisons, unless you explicitly set it to be case-sensitive. If you have case sensitivity one, then wrap the above in lower() or upper(). The functions replace(), upper(), and lower() are available in most databases (although some might have slightly different names).
Try this way for tsql.
You should use replace and upper function. In below example col2 is 12:ad:E4:9f
select *
from tab1 t1
join tab2 t2 on upper(t1.col1) = upper(REPLACE((t2.col2,':',''))
REPLACE(LTRIM(RTRIM(ProductDescription)), ':', '')
SELECT UPPER(ProductDescription)
then compare..

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.