Reading a Text File With SQL Server
Last Updated :
15 Oct, 2021
Here we will see, how to read a text file with SQL Server. We can read the text file using the OPENROWSET(BULK ) function.
OPENROWSET(BULK)
It is a table-valued function that can read data from any file. This function returns a single column table having all the contents of the file. This single large value is known as a SINGLE_CLOB (single character large object)
Syntax:
SELECT * FROM OPENROWSET (BULK 'file_path', SINGLE_CLOB) as correlation_name;
This query will read the content of the text file and return it as a single column in a table named Bulkcolumn. The correlation name is mandatory to specify. We have the text file named "Geek.txt". Content of text file:
Hii!! My name is Romy kumari.
I am an engineer.
Welcome to GeeksforGeeks platform.
Read text file from SQL server
Query:
SELECT * FROM OPENROWSET(BULK 'E:\Geek.txt', SINGLE_CLOB) AS Contents;
Output:
The entire content is returned as a single column.
Read text file from SQL server such that one row represents data in one line from a text file
Step to read each line of the text file in a single row:
- Create a table in your database.
- Insert data from a text file into the table using the 'INSERT' keyword.
- Using WITH clause set ROWTERMINATOR as '\n' (represents newline character). This split the content of the file into separate rows as soon as the new line is encountered in the file.
Step 1: Create Database
Use the below SQL statement to create a database called geeks.
Query:
CREATE DATABASE geeks;
Step 2: Using the Database
Use the below SQL statement to switch the database context to geeks.
Query:
USE geeks;
Step 3: Table definition
We have the following demo table in our geek's database.
CREATE TABLE demo(
Content VARCHAR(1000)
);
Step 4: Insert data from a text file into the table
Use the following command to insert data from a text file.
Syntax:
BULK INSERT dbo.table_name
FROM 'file_path'
WITH
(
ROWTERMINATOR ='\n'
)
Query:
BULK INSERT dbo.demo
FROM 'E:\Geek.txt'
WITH
(
ROWTERMINATOR ='\n'
);
Step 5: See the content of the table
Query:
SELECT * FROM demo;
Output:
Here, NULL represents an empty line.
Similar Reads
Read Fixed Width Text File in R In this article, we are going to see how to read fixed-width text files in R Programming language. In text files, columns will have fixed widths, specified in characters, which determines the maximum amount of data it can contain.  No delimiters are used to separate the fields in the file.  Instead
3 min read
How to Export SQL Server Data to a Text File Format? Exporting SQL Server data to a text file is a common task that is used in data migration, data sharing, etc. SQL Server provides several methods to export data to a text file format, including using the SQL Server Management Studio (SSMS), the SQL Server Command Line Tool (sqlcmd), and the SQL Serve
4 min read
What is a Text File? A file is basically a container that contains data. A text file is a very important and simplest kind of file. Text file is used to store data in textual format. What is a Text File?A text file is a file that contains data in the form of text. This is used to store and share textual data and is usef
4 min read
Read text File with Space as Delimiter in R In this article, we will discuss how to read a text file with spaces as delimiters in R programming language. Base R allows us to read and access the content within the text files with any number of characters as the delimiter. File in use: The read.table() method in R can be used to read data fro
2 min read
Reading rpt files with Pandas In most cases, we usually have a CSV file to load the data from, but there are other formats such as JSON, rpt, TSV, etc. that can be used to store data. Pandas provide us with the utility to load data from them. In this article, we'll see how we can load data from an rpt file with the use of Pandas
2 min read