

SELECT CONCAT_WS(',', SCHEMA_NAME(schema_id), NULL, type_desc COLLATE database_default) FROM sys.tables WHERE type = 'U' ĬONCAT_WS ignore NULL values in the columns, so this CSV would be wrong. Let’s have a look what’s happening if we have a NULL value. We accomplished the task easily and the output is exactly what we needed. SELECT CONCAT_WS(',', SCHEMA_NAME(schema_id), name, type_desc COLLATE database_default) FROM sys.tables WHERE type = 'U' SELECT 'Schema name,Table name,Type desc' AS Result UNION ALL Let's try to create a CSV file with all table names in our database. The concat_ws function requires 3 to 254 arguments.”Įxample: SELECT CONCAT_WS(' ', 'Value 1') AS Result

It requires a minimum of three input values (a separator and two arguments), otherwise, an error is raised. If you need the same separator between 2 or more strings, then CONCAT_WS is the command you are looking for. The return type is a string, the length and type of which depend on the input.Ĭoncatenates a variable number of arguments with a delimiter specified in the 1 st argument (separator). Argument - Is a value of any type to be concatenated Separator - Is a value of any character type (nvarchar, varchar, nchar, or char). Now, let’s have a look at the new function CONCAT_WS which is introduced in MS SQL Server 2017.ĬONCAT_WS (separator, argument1, argument2, …, ) ') AS "Drop Statement" FROM sys.tables WHERE type = 'U' Genertate drop statements for all user tables in database Also, there is no need for casting the data because the input data types are automatically casted to string values in this example).Īnd, finally let’s try to make the ultimate select statement from the MS SQL FORMATMESSAGE using CONCAT. The NULL values are replaced by an empty string. Let’s see how CONCAT handles NULL values.Īs you can see this is also nice.
Sql server concat code#
But also like in the other post I personally don’t like the parameters and for me the code is much readable by using FORMATMESSAGE. Let’s try to accomplish the same task with CONCAT as in the MS SQL FORMATMESSAGE article.Įxample: DECLARE nvarchar(max) = 'Damir' ĭECLARE nvarchar(max) = 'Gin and tonic' The concat function requires 2 to 254 arguments.”Įxample: SELECT CONCAT('Damir') AS Result It requires a minimum of two input values, otherwise, an error is raised. You can use this function for cases like the one described on this blog under MS SQL FORMATMESSAGE. You can find more details about it on the official Microsoft docs site. The return type is a string, the length and type of which depend on the input. string_value - A string value to concatenate to the other values Simple as that it returns a string that is the result of concatenating two or more string values.ĬONCAT (string_value1, string_value2, …, ) MS SQL Server function CONCAT is introduced in MS SQL version 2012. I don't understand why this is, because this is the alias I have given it and want to reference it for things like joins further in the pipeline.Facebook Twitter Google + LinkedIn Pinterest Email Share. The issue I have is how can I insert a separator in the concatenation part that creates NewColumn in the pivot.Īlso when I then run a select * from pivotexample2 query, it says Invalid object name 'pivotexample2'. The distinct values in the category column can change so I needed a dynamic solution (as above). SELECT = + QUOTENAME(category) + ',' FROM (select distinct category from #temp ) as tmp Which I can convert into wide format: DECLARE AS VARCHAR(MAX)=''

Insert into #temp values ('2', 'dinner', 'no') Insert into #temp values ('2', 'lunch', 'yes') Insert into #temp values ('2', 'breakfast','no')

Insert into #temp values ('1', 'dinner','yes') Insert into #temp values ('1', 'lunch','no') Insert into #temp values ('1', 'breakfast','yes') The code was heavily influenced by: SQL Server dynamic PIVOT query? create table #temp I have a table in long format which I convert to wide format dynamically.
