Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

date_add 和 adddate #5

Open
astak16 opened this issue Jan 3, 2022 · 0 comments
Open

date_add 和 adddate #5

astak16 opened this issue Jan 3, 2022 · 0 comments

Comments

@astak16
Copy link
Owner

astak16 commented Jan 3, 2022

date_add对指定的日期加上间隔时间,得到一个新的日期

语法: date_add(date, interval num type) ,第一个参数是 date 或者 date-time 都行,第二个参数是有三部分组成的: interval 关键字,间隔时间,间隔单位。

type 取值:

microsecond 毫秒
second
minute 分钟
hour 小时
day
week
month
quarter
year
select date_add('2022-01-03', interval 1 day); // 2022-01-04

如果日期是个无效的日期,返回值是 null

select date_add('2022-01-33', interval 1 day); // null

如果第一个参数只有日期,没有时间,也可以写间隔时间的

select date_add('2022-01-03', inteval 1 minute); // 2022-01-03 00:01:00

间隔时间也可以是负数

select date_add('2022-01-03', interval -1 day); // 2022-01-02

可以接受复合时间,不过时间要用引号,能用的复合时间只有这五种

  • year_month
  • day_hour
  • hour_minute
  • minute_second
  • second_microsecond

其他的复合时间会报错,有两个例外

  • day_minute 结果和 hour_minute 一样。
  • minute_microsecond 结果和 second_microsecond 一样
select date_add('2022-01-03', interval '1-1' day_hour); // 2022-01-04 01:00:00
-----
select date_add('2022-01-03', interval '1-1' day_minute); // 2022-01-03 01:01:00
select date_add('2022-01-03', interval '1-1' hour_minute); // 2022-01-03 01:01:00

复合时间的 - 号要写在最前面

select date_add('2022-01-03', '-1-1', day_hour); // 2022-01-01 23:00:00

adddatedate_add 一样

语法: adddate(date, interval num type) 或者 adddate(date, num)

第二种语法是一种简写,只是在天的基础上增加间隔时间。

select adddate('2022-01-03', 1); // 2022-01-04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant