January 2, 2012 at 11:14 am
Hi,
I have a table with a column called "DESC".
My requirement is ,if the input is "abc",then the output should be [a][c].
The datatype of the column is varchar(10).
Please help me in this.
Thanks
Thanks,
Pandeeswaran
January 2, 2012 at 11:57 am
DECLARE @SampleData TABLE ([DESC] VARCHAR(10))
INSERT INTO @SampleData
VALUES ('abc'), ('u&w<yz')
SELECT
CA.Result.value('.', 'VARCHAR(30)')
FROM
@SampleData SD
CROSS APPLY
(
SELECT
-- Surround every character with [] using an index from the Tally derived table
'[' + SUBSTRING(SD.[DESC], Tally.N, 1) + ']'
FROM
(
-- Tally table to use for indexing the input string
VALUES (1), (2), (3), (4), (5), (6), (7), (8), (9), (10)
) Tally(N)
WHERE
Tally.N <= DATALENGTH(SD.[DESC])
-- Use FOR XML PATH trick to concatenate all surrounded characters together
FOR XML PATH(''), TYPE
) CA(Result)
Edit: removed useless empty string column from the code
January 2, 2012 at 12:05 pm
Awesome! thanks!
Thanks,
Pandeeswaran
Viewing 3 posts - 1 through 2 (of 2 total)
You must be logged in to reply to this topic. Login to reply