473,396 Members | 1,812 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

T-SQL CONCAT() and +

NeoPa
32,556 Expert Mod 16PB
My understanding from these MSDN pages ({CONCAT (Transact-SQL)}, {+ (String Concatenation) (Transact-SQL)} & {SET CONCAT_NULL_YIELDS_NULL (Transact-SQL)}) is that the + will propagate NULLs, where the server's CONCAT_NULL_YIELDS_NULL property is set to ON as mine is, whereas CONCAT() will convert them implicitly to empty strings.

My code, a (relevant) fraction of my View, is :
Expand|Select|Wrap|Line Numbers
  1. CONCAT(([Employees].[First Name]+' '),[Employees].[Surname]) AS [EmpFullName]
All records where [Employees].[First Name] is NULL return no text. Those with values in both fields return the full name just as expected. The first CONCAT() parameter should be an empty string if [Employees].[First Name] is NULL or some text with a space on the end if not. Neither should stop the [Employees].[Surname] from getting into the result.

According to my understanding this should be the T-SQL equivalent of Access's :
Expand|Select|Wrap|Line Numbers
  1. ([Employees].[First Name]+' ') & [Employees].[Surname] AS [EmpFullName]
The latter works but the former doesn't. Am I going mad or am I missing something?
May 24 '15 #1

✓ answered by Rabbit

Yeah that, I heard it was buggy. Try right-clicking the view, script as, alter to new query window. That gives you just the SQL text, edit it there and execute it. That should prevent the query editor from messing with the SQL.

13 3644
ck9663
2,878 Expert 2GB
I was trying to recreate the issue that you're having but I am returning the expected results, with CONCAT_NULL_YIELDS_NULL both set to ON and OFF. Have you tried to remove the "+" sign and just include the space in the concat function?


~ CK
May 26 '15 #2
Rabbit
12,516 Expert Mod 8TB
Unfortunately I can't test this, our version of SQL Server is too old. And concat is an new function in 2012.

However, you seem to be having a lot of issues with the new functions introduced in 2012. Perhaps the database is set up with an older compatability level?
https://msdn.microsoft.com/en-US/lib...=sql.105).aspx

I can, however, confirm that the CONCAT_NULL_YIELDS_NULL option works as expected in 2008 R2
May 26 '15 #3
NeoPa
32,556 Expert Mod 16PB
Thank you both for your responses.

@Rabbit.
Great thought. I checked mine though, and it came up with "SQL Server 2012 (110)".

@CK.
I'm not really sure if I correctly understand what you're suggesting I try. Without the + I would surely lose the NULL propagation which is the purpose of the code.

Nevertheless, I tried both of these :
Expand|Select|Wrap|Line Numbers
  1. CONCAT(([Employees].[First Name] ' '),[Employees].[Surname]) AS [EmpFullName]
It didn't compile.
Expand|Select|Wrap|Line Numbers
  1. CONCAT([Employees].[First Name],' ',[Employees].[Surname]) AS [EmpFullName]
This returns the following error message which is totally in contradiction of the MSDN help page linked above, which explicitly says it handles two or more parameters.
Expand|Select|Wrap|Line Numbers
  1. ---------------------------
  2. Microsoft SQL Server Management Studio
  3. ---------------------------
  4. The CONCAT function requires 2 argument(s).
I did notice, however, and this will hopefully mean something to you guys where it means absolutely nothing to me, that when stored in the SQL View this code has been converted to :
Expand|Select|Wrap|Line Numbers
  1. { fn CONCAT(dbo.Employees.[First Name] + ' ', dbo.Employees.Surname) } AS EmpFullName
I understand it likes to fiddle around with my brackets ([]) for me, but the { fn CONCAT() } seems more than a little weird. Do you get something similar CK? Or does your SQL keep it's fundamental contents?
May 27 '15 #4
Rabbit
12,516 Expert Mod 8TB
Something is causing your query to revert to an older version of SQL.

The {fn concat()} syntax is an old odbc scalar function that takes only 2 arguments.
https://msdn.microsoft.com/en-us/lib...=sql.105).aspx

How are you accessing SQL Server to create your views? Is it through SSMS? What version of SSMS are you running? Or is it through a different method utilizing ODBC? Which ODBC driver are you using? Is it the most current version?
May 27 '15 #5
NeoPa
32,556 Expert Mod 16PB
Hi Rabbit.

I'm using MS SQL Server Management Studio and I'm logged in as (Domain) Administrator.

My code is worked out in a text editor (where I can format it clearly) then pasted into the SQL pane of the Design window. As soon as I move to the graphic table display the SQL is updated by SQL Server and that's the result :~)

If I can post more details please let me know. I can see nothing that explains why it would do this.

Questions in order :
  1. How are you accessing SQL Server to create your views?
    In SSMS. As described above.
  2. Is it through SSMS?
    Yes.
  3. What version of SSMS are you running?
    See table below.
  4. Or is it through a different method utilizing ODBC?
    No.
  5. Which ODBC driver are you using?
    None for this, but I'm using the native driver that came with SQL to connect to it and see the results.
  6. Is it the most current version?
    No idea, but it's what has been provided for me. I expect it should be good considering it came with the product which was downloaded fairly recently.
  • Microsoft SQL Server Management Studio
    11.0.2100.60
  • Microsoft Data Access Components (MDAC)
    6.3.9600.17415
  • Microsoft MSXML
    3.0 6.0
  • Microsoft Internet explorer
    9.11.9600.17416
  • Microsoft .NET Framework
    4.0.30319.34014
  • Operating System
    6.3.9600
May 27 '15 #6
Rabbit
12,516 Expert Mod 8TB
The version numbers look fine. Are you using the query designer or a regular query window? I've heard the query designer can be buggy.
May 27 '15 #7
NeoPa
32,556 Expert Mod 16PB
I'm not precisely sure what I'm using - or more accurately - what I'm using is called.

From SSMS I display the View, right-click on it and select Design. This opens up the View in what is called a Query Editor Window. That window is split into four distinct panes (which are not named anywhere I can see) :
  1. Top - The Table Layout Pane.
    Table layout view including graphic links between table fields.
  2. Next down - The Field List Pane.
    A list of all fields used whether to display, filter or sort.
  3. Next down - SQL Pane.
    This is where I paste my SQL into.
  4. Bottom - The Results Pane.
    This is another - but much slower, way of seeing the results.
May 27 '15 #8
Rabbit
12,516 Expert Mod 8TB
Yeah that, I heard it was buggy. Try right-clicking the view, script as, alter to new query window. That gives you just the SQL text, edit it there and execute it. That should prevent the query editor from messing with the SQL.
May 28 '15 #9
NeoPa
32,556 Expert Mod 16PB
Well I'll be {F*£$%} - a son of a gun!

That worked a treat. It looked very similar. It still used the scalar version of the CONCAT() function if I look at what's in there via the Design option, but not when I use the Script to option.

Best of all, it works as advertised.

Thank you so much for sticking with me and finding the solution for me Rabbit.
May 29 '15 #10
Rabbit
12,516 Expert Mod 8TB
My pleasure. I'll have to admit I don't ever use the designer so it took a while and more than a few Google searches to realize what was happening.
May 29 '15 #11
NeoPa
32,556 Expert Mod 16PB
So, how do you make updates to objects then? All in T-SQL?
May 29 '15 #12
Rabbit
12,516 Expert Mod 8TB
For the most part, I use T-SQL to create and update views, stored procedures, function, and tables. I will use the job editor gui to create and schedule jobs on sql server agent. I use SSIS in Visual Studio to create ETL packages and SSAS in Visual Studio to create OLAP cubes.
May 29 '15 #13
NeoPa
32,556 Expert Mod 16PB
My table manipulation SQL (As opposed to Data processing SQL) has always been pretty lightweight. It's an area I may well get more into. I've done some, just not a great deal and most of that's been quite recent.
May 29 '15 #14

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Steve | last post by:
Hi; I'm brand spanking new to sqlserver ( nice so far ). I need to make a simple data change across a list of tables. Basically replace an old date with a new date. However, the people I am...
2
by: Steve | last post by:
Hi; I have been writing a lot of short tsql scripts to fix a lot of tiny database issues. I was wondering if a could make an array of strings in tsql that I could process in a loop, something...
18
by: mountain man | last post by:
Greetings to all database professionals and laymen, Let us make a bold assumption that we have developed a software tool for the SQL Server environment which simply acts as an interface between...
11
by: Jeremy Pridmore | last post by:
Hi All, Here's a challenge. If there is a 'one word' answer to this, i.e. the name of a built in SQL Server function I can use, that would be great! However... I have a standard 1:m...
2
by: dynoweb | last post by:
I have several *.sql files with schema/data changes to be applied to our current database. Is there a way to create a TSQL script that could be run from the SQL Query Analyzer that would...
1
by: TOM GUGGER | last post by:
OMNI GROUP tgugger@aimexec.com T-SQL/ CONTRACT TO PERM/ ATLANTA
3
by: David Lozzi | last post by:
Howdy, ISSUE 1: See issue 2 below. I have a distance calculator on my site which works great. However, the users need to sort by distance, which make sense. I'm not sure how to do it other than...
16
by: David Lozzi | last post by:
Hello, I have some code that adds a new user. The new user has a checkboxlist of items which they can be associated with. I would like to send this list of items to TSQL along with the new user...
4
by: Martin Evans | last post by:
Hi, I'm getting: DBD::DB2::db do failed: SQL0440N No authorized routine named "CONCAT" of type "FUNCTION" having compatible arguments was found. SQLSTATE=42884 for some SQL like this:
1
by: M | last post by:
If I have table1 and table2 with table2 having multiple rows tied to a single row in table 1. What I am trying to do is set up a view that has one row that shows the following table1.uniqueid,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.