Replace multiple space with single space – SQL Server

Replace multiple space with single space – SQL Server

DECLARE @Temp TABLE (OriginalString VARCHAR(8000))

INSERT INTO @Temp (OriginalString)
SELECT ‘ Hello. This is Hello World. ‘

SELECT REPLACE(REPLACE(REPLACE(LTRIM(RTRIM(OriginalString)), ‘ ‘, ‘ ‘ + CHAR(7)), CHAR(7) + ‘ ‘, ”), CHAR(7), ”) AS [result]
FROM @Temp
WHERE CHARINDEX(‘ ‘, OriginalString) > 0

Happy Coding 🙂

Advertisements

Split a delimited string AND LOOP through

Split a delimited string AND LOOP through

DECLARE @Id VARCHAR(50)

SET @Id = ‘51,52,61,75,95’

WHILE LEN(@Id) > 0
BEGIN
PRINT LEFT(@Id, CHARINDEX(‘,’, @Id + ‘,’) – 1)

–Your SP Code

SET @Id = STUFF(@Id, 1, CHARINDEX(‘,’, @Id + ‘,’), ”)
END

Happy Coding 🙂