Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Sql Query for reading a comma delimited  textfile

Posted on 2008-06-10
4
Medium Priority
?
675 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 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

by Mark Wills PIVOT is a great facility and solves many an EAV (Entity - Attribute - Value) type transformation where we need the information held as data within a column to become columns in their own right. Now, in some cases that is relatively…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Integration Management Part 2

885 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