Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
DB Engine
SQL ServerMSDESQL Server CE
Services
Analysis (Data Mining)Analysis (OLAP)DTSIntegration ServicesNotification ServicesReporting Services
Programming
CLRConnectivitySQLXML
Other Technologies
ClusteringEnglish QueryFull-Text SearchReplicationService Broker
General
Data WarehousingPerformanceSecuritySetupSQL Server ToolsOther SQL Server Topics
DirectoryUser Groups
Related Topics
MS AccessOther DB ProductsMS Server Products.NET DevelopmentVB DevelopmentJava DevelopmentMore Topics ...

SQL Server Forum / Programming / SQL / July 2008

Tip: Looking for answers? Try searching our database.

INSERT

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
rajdaksha@gmail.com - 24 Jul 2008 09:19 GMT
Hi Guys

I have the data like this

Declare @var        nvarchar(max)
Declare @type        nvarchar(max)
Declare @Fields        nvarchar(max)
DECLARE @samplingID    INT

SET    @ID             =    5
SET     @VAR        =    '5~1~2'
SET     @TYPE        =    'D~U~U'
SET     @FIELDS
=    'Customer_name~address~city#Customer_name~address~city#Customer_name~address~city'

But i want to store into table Like below

ID      Var   Type   Fields
------------------------------------------------------------------------
5    5    D    Customer_name~address~city
5    1    U    Customer_name~address~city
5    2    U    Customer_name~address~city

Please Guys help on this

Thanks
Raj
vinu - 24 Jul 2008 10:37 GMT
Raj

Try this, not the best but works...

CREATE FUNCTION [dbo].[ufn_CSVToCoulmn] ( @StringInput
VARCHAR(8000),@delimiter varchar(1) )
RETURNS @OutputTable TABLE ( ID INT IDENTITY,[String] VARCHAR(1000) )
AS
BEGIN

DECLARE @String VARCHAR(1000)

WHILE LEN(@StringInput) > 0
BEGIN
SET @String = LEFT(@StringInput,
ISNULL(NULLIF(CHARINDEX(@delimiter, @StringInput) - 1, -1),
LEN(@StringInput)))
SET @StringInput = SUBSTRING(@StringInput,
ISNULL(NULLIF(CHARINDEX(@delimiter, @StringInput), 0),
LEN(@StringInput)) + 1, LEN(@StringInput))

INSERT INTO @OutputTable ( [String] )
VALUES ( @String )
END

RETURN
END
GO

Declare @var  varchar(1000)
Declare @type  varchar(1000)
Declare @Fields  varchar(1000)
DECLARE @samplingID INT
declare @Id int
SET @ID           = 5
SET  @VAR  = '5~1~2'
SET  @TYPE  = 'D~U~U'
SET
@FIELDS='Customer_name~address~city#Customer_name~address~city#Customer_name~address~city'

select @ID ID,v.[String] [VAR],t.[String] [Type],f.[String] from
(
select * from dbo.ufn_CSVToCoulmn(@var,'~')
)v inner join
(
select * from dbo.ufn_CSVToCoulmn(@TYPE,'~')
)t on t.id=v.id
inner join
(
select* from dbo.ufn_CSVToCoulmn(@FIELDS,'#')
)f on v.id=f.id

vinu
http://oneplace4sql.blogspot.com/

> Hi Guys
>
[quoted text clipped - 24 lines]
> Thanks
> Raj
--CELKO-- - 24 Jul 2008 19:34 GMT
You have re-discovered EAV.  Google it.  Read about it.  Don't do it
again. Ever.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.