SQL必備-ORACLE-SQSLSERVER-DB2時間函數及常見函數總結

SQLSERVER 時間篇:*************************************************************
一、時間函數
 --getdate 獲取當前時間
 select getdate()

 --dateadd 原有時間加: 2013-02-17 13:20:16 此時間加12個月
 select dateadd(MONTH,12,'2013-02-17 13:20:16')  --返回:2014-02-17 13:20:16.000  (參數month可以改爲 day,year等日期加相應的值)

 --datediff 兩個時間的差 (後面-前面=返回值)
 select datediff(day,'2013-02-01','2013-02-18')   --返回:17  (參數day可以改爲 month,year等日期加相應的值)
 
 --datepart 獲取日期的某個部分整數
 select DATEPART(month, '2013-2-17')  --返回 2    (參數month可以改爲 day,year等日期加相應的值)
 
 --datename 獲取指定部位的字符串
 select datename(weekday, '2013-2-17')  --返回 星期日 (參數weekday可以改爲 day,year等日期加相應的值)

 --day(), month(),year() 獲取指定部位的字符串
 select day('2013-2-15') --返回15

二、時間格式轉換

    select CONVERT(varchar, getdate(), 120 )  --返回 2013-02-17 13:37:54
    select replace(replace(replace(CONVERT(varchar, getdate(), 120 ),'-',''),' ',''),':','') --返回 20130217133828
    select CONVERT(varchar(12) , getdate(), 111 )  --返回 2013/02/17
    select CONVERT(varchar(12) , getdate(), 112 )  --返回 20130217
    select CONVERT(varchar(12) , getdate(), 102 )  --返回 2013.02.17
    select CONVERT(varchar(12) , getdate(), 101 )  --返回 02/17/2013
    select CONVERT(varchar(12) , getdate(), 103 )  --返回 17/02/2013
    select CONVERT(varchar(12) , getdate(), 104 )  --返回 17.02.2013
    select CONVERT(varchar(12) , getdate(), 105 )  --返回 17-02-2013
    select CONVERT(varchar(12) , getdate(), 106 )  --返回 17 02 2013
    select CONVERT(varchar(12) , getdate(), 107 )  --返回 02 17, 2013
    select CONVERT(varchar(12) , getdate(), 108 )  --返回 13:42:50
    select CONVERT(varchar(12) , getdate(), 109 )  --返回 02 17 2013  
    select CONVERT(varchar(12) , getdate(), 110 )  --返回 02-17-2013
    select CONVERT(varchar(12) , getdate(), 113 )  --返回 17 02 2013 1
    select CONVERT(varchar(12) , getdate(), 114 )  --返回 13:42:24:743

三、時間格式與其他格式轉換
   
   將int轉換爲時間:convert(datetime,left(DATEid,8),101)
   將時間轉換爲int: year(CreateDate)*10000 + month(CreateDate)*100 + day(CreateDate)
   將時間格式[2014-07-24 10:32:43.197]轉換爲int20140724:select cast(replace(convert(char(10),GETDATE(),120),'-','') as int)

SQLSERVER 函數篇: *************************************************************

一、字符函數
ascii(字符串表達式)
char(整數表達式)
charindex(字符串表達式 1, 字符串表達式2[,整數表達式])
difference(字符串表達式 1,字符串表達式 2)
left(字符串表達式,整數表達式)
right(字符串表達式,整數表達式)
len(字符串表達式)
lower(字符串表達式)
upper(字符串表達式)
ltrim(字符串表達式)
rtrim(字符串表達式)
patindex(字符串表達式 1,字符串表達式 2)
reverse(字符串表達式)
space(整數表達式)
str(float 型小數[,總長度[,小數點後保留的位數]])
stuff (字符串表達式 1,開始位置,長度,字符串表達式 2)
substring(字符串表達式,開始位置,長度)
replace(字符串表達式 1,字符串表達式 2,字符串表達式 3)
二、數學函數
abs(數值表達式)
cos(浮點表達式)
sin(浮點表達式)
cot(浮點表達式)
ceiling(數值表達式)
floor(數值表達式)
rand([整數表達式])
round(數值表達式[,長度[,操作方式]])
三、日期函數
dateadd(日期部分,數字,日期)
datediff(日期部分,開始日期,結束日期)
datename(日期部分,日期)
datepart(日期部分,日期)
day(日期) month (日期) year (日期)
四、轉換函數
convert(數據類型[(長度)],表達式[,樣式])
cast(表達式 as 數據類型[(長度)])
getdate()
五、系統和功能函數
select newid()
isnumeric(任意表達式)
isdate(任意表達式)  
----------------------------------------------------------------------------
ORACLE 時間篇:*************************************************************

一、當前時間拆分:
select to_char(sysdate,'yyyy') as nowYear   from dual;   //獲取時間的年  
select to_char(sysdate,'mm')    as nowMonth from dual;   //獲取時間的月  
select to_char(sysdate,'dd')    as nowDay    from dual;   //獲取時間的日
select to_char(sysdate,'day') from dual;  
select to_char(sysdate,'hh24') as nowHour   from dual;   //獲取時間的時  
select to_char(sysdate,'mi')    as nowMinute from dual;   //獲取時間的分  
select to_char(sysdate,'ss')    as nowSecond from dual;   //獲取時間的秒
select to_date('2004-05-07 13:23:44','yyyy-mm-dd hh24:mi:ss')    from dual

二、當前時間自身計算:
當前時間減去 7 分鐘的時間
select sysdate,sysdate - interval '7' MINUTE from dual
當前時間減去 7 小時的時間
select sysdate - interval '7' hour from dual
當前時間減去 7 天的時間
select sysdate - interval '7' day from dual
當前時間減去 7 月的時間
select sysdate,sysdate - interval '7' month from dual
當前時間減去 7 年的時間
select sysdate,sysdate - interval '7' year from dual  

三、當前時間其他計算:
START_DATE,END_DATE,計算這兩個日期的時間差(分別以天,小時,分鐘,秒,毫秒):
天:   ROUND(TO_NUMBER(sysdate - START_DATE))
小時: ROUND(TO_NUMBER(sysdate - START_DATE) * 24)
分鐘: ROUND(TO_NUMBER(sysdate - START_DATE) * 24 * 60)
秒:   ROUND(TO_NUMBER(sysdate - START_DATE) * 24 * 60 * 60)
毫秒: ROUND(TO_NUMBER(sysdate - START_DATE) * 24 * 60 * 60 * 1000)

四、當前時間轉換

時間轉換爲char:
select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') as nowTime from dual;    
char轉換爲時間:
select to_date('2003-10-17 21:15:37','yyyy-mm-dd hh24:mi:ss') from dual


ORACLE 函數篇: *************************************************************

一、字符函數
lower(char):將字符串轉化爲小寫的格式。
upper(char):將字符串轉化爲大寫的格式。
length(char):返回字符串的長度。
substr(char, m, n):截取字符串的子串,n代表取n個字符的意思,不是代表取到第n個
replace(char1, search_string, replace_string)
instr(C1,C2,I,J) -->判斷某字符或字符串是否存在,存在返回出現的位置的索引,否則返回小於1;在一個字符串中搜索指定的字符,返回發現指定的字符的位置;
C1 被搜索的字符串
C2 希望搜索的字符串
I 搜索的開始位置,默認爲1
J 出現的位置,默認爲1

二、數學函數
round(n,[m]) 該函數用於執行四捨五入
trunc(n,[m]) 該函數用於截取數字。
mod(m,n)取餘函數
floor(n) 返回小於或是等於n的最大整數
ceil(n) 返回大於或是等於n的最小整數
abs(n) 返回數字n的絕對值

三、日期函數
sysdate 返回系統時間
add_months函數 可以得到某一時間之前或之後n個月的時間
last_day(d) 返回指定日期所在月份的最後一天

四、轉換函數
to_date()函數
to_char()函數

五、系統和功能函數
1)terminal:當前會話客戶所對應的終端的標示符,如計算機名
2)language: 語言
3)db_name: 當前數據庫名稱
4)nls_date_format: 當前會話客戶所對應的日期格式
5)session_user: 當前會話客戶所對應的數據庫用戶名
6)current_schema: 當前會話客戶所對應的默認方案名
7)host: 返回數據庫所在主機的名稱



Oracle trunc()函數的用法
/**************日期********************/
1.select trunc(sysdate) from dual  --2011-3-18  今天的日期爲2011-3-18
2.select trunc(sysdate, 'mm')   from   dual  --2011-3-1    返回當月第一天.
3.select trunc(sysdate,'yy') from dual  --2011-1-1       返回當年第一天
4.select trunc(sysdate,'dd') from dual  --2011-3-18    返回當前年月日
5.select trunc(sysdate,'yyyy') from dual  --2011-1-1   返回當年第一天
6.select trunc(sysdate,'d') from dual  --2011-3-13 (星期天)返回當前星期的第一天
7.select trunc(sysdate, 'hh') from dual   --2011-3-18 14:00:00   當前時間爲14:41  
8.select trunc(sysdate, 'mi') from dual  --2011-3-18 14:41:00   TRUNC()函數沒有秒的精確
/***************數字********************/
/*
TRUNC(number,num_digits)
Number 需要截尾取整的數字。
Num_digits 用於指定取整精度的數字。Num_digits 的默認值爲 0。
TRUNC()函數截取時不進行四捨五入
*/
9.select trunc(123.458) from dual --123
10.select trunc(123.458,0) from dual --123
11.select trunc(123.458,1) from dual --123.4
12.select trunc(123.458,-1) from dual --120
13.select trunc(123.458,-4) from dual --0
14.select trunc(123.458,4) from dual  --123.458
15.select trunc(123) from dual  --123
16.select trunc(123,1) from dual --123
17.select trunc(123,-1) from dual --120
-------------------------------------------------

DB2:

 --獲取當前日期: 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;   
 --要使當前時間或當前時間戳記調整到 GMT/CUT,則把當前的時間或時間戳記減去當前時區寄存器:
         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); 07/24/14
  values VARCHAR_FORMAT(current TIMESTAMP,'yyyy-mm-dd');  
  values char(current date);  2014-07-24-10.46.51.978540
  values time(current timestamp);  10:47:25
--執行日期和時間的計算
  values current date+1 year;  
  values current date+3 years+2 months +15 days;
  values current time +5 hours -3 minutes +10 seconds;   
--計算兩個日期之間的天數   
  values days(current date)- days(date('2010-02-20'));    
--時間和日期換成字符串   
  values char(current date);   
  values char(current time);
--要將字符串轉換成日期或時間值   
  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');  
  --計算兩個時間戳記之間的時差:   
  --秒的小數部分爲單位   
  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')));

  將時間轉換爲int:CAST(TO_CHAR(IN_DATE,'YYYYMMDD') AS INT) AS DATEID
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章