Sybase時間日期函式

2021-04-17 12:56:46 字數 2935 閱讀 6789

日期函式

getdate()

得到當前時間,可以設定得到各種時間格式.

datepart(日期部分,日期)

取指定時間的某乙個部分,年月天時分秒.

datediff(日期部分,日期1,日期2)

計算指定的日期1和日期2的時間差多少.

dateadd(日期部分,數值表示式,日期)

計算指定時間,再加上表示式指定的時間長度.

--取時間的某乙個部分

select datepart(yy,getdate()) --year

select datepart(mm,getdate()) --month

select datepart(dd,getdate()) --day

select datepart(hh,getdate()) --hour

select datepart(mi,getdate()) --min

select datepart(ss,getdate()) --sec

--取星期幾

set datefirst 1

select datepart(weekday,getdate()) --weekday

--字串時間

select getdate() -- '03/11/12'

select convert(char,getdate(),101) -- '09/27/2003'

select convert(char,getdate(),102) -- '2003.11.12'

select convert(char,getdate(),103) -- '27/09/2003'

select convert(char,getdate(),104) -- '27.09.2003'

select convert(char,getdate(),105) -- '27-09-2003'

select convert(char,getdate(),106) -- '27 sep 2003'

select convert(char,getdate(),107) --'sep 27, 2003'

select convert(char,getdate(),108) --'11:16:06'

select convert(char,getdate(),109) --'sep 27 2003 11:16:28:746am'

select convert(char,getdate(),110) --'09-27-2003'

select convert(char,getdate(),111) --'2003/09/27'

select convert(char,getdate(),112) --'20030927'

select rtrim(convert(char,getdate(),102))+' '+(convert(char,getdate(),108)) -- '2003.11.12 11:03:41'

--整數時間

select convert(int,convert(char(10),getdate(),112)) -- 20031112

select datepart(hh,getdate())*10000 + datepart(mi,getdate())*100 + datepart(ss,getdate()) -- 110646

--時間格式 "yyyy.mm.dd hh:mi:ss" 轉換為 "yyyymmddhhmiss"

declare @a datetime,@tmp varchar(20),@tmp1 varchar(20)

select @a=convert(datetime,'2004.08.03 12:12:12')

select @tmp=convert(char(10),@a,112)

select @tmp

select @tmp1=convert(char(10),datepart(hh,@a)*10000 + datepart(mi,@a)*100 + datepart(ss,@a))

select @tmp1

select @tmp=@tmp+@tmp1

select @tmp

--當月最後一天

declare

@tmpstr varchar(10)

@mm int,

@premm int,

@curmmlastday varchar(10)

begin

select @mm=datepart(month,getdate())--當月

select @premm=datepart(month,dateadd(month,-1,getdate())) --上個月

if (@mm>=1 and @mm<=8)

select @tmpstr=convert(char(4),datepart(year,getdate()))+'.0'+convert(char(1),datepart(month,dateadd(month,1,getdate())))+'.'+'01'

else if (@mm>=9 and @mm<=11)

select @tmpstr=convert(char(4),datepart(year,getdate()))+'.'+convert(char(2),datepart(month,dateadd(month,1,getdate())))+'.'+'01'

else

select @tmpstr=convert(char(4),datepart(year,dateadd(year,1,getdate())))+'.0'+convert(char(1),datepart(month,dateadd(month,1,getdate())))+'.'+'01'

select @curmmlastday=convert(char(10),dateadd(day,-1,@tmpstr),102) --當月最後一天

end

Sybase時間日期函式

sybase時間日期函式 sybase日期函式 日期函式 getdate 得到當前時間,可以設定得到各種時間格式.datepart 日期部分,日期 取指定時間的某乙個部分,年月天時分秒.datediff 日期部分,日期1,日期2 計算指定的日期1和日期2的時間差多少.dateadd 日期部分,數值表...

Sybase時間日期函式

sybase時間日期函式 sybase日期函式 日期函式 getdate 得到當前時間,可以設定得到各種時間格式.datepart 日期部分,日期 取指定時間的某乙個部分,年月天時分秒.datediff 日期部分,日期1,日期2 計算指定的日期1和日期2的時間差多少.dateadd 日期部分,數值表...

時間日期函式

獲取 當前日期 curdate 2021 03 02 select curdate 獲取 當前時間 select curtime 獲取日期和時間 2021 03 02 14 47 31 select now 獲取日期所在的週數 第幾周 select week 1998 02 20 獲取日期中的年份 ...