POSTGRESQL日期函數大全

一:各個數據中的鏈接符函數

SqlServer的鏈接符:+(加號)spa

Sqlite的鏈接符:.(點)code

PostgreSQL的鏈接符:||(或)blog

二:get

今天在PostgreSQL計算日期的時候,翻了一些資料,不多,就記錄下來吧~!it

其中使用到的函數及變量table

to_char()、Date()、now()、current_dateclass

計算兩個日期的差變量

current_date爲今天的日期,爲:2015-06-03date

Date('2015-06-05')-current_date=2

select   to_char(current_date,'yyyy')||'-'||to_char(birthday,'mm-dd'),Date(to_char(current_date,'yyyy')||'-'||to_char(birthday,'mm-dd'))-current_date 

from tablename where delflag=0 and status=0 

and Date(to_char(current_date,'yyyy')||'-'||to_char(birthday,'mm-dd'))-current_date>=0 

and Date(to_char(current_date,'yyyy')||'-'||to_char(birthday,'mm-dd'))-current_date<=7
order by birthday desc

三:

select now() + interval '1 days'; 

select now() + interval '1 month'; 

select now() + interval '1 years'; 
四:
SqlServer中進行日期計算時,用到函數
CONVERT(),DATEPART(),getDate()

select CONVERT(varchar(10),CONVERT(varchar(4),DATEPART(year,getDate()))+'-'+ CONVERT(varchar(2),DATEPART(month,birthday))+'-'+CONVERT(varchar(2),DATEPART(day,birthday)),120),datediff(day,CONVERT(varchar(10),getdate(),120),CONVERT(varchar(10),CONVERT(varchar(4),DATEPART(year,getDate()))+'-'+ CONVERT(varchar(2),DATEPART(month,birthday))+'-'+CONVERT(varchar(2),DATEPART(day,birthday)),120)) 

from tablename where delflag=0 and status=0 

and datediff(day,CONVERT(varchar(10),getdate(),120),CONVERT(varchar(10),CONVERT(varchar(4),DATEPART(year,getDate()))+'-'+ CONVERT(varchar(2),DATEPART(month,birthday))+'-'+CONVERT(varchar(2),DATEPART(day,birthday)),120))<=7 

and datediff(day,CONVERT(varchar(10),getdate(),120),CONVERT(varchar(10),CONVERT(varchar(4),DATEPART(year,getDate()))+'-'+ CONVERT(varchar(2),DATEPART(month,birthday))+'-'+CONVERT(varchar(2),DATEPART(day,birthday)),120))>=0 order by birthday desc
相關文章
相關標籤/搜索