DB2 日期時間函數(shù)

SQL 日期和時間函數(shù)

標簽(空格分隔): SQL DB2


SQL 日期和時間函數(shù)

函數(shù)名 含義
DAYNAME 返回一個大小寫混合的字符串,對于參數(shù)的日部分,用星期表示這一天的名稱(例如,F(xiàn)riday)。
DAYOFWEEK 返回參數(shù)中的星期幾,用范圍在 1-7 的整數(shù)值表示,其中 1 代表星期日。
DAYOFWEEK_ISO 返回參數(shù)中的星期幾,用范圍在 1-7 的整數(shù)值表示,其中 1 代表星期一。
DAYOFYEAR 返回參數(shù)中一年中的第幾天,用范圍在 1-366 的整數(shù)值表示。
DAYS 返回日期的整數(shù)表示。
JULIAN_DAY 返回從公元前 4712 年 1 月 1 日(儒略日歷的開始日期)到參數(shù)中指定日期值之間的天數(shù),用整數(shù)值表示。
MIDNIGHT_SECONDS 返回午夜和參數(shù)中指定的時間值之間的秒數(shù),用范圍在 0 到 86400 之間的整數(shù)值表示。
*MONTHNAME 對于參數(shù)的月部分的月份,返回一個大小寫混合的字符串(例如,January)。
TIMESTAMP_ISO 根據(jù)日期、時間或時間戳記參數(shù)而返回一個時間戳記值。
TIMESTAMP_FORMAT 從已使用字符模板解釋的字符串返回時間戳記。
TIMESTAMPDIFF 根據(jù)兩個時間戳記之間的時差,返回由第一個參數(shù)定義的類型表示的估計時差。
TO_CHAR 返回已用字符模板進行格式化的時間戳記的字符表示。TO_CHARVARCHAR_FORMAT 的同義詞。
TO_DATE 從已使用字符模板解釋過的字符串返回時間戳記。TO_DATETIMESTAMP_FORMAT 的同義詞。
WEEK 返回參數(shù)中一年的第幾周,用范圍在 1-54 的整數(shù)值表示。以星期日作為一周的開始。
WEEK_ISO 返回參數(shù)中一年的第幾周,用范圍在 1-53 的整數(shù)值表示。

示例

--獲取當前日期:  
select current date from sysibm.sysdummy1;   
values current date;  
 
--獲取當前日期   
select current time from sysibm.sysdummy1;   
values current time;   
--獲取當前時間戳   
select current timestamp from sysibm.sysdummy1;   
values current timestamp;   
 
--要使當前時間或當前時間戳記調(diào)整到 GMT/CUT,則把當前的時間或時間戳記減去當前時區(qū)寄存器:  
values current time -current timezone;   
values current timestamp -current timezone;   
 
--獲取當前年份  
values year(current timestamp);
 
--獲取當前月   
values month(current timestamp);
 
--獲取當前日   
values day(current timestamp);  
 
--獲取當前時   
values hour(current timestamp);  
 
--獲取分鐘   
values minute(current timestamp);  
 
--獲取秒   
values second(current timestamp);  
 
--獲取毫秒   
values microsecond(current timestamp);   
 
--從時間戳記單獨抽取出日期和時間  
values date(current timestamp);   
values VARCHAR_FORMAT(current TIMESTAMP,'yyyy-mm-dd');   
values char(current date);   
values time(current timestamp);   
 
--執(zhí)行日期和時間的計算 
values current date+1 year;      
values current date+3 years+2 months +15 days;   
values current time +5 hours -3 minutes +10 seconds;   
 
--計算兩個日期之間的天數(shù)
values days(current date)- days(date('2010-02-20'));   
 
--時間和日期換成字符串
values char(current date);   
values char(current time);   
 
--要將字符串轉(zhuǎn)換成日期或時間值
values timestamp('2010-03-09-22.43.00.000000');   
values timestamp('2010-03-09 22:44:36');   
values date('2010-03-09');   
values date('03/09/2010');   
values time('22:45:27');   
values time('22.45.27');   

計算兩個時間戳之間的差

--秒的小數(shù)部分為單位   
values timestampdiff(1,char(current timestamp - timestamp('2010-01-01-00.00.00')));   
--秒為單位   
values timestampdiff(2,char(current timestamp - timestamp('2010-01-01-00.00.00')));   
--分為單位   
values timestampdiff(4,char(current timestamp - timestamp('2010-01-01-00.00.00')));   
--小時為單位   
values timestampdiff(8,char(current timestamp - timestamp('2010-01-01-00.00.00')));   
--天為單位   
values timestampdiff(16,char(current timestamp - timestamp('2010-01-01-00.00.00')));   
--周為單位   
values timestampdiff(32,char(current timestamp - timestamp('2010-01-01-00.00.00')));   
--月為單位   
values timestampdiff(64,char(current timestamp - timestamp('2010-01-01-00.00.00')));   
--季度為單位   
values timestampdiff(128,char(current timestamp - timestamp('2010-01-01-00.00.00')));   
--年為單位   
values timestampdiff(256,char(current timestamp - timestamp('2010-01-01-00.00.00')));
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容