SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Substring into table
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

ranalk
Starting Member

49 Posts

Posted - 01/06/2014 :  06:57:15  Show Profile  Reply with Quote
Hi,

Would appreciate if someone can assist with substring NVARCHAR into table.

Declare @tmp NVARCHAR(MAX) = 'aaa,bbb,ccc,ddd'

I need it as rows in a table like:
aaa
bbb
ccc
ddd

Thanks in advance

bandi
Flowing Fount of Yak Knowledge

India
2206 Posts

Posted - 01/06/2014 :  07:35:56  Show Profile  Reply with Quote
Use User Defined function called "[dbo].[CustomSplit]" as follows:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


Create FUNCTION [dbo].[CustomSplit] (@sep char(1), @Words varchar(512))
RETURNS table
AS
RETURN (
WITH Pieces(n, start, stop) AS (
SELECT 1, 1, CHARINDEX(@sep, @Words)
UNION ALL
SELECT n + 1, stop + 1, CHARINDEX(@sep, @Words, stop + 1)
FROM Pieces
WHERE stop > 0
)
SELECT n,
SUBSTRING(@Words, start, CASE WHEN stop > 0 THEN stop-start ELSE 512 END) AS Words
FROM Pieces
)


GO
Declare @tmp NVARCHAR(MAX) = 'aaa,bbb,ccc,ddd'
SELECT * FROM dbo.CustomSplit(',', @tmp)

--
Chandu
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 01/06/2014 :  07:39:22  Show Profile  Reply with Quote
quote:
Originally posted by ranalk

Hi,

Would appreciate if someone can assist with substring NVARCHAR into table.

Declare @tmp NVARCHAR(MAX) = 'aaa,bbb,ccc,ddd'

I need it as rows in a table like:
aaa
bbb
ccc
ddd

Thanks in advance


see
http://visakhm.blogspot.com/2010/02/parsing-delimited-string.html
http://visakhm.blogspot.com/2013/01/delimited-string-split-xml-parsing.html

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

madhivanan
Premature Yak Congratulator

India
22754 Posts

Posted - 01/07/2014 :  07:40:33  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote
If it is always four parts, you can use parsename function

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000