1、日期函數(shù)
1)時(shí)間戳:unix_timestamp/from_unixtime
select unix_timestamp(); --查詢當(dāng)前時(shí)間的時(shí)間戳,返回 1486524284select unix_timestamp('2017-02-08 11:15:50'); --查詢指定時(shí)間的時(shí)間戳,返回 1486523750 (若轉(zhuǎn)換失敗返回0)select unix_timestamp('20170208 11:00:00','yyyyMMdd HH:mm:ss'); --轉(zhuǎn)換指定格式時(shí)間的時(shí)間戳select from_unixtime(1486523750); --查詢指定時(shí)間戳的時(shí)間,默認(rèn)格式y(tǒng)yyy-MM-dd HH:mm:ss ,返回 2017-02-08 11:15:50select from_unixtime(1486523750,'yyyyMMdd'); --查詢指定時(shí)間戳的時(shí)間,轉(zhuǎn)換成指定格式,返回 20170208
2)日期提取年月日時(shí)分秒+周:year/month/day/hour/minute/second/weekofyear
select year('2017-02-08 11:15:50'),month('2017-02-08 11:15:50'),day('2017-02-08 11:15:50'),hour('2017-02-08 11:15:50'),minute('2017-02-08 11:15:50'),second('2017-02-08 11:15:50'),weekofyear('2017-02-08 11:15:50'); --查詢指定日期的年月日時(shí)分秒和周,返回 2017、2、8、11、15、50、6
3)日期時(shí)間轉(zhuǎn)日期:to_date
select to_date('2017-02-08 11:15:50'); --查詢指定日期時(shí)間中的日期。返回 2017-02-08
4)日期比較
--日期比較,結(jié)束時(shí)間距離開始時(shí)間的天數(shù)。datediff(string enddate, string startdate)select datediff('2017-02-02','2017-01-31');--返回 2--日期增加和減少。date_sub (string startdate, int days)select date_add('2017-01-31',2); --返回 2017-02-02select date_sub('2017-02-02',2); --返回 2017-01-31
5)日期偏移
--自定義日期操作函數(shù)(返回帶橫線的日期):get_dateselect get_date();--返回當(dāng)前日期,返回 2017-02-09select get_date(-2);--返回當(dāng)前日期往前偏移2天的日期 ,返回 2017-02-07--自定義日期操作函數(shù)(返回不帶橫線的日期):get_dt_dateselect get_dt_date();--獲取當(dāng)前日期,返回 20170209select get_dt_date(get_date(-2));--獲取當(dāng)前日期偏移,轉(zhuǎn)為不帶橫桿的格式select get_dt_date('2017-02-02',-2);--20170131
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注