Home > Database > Mysql Tutorial > body text

Can LOAD DATA INFILE Convert YYYYMMDD Dates to YYYY-MM-DD During Import?

Mary-Kate Olsen
Release: 2024-11-17 13:11:02
Original
1002 people have browsed it

Can LOAD DATA INFILE Convert YYYYMMDD Dates to YYYY-MM-DD During Import?

Can LOAD DATA INFILE Easily Convert YYYYMMDD to YYYY-MM-DD?

Importing data with dates in the YYYYMMDD format can be a challenge. However, you can easily convert these dates to the more standard YYYY-MM-DD format using the LOAD DATA INFILE command in MySQL.

Solution:

The following LOAD DATA INFILE statement combines the data loading and date conversion into a single step:

LOAD DATA INFILE 'file.txt'
INTO TABLE t1
FIELDS TERMINATED BY ','
(column1, @var1, column3, ...)
SET column2 = STR_TO_DATE(@var1,'%Y%m%d')
Copy after login

In this command:

  • The file.txt parameter specifies the input data file.
  • The t1 table is the target for the imported data.
  • The fields within the data file are separated by commas.
  • The @var1 placeholder represents the YYYYMMDD date field.
  • The STR_TO_DATE function converts the date in @var1 from %Y%m%d format to %Y-%m-%d format, storing the result in column2 of the target table.

By using this command, you can import and convert data in one go, eliminating the need for additional scripts or data transformations.

The above is the detailed content of Can LOAD DATA INFILE Convert YYYYMMDD Dates to YYYY-MM-DD During Import?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template