DB2開發常用

2021-12-30 01:07:59 字數 2814 閱讀 2723

db2開發常用-----日期函式(常用日期函式)

獲取時間

1、獲取當期日期:

values current date;

2012-08-28

2、獲取當期時間

values current time;

11:56:36

3、獲取當前時間戳

values current timestamp;

2012-08-28 11:57:32

4、year()

獲取當前年份

values year(current timestamp);

2012

5、month()

獲取當前月份

values month(current timestamp); 8

6、day()

獲取當前日

values day(current timestamp); 28

7、 hour()

獲取當前時

values hour(current timestamp); 12

8、minute()

獲取當前分

values minute(current timestamp); 3

9、second()

獲取秒values second(current timestamp); 48

10、microsecond()

獲取毫秒

values microsecond(current timestamp);

59000

11、timestamp轉varchar

values varchar_format(current timestamp,'yyyy-mm-dd hh24-mm-ss');

2012-08-28 12-08-21

12、timestamp中抽取date

values date(current timestamp);

2012-08-28

13、timestamp中抽取time

values time(current timestamp);

12:14:51

14、星期相關

dayname()返回日期引數中的星期幾,返回值型別:字串;例如:星期一

values dayname(current timestamp);

tuesday

dayofweek()返回日期引數中的星期幾,返回值型別:整數;例如:1;其中1代表星期日

values dayofweek(current timestamp); 3 

----今天是tuesday

dayofweek_iso()返回日期引數中的星期幾,返回值型別:整數;例如:1;其中1代表星期一

values dayofweek_iso(current timestamp); 2

----今天是tuesday

week()返回日期引數中所在年的第幾周,返回範圍在(1-54)的整數,以星期日作為一周的開始

values week(timestamp('2012-1-8')); 2

week()返回日期引數中所在年的第幾周,返回範圍在(1-53)的整數,以星期一作為一周的開始

values week_iso(timestamp('2012-1-8')); 1

15、時間與字串之間轉換

varchar_format(,』yyyy-mm-dd』)返回值:字串  

values varchar_format(current timestamp,'yyyy-mm-dd hh24-mm-ss');

2012-08-28 12-08-37

to_char()  

values to_char(current timestamp);

aug 28, 2012 12:37:33 pm

char()  

values char(current timestamp);

2012-08-28-12.38.10.387000

values char(time('22:24:23'));

22.24.23

字串轉日期或時間  

date()  

values date('2012-1-1');

2012-01-01

time()  

values time('22.22.22');

22:22:22

timestamp()  

values timestamp('2012-1-1-22.42.23.000890');

2012-01-01 22:42:23

16、時間計算  

values current date+1 year+2 months+4 days;

2013-11-01

-----2012-08-28

17、時間差計算

timestampdiff()

前提條件:1、不考慮閏年;2、假設每個月只有30天

1 = 秒的小數部分 

2 = 秒 

4 = 分 

8 = 時 

16 = 天 

32 = 周 

64 = 月 

128 = 季度 

256 = 年

timestampdiff(2,char(current timestamp - timestamp(task.create_)))

精確計算()返回值:整數

(days() - days()) * 86400 + 

(midnight_seconds() - midnight_seconds())

`

DB2開發常用日期函式

db2 開發常用日期函式 獲取時間 1 獲取當期日期 values current date 2012 08 28 www.2cto.com 2 獲取當期時間 values current time 11 56 36 3 獲取當前時間戳 values current timestamp 2012 0...

DB2常用函式

1 char函式 char current date,iso 轉換成yyyy mm dd char current date,usa 轉換成mm dd yyyy char current date,eur 轉換成dd.mm.yyyy char current date,jis char curren...

常用DB2命令

建庫 db2 create db vipface using codeset utf 8 territory cn on 建庫到指定位置 db2 create database oadb on d using codeset gbk territory cn 列出所有資料庫 db2 list dat...