Solved

Sql Query for reading a comma delimited  textfile

Posted on 2008-06-10
4
668 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

635 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