0

I have data in text file (field1,field2,fiel3,field4) , (field1,field2,fiel3,field4),... I want to import this in sql server table .. plz help

(699,429,1,'2000-11-30 04:34:16'),(699,2170,2,'2000-11-30 04:07:23'),(699,2171,2,'2000-11-30 04:21:44'),(699,919,4,'2001-04-05 03:21:50'),(699,3160,5,'2000-11-30 04:16:29'),(699,1250,3,'2001-04-05 03:18:20')

4
  • Please show what you've tried. Commented Oct 31, 2014 at 11:49
  • i have tried to import via wizard in sql server but there is no option of importing data from text file into sql table....there exist option of importing data from excel to sql server table Commented Oct 31, 2014 at 11:56
  • In addition to using the Import / Export wizards or SSIS to move data into SQL Server there are also a few other options for doing this that are built into SQL Server. Some these other options include bcp, BULK INSERT, OPENROWSET as well as others Commented Oct 31, 2014 at 11:57
  • My data is store in .txt file in which every field is seperated with comma inside () bracket ... such as : (f1,f2,f3,f4),(f5,f6,f7,f8) where f1,f2,f3,f4 are record no 1 fields data and f5,f6,f7,f8 are data related to record 2. sory for bad english Commented Oct 31, 2014 at 11:59

2 Answers 2

1

Read Data from your text file and get it into SQL Server first. Then try to manipulate it with all the features avilable in t-sql.

I would do something like ....

Query

DECLARE @FileTable TABLE (FileData NVARCHAR(MAX))

INSERT INTO @FileTable
SELECT BulkColumn 
FROM OPENROWSET(BULK N'C:\Path_to_Text_File\Test_Text_FIle.txt', SINGLE_CLOB) AS Contents

;WITH CTE AS(
 SELECT REPLACE(
         REPLACE(
           REPLACE(
              REPLACE(FileData, '),(', '|')
               , '(', '') 
             , ')', '')
            , '''', '')   AS TextData
              FROM @FileTable
 ),Column_Split AS
 (
 SELECT CONVERT(XML,'<Columns><Column>'  
   + REPLACE(val,',', '</Column><Column>') + '</Column></Columns>') [Columns]
 FROM CTE C CROSS APPLY dbo.split(c.TextData, '|')
)
SELECT      
   [Columns].value('/Columns[1]/Column[1]','varchar(100)') AS Column1,    
   [Columns].value('/Columns[1]/Column[2]','varchar(100)') AS Column2,
   [Columns].value('/Columns[1]/Column[3]','varchar(100)') AS Column3,
   [Columns].value('/Columns[1]/Column[4]','varchar(100)') AS Column4
 FROM Column_Split

Result

╔═════════╦═════════╦═════════╦═════════════════════╗
║ Column1 ║ Column2 ║ Column3 ║       Column4       ║
╠═════════╬═════════╬═════════╬═════════════════════╣
║     699 ║     429 ║       1 ║ 2000-11-30 04:34:16 ║
║     699 ║    2170 ║       2 ║ 2000-11-30 04:07:23 ║
║     699 ║    2171 ║       2 ║ 2000-11-30 04:21:44 ║
║     699 ║     919 ║       4 ║ 2001-04-05 03:21:50 ║
║     699 ║    3160 ║       5 ║ 2000-11-30 04:16:29 ║
║     699 ║    1250 ║       3 ║ 2001-04-05 03:18:20 ║
╚═════════╩═════════╩═════════╩═════════════════════╝

Split Function Definition

CREATE FUNCTION [dbo].[split](  
  @delimited NVARCHAR(MAX),  
  @delimiter NVARCHAR(100)  
) RETURNS @t TABLE (id INT IDENTITY(1,1), val NVARCHAR(MAX))  
AS  
BEGIN  
  DECLARE @xml XML  
  SET @xml = N'<t>' + REPLACE(@delimited,@delimiter,'</t><t>') + '</t>'  

  INSERT INTO @t(val)  
  SELECT  r.value('.','varchar(MAX)') as item  
  FROM  @xml.nodes('/t') as records(r)  
  RETURN  
END
Sign up to request clarification or add additional context in comments.

1 Comment

Actually in my case i have data in this format (1005866,185749,110,'21,172 (Portugal) (20February 1997)',NULL) where last field is text field and there may exist ; or , in last field sql...bulk insert cause last field to split
0

Please try the following:

BULK INSERT DBNAME.dbo.TABLENAME FROM  FULLPATH  WITH (KEEPIDENTITY,  FIELDTERMINATOR = ',', ROWTERMINATOR = '\n' )

Please remember to fill in you DB Name, Table Name and FullPath.

Hope this helps.

2 Comments

Actually in my case i have data in this format (1005866,185749,110,'21,172 (Portugal) (20February 1997)',NULL) where last field is text field and there may exist ; or , in last field sql...bulk insert cause last field to split
plz help me... i have large amount of hard coded data in this format and i want to insert it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.