Solved

Sql Query for reading a comma delimited  textfile

Posted on 2008-06-10
4
662 Views
Last Modified: 2008-06-10
I want Sql query which can read a comma-delimited text file and create a table for it. I know that it can be done using loop by reading line by line. But I want a query which can directly do the task. I had found that query which can directly read a delimited textfile and create a table for it. But it has been misplaced. That was a direct sql query which also created a table alongwith reading the file. I want that query.
0
Comment
Question by:ShreyaTrivedi
  • 2
4 Comments
 
LVL 14

Accepted Solution

by:
rachitkohli earned 500 total points
ID: 21749727
assume i have a text file with following tab-delimited data
Name      Age      location
rachit      26      mumbai
nitin      21      chandigarh
rakhi      23      delhi
lokesh      31      calcutta

to insert into the table the code will be:

Create a table like this :
create table txtTable (name varchar(100), age varchar(5), location varchar(100))

To insert it will be :
bulk insert txtTable from 'c:\test\test.txt'

Query it now
select * from txtTable
0
 

Author Comment

by:ShreyaTrivedi
ID: 21751104
How can I assign delimiter to the query? What if I have comma delimited file?
0
 

Expert Comment

by:Oetzy
ID: 21751463
Hi,
try this:

INSERT INTO TableName
SELECT *
FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0', 'Text;Database=C:\TEST\;',
   'SELECT * FROM test1.csv')
0
 

Author Comment

by:ShreyaTrivedi
ID: 21752605
The following query works-
bulk insert txtTable from 'c:\test\test.txt' with (FieldTerminator=',')
0

Featured Post

Free Webinar: AWS Backup & DR

Join our upcoming webinar with experts from AWS, CloudBerry Lab, and the Town of Edgartown IT to discuss best practices for simplifying online backup management and cutting costs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
spx for moving values to new table 5 75
SQL Select - Finding chars in a column 2 71
Where clause in stored procedure 8 64
Find results from sql within a time span 11 55
If you having speed problem in loading SQL Server Management Studio, try to uncheck these options in your internet browser (IE -> Internet Options / Advanced / Security):    . Check for publisher's certificate revocation    . Check for server ce…
Recently, when I was asked to create a new SQL 2005 cluster, Microsoft released a new service pack for MS SQL 2005 what is Service Pack 3. When I finished the installation of MS SQL 2005 I found myself troubled why the installation of SP3 failed …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question