The MySQL date type and millisecond value are converted to each other.

Source: Internet
Author: User

The MySQL date type and millisecond value are converted to each other.

Sometimes the date type value needs to be converted to the millisecond value, and sometimes the millisecond value needs to be converted to the date, for more convenience, meet the Query Needs.

Create a new database table t_stu_info and insert data to the table.

use test;show tables;create table t_stu_info(     id int not null primary key,     stu_name varchar(20),     stu_date timestamp,     stu_age int(3),     stu_sex varchar(5));    insert into t_stu_info(id,stu_name,stu_date,stu_age,stu_sex) values ('1','zhangsan','2000-12-12 12:30:00','15','man');commit;select * from t_stu_info;



1. convert a date to a millisecond Value

select UNIX_TIMESTAMP(t.stu_date) from t_stu_info t



2. Convert millisecond value to date

SELECT     FROM_UNIXTIME(UNIX_TIMESTAMP(t.stu_date),            '%Y-%m-%d %h:%i:%s') AS stu_dateFROM    t_stu_info t



SELECT     FROM_UNIXTIME(UNIX_TIMESTAMP(t.stu_date),            '%Y-%m-%d') AS stu_dateFROM    t_stu_info t


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.