0% found this document useful (0 votes)
9 views3 pages

L44 SQL-Developer-Lecture-23-1

The document explains two T-SQL functions: STUFF, which replaces a specified length of characters in a string, and FOR XML PATH, which consolidates multiple row values into a single row. It provides examples of how to use these functions with a temporary table containing string values. Additionally, it discusses the COALESCE and ISNULL functions for handling NULL values in SQL queries.

Uploaded by

ambeydeveloper
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views3 pages

L44 SQL-Developer-Lecture-23-1

The document explains two T-SQL functions: STUFF, which replaces a specified length of characters in a string, and FOR XML PATH, which consolidates multiple row values into a single row. It provides examples of how to use these functions with a temporary table containing string values. Additionally, it discusses the COALESCE and ISNULL functions for handling NULL values in SQL queries.

Uploaded by

ambeydeveloper
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

STUFF :

Stuff is another T-SQL Function which is used to delete a specified length of


characters within a string and replace with another set of characters.

Intellipaat Software Solutions Pvt. Ltd.


Example :
SELECT STUFF('SQL SERVER is USEFUL',5,6,'DATABASE')

FOR XML PATH :


This is use to appear multiple row values of a single column in a
single row.
Multiple row values of a single column Single row value
Use of XML Path

create table #NumberTable(NumberCols Varchar(10))

Intellipaat Software Solutions Pvt. Ltd.


insert into #NumberTable(NumberCols ) VALUES ('First')
insert into #NumberTable(NumberCols ) VALUES ('Second')
insert into #NumberTable(NumberCols ) VALUES ('Third')
insert into #NumberTable(NumberCols ) VALUES ('Fourth')
insert into #NumberTable(NumberCols ) VALUES ('Fifth')
Option 1: This is the smartest way.
DECLARE @listStr VARCHAR(MAX)
SELECT @listStr = COALESCE(@listStr+',' , '') + NumberCols
FROM NumberTable
SELECT @listStr

Please make a note that COALESCE returns the first NOT NULL value
from the argument list we pass.
Intellipaat Software Solutions Pvt. Ltd.
.

Option 2: This is the smart but not the best way; though I have seen similar
code many times.
DECLARE @listStr VARCHAR(MAX)
SET @listStr = ''
SELECT @listStr = @listStr + NumberCols + ','
FROM #NumberTable
SELECT SUBSTRING(@listStr , 1, LEN(@listStr)-1)

I sometime use ISNULL(NumberCols,’NullValue’) to convert NULL values to


other desired value.

You might also like