STR

xiaoxiao2025-06-20  6

语法

STR_TO_DATE(str,format) DATE_FORMAT(date,format)

     STR_TO_DATE 和 DATE_FORMAT 一样都用于格式化时间数据

 

其中格式化数据的格式一般为

 

%Y年,四位%m

月,数值(00-12)

%d月的天,数值(00-31)%h小时 (01-12)%i分钟,数值(00-59)%s秒(00-59)

 

例如我们在添加sql语句上格式化时间,如下

 

CREATE TABLE Customer( First_Name char(50), Last_Name char(50), Address char(50), City char(50), Country char(25), Birth_Date datetime ); insert into Customer (First_Name,Last_Name,Address,City,Country,Birth_Date) VALUES('zs','zs1','上海','闵行区','2',STR_TO_DATE('2018-5-25','%Y-%m-%d')); insert into Customer (First_Name,Last_Name,Address,City,Country,Birth_Date) VALUES ('赵','小军','河北','北京','china',DATE_FORMAT('2018-10-25','%Y-%m-%d')); commit;

那么我们在更新一条数据的时间的时候,如何格式化时间格式呢?

--更新时间 update CS_BASE SET CREATE_TIME = TO_DATE('2018-10-23 16:26:12', 'YYYY-MM-DD HH24:MI:SS') where UIDPK=1838

 

 

在查询语句时,来格式化查询到的时间

 

select DATE_FORMAT(BirthDate01,'%Y/%m/%d %h:%i:%s') from Customer; select STR_TO_DATE(Birth_Date,'%Y-%m-%d') from Customer;

查询结果为

 

转载请注明原文地址: https://www.6miu.com/read-5032189.html

最新回复(0)