
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
How can we export data to a CSV file whose filename name contains timestamp at which the file is created?
Sometimes we need to export data into a CSV file whose name has a timestamp at which that file is created. It can be done with the help of MySQL prepared statement. To illustrate it we are using the following example −
Example
The queries in the following example will export the data from table ‘student_info’ to the CSV file having a timestamp in its name.
mysql> SET @time_stamp = DATE_FORMAT(NOW(),'_%Y_%m_%d_%H_%i_%s'); Query OK, 0 rows affected (0.00 sec) mysql> SET @FOLDER = 'C:/mysql/bin/mysql-files'; Query OK, 0 rows affected (0.00 sec) mysql> SET @FOLDER = 'C:/mysql/bin/mysql-files/'; Query OK, 0 rows affected (0.00 sec) mysql> SET @PREFIX = 'Student15'; Query OK, 0 rows affected (0.00 sec) mysql> SET @EXT = '.CSV'; Query OK, 0 rows affected (0.00 sec) mysql> SET @Command = CONCAT("SELECT * FROM Student_info INTO OUTFILE '",@FOLDER, @PREFIX, @time_stamp, @EXT,"' FIELDS ENCLOSED BY '\"' TERMINATED BY ';' ESCAPED BY '\"'"," LINES TERMINATED BY '\r
';"); Query OK, 0 rows affected (0.00 sec) mysql> PREPARE stmt FROM @command; Query OK, 0 rows affected (0.00 sec) Statement prepared mysql> execute stmt; Query OK, 6 rows affected (0.07 sec)
The above query will create CSV file name ‘student_2017_12_10_18_52_46.CSV’ i.e. a CSV file with timestamp value, having the following data −
101;"YashPal";"Amritsar";"History" 105;"Gaurav";"Chandigarh";"Literature" 125;"Raman";"Shimla";"Computers" 130;"Ram";"Jhansi";"Computers" 132;"Shyam";"Chandigarh";"Economics" 133;"Mohan";"Delhi";"Computers"
- Related Articles
- How can we store any other value than N in CSV file if we export the data to CSV file from a table which contains a NULL value(s)?
- How can we export all the data from MySQL table into a CSV file?
- How MySQL evaluates if we export the data to CSV file from a table which contains a NULL value(s)?
- How can we export data to a CSV file along with columns heading as its first line?
- How can we export some field(s) from MySQL table into a CSV file?
- Python Tkinter – How to export data from Entry Fields to a CSV file?
- How can we export all the data from MySQL table into a text file?
- How can we import data from .CSV file into MySQL table?
- How to Export and import CSV file manually in PowerShell?
- How to write data to .csv file in Java?
- How to append data into a CSV file using PowerShell?
- What kind of settings can we do to a CSV file by query while exporting the values from MySQL table into a CSV file?
- Writing data from database to .csv file
- How to read data from *.CSV file using JavaScript?
- How to read data from .csv file in Java?

Advertisements