Solved

Sql Query for reading a comma delimited  textfile

Posted on 2008-06-10
4
647 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
Comment Utility
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
Comment Utility
How can I assign delimiter to the query? What if I have comma delimited file?
0
 

Expert Comment

by:Oetzy
Comment Utility
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
Comment Utility
The following query works-
bulk insert txtTable from 'c:\test\test.txt' with (FieldTerminator=',')
0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Join & Write a Comment

I've encountered valid database schemas that do not have a primary key.  For example, I use LogParser from Microsoft to push IIS logs into a SQL database table for processing and analysis.  However, occasionally due to user error or a scheduled task…
So every once in a while at work I am asked to export data from one table and insert it into another on a different server.  I hate doing this.  There's so many different tables and data types.  Some column data needs quoted and some doesn't.  What …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

771 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now