sql date時間加減幾天幾小時

2022-03-25 01:27:38 字數 1377 閱讀 1190

//時間轉成年月日時分秒

select date_format(now(),'%y%m%d%h%i%s')

//時間轉成年月日

select date_format(now(),'%y%m%d')

//去年此時

select date_add(now(), interval -1 year)

//上月此時

select date_add(now(), interval -1 month)

//昨天此時

select date_add(now(), interval -1 day)

//7天後

select date_add(now(), interval 7 day)

//一小時前

select date_add(now(), interval -1 hour)

//一分鐘前

select date_add(now(), interval -1 minute)

//一秒鐘前

select date_add(now(), interval -1 second)

//昨天(年月日)

select date_format(date_add(now(), interval 1 day),'%y%m%d')

//上個月第一天和最後一天

select date_sub(date_sub(date_format(now(),'%y%m%d'),interval extract( day from now())-1 day),interval 1 month);

select date_sub(date_sub(date_format(now(),'%y%m%d'),interval extract(day from now()) day),interval 0 month);

//某個字串

select date_format(date_add('20090605123020', interval 20 minute),'%y%m%d')

//第幾周

select weekofyear( now() )

select weekofyear('20090606')

在mysql中,會把'20090707123050'和'20090707'格式的字串作為date型別轉換。

在mysql中,沒有類似oracle的to_char(num,format)函式,所以涉及到數字前面補0的情況需要特殊處理。

如select left(concat('00'),@num),3)就會顯示三位數字的字串, @num=1時顯示001,為123是顯示123。

concat(year(a.createtime),left(concat('0',weekofyear(a.createtime)),2))

sql date時間加減幾天幾小時

時間轉成年月日時分秒 select date format now y m d h i s 時間轉成年月日 select date format now y m d 去年此時 select date add now interval 1 year 上月此時 select date add now i...

mysql加減時間 函式 時間加減

mysql加減時間 函式 時間加減 1.mysql 為日期增加乙個時間間隔 date add set dt now select date add dt,interval 1 day 加1天 select date add dt,interval 1 hour 加1小時 select date ad...

mysql加減時間 函式 時間加減

mysql加減時間 函式 時間加減 1.mysql 為日期增加乙個時間間隔 date add set dt now select date add dt,interval 1 day 加1天 select date add dt,interval 1 hour 加1小時 select date ad...