Update Concat statement SQL Server 2008 [duplicate] - sql

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

Related

[Informatica][ODBC 20101 driver][Microsoft SQL Server]Invalid operator for data type. Operator equals minus, type equals varchar

REPLACE(T.FirstName, ' ', '') + '.' +
REPLACE(REPLACE(T.LastName, ' ', ''), '''', '') + '.' + SUBSTRING(CONVERT(varchar(50), T1.Id), 1, 8) +
'#outlook.com' AS 'EmailAddress'
EmailAddress is the one of the column which has above logic. Informatica workflow failing due to conversion with below error. Can any one help me how to handle this.
Source is SQL Server, and target is Oracle
I get this error:
[Informatica][ODBC 20101 driver][Microsoft SQL Server]Invalid operator for data type. Operator equals minus, type equals varchar.
This is not Informatica error. This is an error thrown by MS SQL Server and passes on to Informatica.
This means, the error is thrown by the query. Debug the query on your source system. It seems it some part results in a minus that is then interpreted as substraction by the Query Optimizer.
{FN CONCAT( {FN CONCAT({FN CONCAT({FN CONCAT( {FN CONCAT(REPLACE(T.FirstName,' ',''), '.')}, REPLACE(REPLACE(
T.LastName,' ',''),'''',''))},'.' )}, SUBSTRING(convert(varchar(50),T1.PersonId),1,8))},'#Outlook.com')} AS 'EmailAddress'
Made the changes as above. SQL Server doesnt have any issue with '+' or 'CONCAT'. inforamtica was throwing error for '+' for concatenation in query. Now im able to load the data to Oracle table from SQL Server. Thanks all for the responses and help.

CONCAT function alternative in SQL Server 2008

I am trying to use CONCAT function in SQL Server 2008. It doesn't work because it is not available in that version:
$sql = "SELECT DISTINCT(CONCAT(Project, '-', FLOOR_ID)) AS value FROM dbo.IMP_MODEL_GEOMETRY WHERE Project LIKE '%".$test_term."%'";
When i was google "How to" I found a post here said using + instead. So I tried:
$sql = "SELECT DISTINCT( (Project + '-' + FLOOR_ID) ) AS value FROM dbo.IMP_MODEL_GEOMETRY WHERE Project LIKE '%".$test_term."%'";
But i got this message:
[Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Conversion failed when converting the nvarchar value ' 22067-' to data type int.
So, how should i solve this?
You are mixing datatypes for the + operation. In this case SQL server will attempt to convert VARCHAR expression to INT (this is done according to datatype precedence rules). You need to explicitly cast INT to VARCHAR:
SELECT COALESCE(Project, '')
+ '-'
+ COALESCE(CAST(FLOOR_ID AS VARCHAR(11)), '')
CONCAT function does not return NULL if the arguments are NULL. COALESCE(..., '') is required to match the behavior.
Try casting the FLOOR_ID to text:
SELECT DISTINCT
Project + '-' + CAST(FLOOR_ID AS VARCHAR(MAX)) AS value
FROM dbo.IMP_MODEL_GEOMETRY
WHERE Project LIKE '%".$test_term."%'";
The current error message mentions the string ' 22067-', which implies that the FLOOR_ID is the source of the problem.
Cast Project and FLOOR_ID to String like this -
CAST(Project AS VARCHAR("MAX LENGTH OF PROJECT FIELD"))
and same for FLOOR_ID

My query works in SQL Server 2017, but it does not work in SQL Server 2014

I am trying to replace values in a delimited list that I am stuck with, and I know it is against the normalization. However I have no choice. I have written a query that replaces a value in delimited list and avoids duplication if the value I am trying to replace it with already exists.
My query works perfectly fine in SQL Server 2017, but it does not work in SQL Server 2014. I get an error message. I need this query to work in SQL Server 2014. I would appreciate it a lot. My query is given below along with fiddle execution link. The error message is
Msg 156 Level 15 State 1 Line 4
Incorrect syntax near the keyword 'FROM'.
Msg 102 Level 15 State 1 Line 14
Incorrect syntax near 'inputs'.
Msg 102 Level 15 State 1 Line 23
Incorrect syntax near 'replacement'.
https://dbfiddle.uk/rdbms=sqlserver_2014&fiddle=9eb71e1e90b07c8c150004dc9a6d5107
UPDATE test
SET appValue = TRIM(',' FROM REPLACE(inputs.expression, inputs.pattern, replacement.value))
FROM test
CROSS APPLY
(SELECT
',' + appValue + ',' AS expression,
',' + '406' + ',' AS pattern,
',' + '506' + ',' AS replacement
) inputs
CROSS APPLY
(SELECT
CASE
WHEN inputs.expression LIKE '%' + inputs.replacement + '%'
THEN ','
ELSE inputs.replacement
END) replacement(value)
WHERE
inputs.expression LIKE '%' + inputs.pattern + '%'
The TRIM() function is new for Sql Server 2017. For older versions of Sql Server you have to use the older LTRIM() and RTRIM() functions, which don't have the same characters FROM string syntax, or other more complicated string functions like CHARINDEX() and SUBSTRING().
The easiest thing here might be to REPLACE() all spaces with a special character you know is not part of your data, then REPLACE() all commas with a space, trim the spaces using the old LTRIM()/RTRIM() functions, REPLACE() the spaces back to commas again, and REPLACE() the special character back to spaces.
REPLACE(REPLACE(RTRIM(LTRIM(REPLACE(REPLACE(column, ' ', '|'), ',', ' '))), ' ', ','), '|', ' ')
Or if you know there is likely to be just one extra trailing or leading comma, you can test for that and use SUBSTRING() to cut out just that character.

SQL Server, Oracle8i syntax

I am running the simple query below
select '''' + event_number + '''' +',' as Event_N
from mytable
which gives:
Event_N
---------------
'BAB0910000001',
'CDD0910000002',
'ODB0910000002',
'YDB0910000003',
'NYC0910000004',
Question 1: Is there a way in SQL that can show the result as:
Event_N
'BAB0910000001','CDD0910000002','ODB0910000002','YDB0910000003','NYC0910000004',
I could do a dirty step by pressing Delte and End but would not be efficient when more than 100 records.
I am using Aqua Data Studio 7.5.34 and the back end is MS SQL Server 2000/5/8.
Event_number data type is varchar.
Question 2: But when I tried to run this query, it showed me the error below. Could someone please help!!!
select '''' + event_key + '''' +',' as Event_K
from mytable
I am using Aqua Data Studio 7.5.34 and the back end is MS SQL Server 2000/5/8.
--event_key data type is int.
[Error] Script lines: 1-3 --------------------------
Conversion failed when converting the varchar value ''' to data type int.
Msg: 245, Level: 16, State: 1, Procedure: , Line: 1
I ran the following query, which gives:
select ''''||EVENT_KEY||'''' as Event_K
from mytable
EVENT_K
'28732033'
'28797708'
'28796943'
'28133100'
'28718239'
Question 3: Is there a way in SQL that could add a comma (,) to the end of these records and output the result similar to question 1?
I am using Aqua Data Studio 7.5.34 and the back end is oracle 8i
Question 1 and 3 - essentially the same as this question: Use SQL instead of Delete and End from keyboard
OMG Ponies' post includes links to a selection of techniques in Oracle, as well as SQLServer.
Question 2 requires an explicit CAST or CONVERT on the event_key, to change it into character instead of numeric data - like so:
select '''' + convert(varchar,event_key) + '''' +',' as Event_K
from mytable

SQL Server 2000:Invalid operator for data type. Operator equals add, type equals ntext

I've got some code that runs all lovely on SQL Server 2008, however I'm forced to try it on SQL Server 2000 server, where it falls down.
Basically I'm looking to combine two columns, with a comma in-between.
SELECT COALESCE(cardesc1, '') + ', ' + COALESCE(cardesc2, '') AS "Car Summary" FROM macros;
Solved it!
SELECT COALESCE(cardesc1, '') + ', ' + COALESCE(convert(varchar(100),cardesc2), '') AS "Car Summary" FROM macros;