Home > Database > Mysql Tutorial > SQL对时间处理的语句小结

SQL对时间处理的语句小结

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-07 18:03:46
Original
956 people have browsed it

做项目时经常要对数据库中存储的时间做一些处理后再操作,于是自己总结了一些

代码如下:
--获取当前时间
Select getdate()
--获取当前年月日 YY代表年,MM代表月,DD代表日,hh代表时,ss代表秒
/*
year yy 1753--9999 quarter qq 1--4 month mm 1--12 day of year dy 1--366

day dd 1--31 week wk 1--53 weekday dw 1--7(Sunday--Saturday)

hour hh 0--23 minute mi 0--59 second ss 0--59 milisecond ms 0--999 */
--DATEPART与DATENAME实现差不多,都能实现同样效果,DATEPART返回的是整数,DATENAME返回的是字符
Select DATEPART(qq,getdate())

--获取前时间的前几天 -d ;后几天 +d
Select getdate()-1

--获得两段时间相距的几年 yy,几月 mm,几日 dd
Select DATEDIFF(dd,'2011-7-7',getdate())

--将字符串类型的日期转为日期类型的
Select CONVERT (DATETIME,'2011-8-6'))

--在当前时间上加上或减去年月日
Select DATEADD(mm,-2,getdate())
Related labels:
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 Issues
sql file
From 1970-01-01 08:00:00
0
0
0
php - Overhead of prepare vs sql?
From 1970-01-01 08:00:00
0
0
0
Print sql statement
From 1970-01-01 08:00:00
0
0
0
Pass array to SQL insert query using PHP
From 1970-01-01 08:00:00
0
0
0
sql optimization or
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template