The STUFF function
is insert a string into another string .It removes the specified
length of characters and insert new string to matching position
SELECT
STUFF('Hi
Rahul',
1,
2,
'Mr')
AS
Name
From the above query it will replace Hi
to Mr. You can use the STUFF with XML Path for concatenate the column
data easily.
DECLARE
@TempTable TABLE(Id
INT,Name
VARCHAR(20))
INSERT
INTO
@TempTable
SELECT
1,
'Raju'
UNION
ALL
SELECT
2,
'Manu'
UNION
ALL
SELECT
3,
'Vinu'
SELECT
*
FROM
@TempTable
SELECT
STUFF(
(SELECT
',' + Name
FROM @TempTable FOR XML PATH ('')), 1, 1, ''
',' + Name
FROM @TempTable FOR XML PATH ('')), 1, 1, ''
)
AS
NAMES
FROM
@TempTable WHERE
ID =
1
To handle this exact scenario, MS
introduced new functionality called
STRING_AGG
in
SQL 2017. STRING_AGG
Concatenates the values of string expressions and places
separator values between them.
DECLARE
@TempTable TABLE(ID
INT,Name
VARCHAR(20))
INSERT
INTO
@TempTable
SELECT
1,
'Raju'
UNION
ALL
SELECT
2,
'Manu'
UNION
ALL
SELECT
3,
'Vinu'
SELECT
*
FROM
@TempTable
SELECT
STRING_AGG(Name,
',')
AS
NAMES
FROM
@TempTable
I believe this would
be help full method for what you are trying to accomplish with the
XML/STUFF method.
Thanks it's helpful
ReplyDeleteWow,great information. I am sure the info on your blog will help others,Thanks.
ReplyDeleteSpring Training in Chennai
Spring framework Certification
Spring framework Training
Hibernate Training in Chennai
Hibernate Training in Adyar
Hibernate Training in Anna Nagar
Spring Hibernate Training in Chennai
Hibernate Training in T Nagar
ReplyDeleteVery creative post, truly this is a very good job. Your explanation is very superb and I appreciate your great efforts. I like a more valuable post from your blog...
Tableau Training in Chennai
Tableau Course in Chennai
Pega Training in Chennai
Excel Training in Chennai
Power BI Training in Chennai
Primavera Training in Chennai
Unix Training in Chennai
Tableau Training in Chennai
Tableau Course in Chennai
A IEEE project is an interrelated arrangement of exercises, having a positive beginning and end point and bringing about an interesting result in Engineering Colleges for a particular asset assignment working under a triple limitation - time, cost and execution. Final Year Project Domains for CSE In Engineering Colleges, final year IEEE Project Management requires the utilization of abilities and information to arrange, plan, plan, direct, control, screen, and assess a final year project for cse. The utilization of Project Management to accomplish authoritative objectives has expanded quickly and many engineering colleges have reacted with final year IEEE projects Project Centers in Chennai for CSE to help students in learning these remarkable abilities.
ReplyDeleteSpring Framework has already made serious inroads as an integrated technology stack for building user-facing applications. Spring Framework Corporate TRaining the authors explore the idea of using Java in Big Data platforms.
Specifically, Spring Framework provides various tasks are geared around preparing data for further analysis and visualization. Spring Training in Chennai
I am having this error
ReplyDelete'STRING_AGG' is not a recognised built in function
Reason please...
It might be due to version compatibility issue. As per below microsoft link it will work from 2017 onward. Please check your version
Deletehttps://docs.microsoft.com/en-us/sql/t-sql/functions/string-agg-transact-sql?view=sql-server-ver15
Yes I think it's introduced 2017 onward.
DeleteThis blog is really nice and informative blog, The explanation given is really comprehensive and informative.
ReplyDeleteDOT NET Training in Bangalore
DOT NET Training in Chennai
Dot NET Training in Marathahalli
DOT NET Training Institute in Marathahalli
DOT NET Course in Bangalore
AWS Training in Bangalore
Data Science Courses in Bangalore
DevOps Training in Bangalore
PHP Training in Bangalore
Spoken English Classes in Bangalore
If Column has same name multiple times then string_agg is providing multiple times but where as stuff gives it single time. is there a way to get single name even if name repeats multiple times using String_agg
ReplyDeleteAmazing post.Thanks for sharing.........
ReplyDeleteIELTS Coaching in Hyderabad
IELTS Coaching in Bangalore
IELTS Coaching in Pune
IELTS Coaching in Gurgaon
IELTS Coaching in Delhi
Great post. keep sharing such a worthy information
ReplyDeletecyber security course in bangalore
cyber security training in chennai
Great post. keep sharing such a worthy information
ReplyDeletecyber security course in bangalore
cyber security training in chennai
I read this information,Keep sharing such type of post.
ReplyDeleteSQL Classes in Pune
This post is so interactive and informative.keep update more information...
ReplyDeleteios training in bangalore
ios training institute in bangalore
ReplyDeleteWonderful blog. It is really informative to all.keep update more information about this
Software Testing Course in Bangalore
Software Testing Course in Hyderabad
Software Testing Course in Pune
Software Testing Training in Gurgaon
Software Testing course in Delhi