SQL Server的日期計算

2021-08-31 21:58:12 字數 1916 閱讀 8683

[size=large]a. 乙個月的第一天 

select dateadd(mm, datediff(mm,0,getdate()), 0)

b. 本週的星期一

select dateadd(wk, datediff(wk,0,getdate()), 0)

c. 一年的第一天

select dateadd(yy, datediff(yy,0,getdate()), 0)

d. 季度的第一天

select dateadd(qq, datediff(qq,0,getdate()), 0)

e. 上個月的最後一天

select dateadd(ms,-3,dateadd(mm, datediff(mm,0,getdate()), 0))

f. 去年的最後一天

select dateadd(ms,-3,dateadd(yy, datediff(yy,0,getdate()), 0))

g. 本月的最後一天

select dateadd(ms,-3,dateadd(mm, datediff(m,0,getdate())+1, 0))

h. 本月的第乙個星期一

select dateadd(wk, datediff(wk,0,

dateadd(dd,6-datepart(day,getdate()),getdate())

), 0)

i. 本年的最後一天

select dateadd(ms,-3,dateadd(yy, datediff(yy,0,getdate())+1, 0))。

獲取表結構[把 'sysobjects' 替換 成 'tablename' 即可]

select case isnull(i.name, '')

when '' then ''

else '*'

end as ispk,

object_name(a.id) as t_name,

a.name as c_name,

isnull(substring(m.text, 1, 254), '') as pbc_init,

t.name as f_datatype,

case isnull(typeproperty(t.name, 'scale'), '')

when '' then cast(a.prec as varchar)

else cast(a.prec as varchar) + ',' + cast(a.scale as varchar)

end as f_scale,

a.isnullable as f_isnullable

from syscolumns as a

join systypes as t

on (a.xtype = t.xusertype and a.id = object_id('sysobjects') )

left join ( sysindexes as i

join syscolumns as a1

on ( i.id = a1.id and a1.id = object_id('sysobjects') and (i.status & 0x800) = 0x800 and a1.colid <= i.keycnt) )

on ( a.id = i.id and a.name = index_col('sysobjects', i.indid, a1.colid) )

left join syscomments as m

on ( m.id = a.cdefault and objectproperty(a.cdefault, 'isconstraint') = 1 )

order by a.colid asc [/size]

SQL Server的日期計算

a.乙個月的第一天 select dateadd mm,datediff mm,0,getdate 0 b.本週的星期一 select dateadd wk,datediff wk,0,getdate 0 c.一年的第一天 select dateadd yy,datediff yy,0,getdat...

SQL Server 的日期函式

23 代號結果0 01 2 2021 12 26pm 12001 2 21 221.01.02 32002 1 21 402.01.21 52002 1 21 602 01 21 701 02,21 812 26 01 901 2 2021 12 26 01 220pm 102001 2 21 11...

SQL Server 根據出生日期計算年齡

getdate 函式用於返回當前的日期和時間 datediff 函式返回兩個日期之間的時間。語法 datediff datepart,startdate,enddate startdate 和 enddate 引數是合法的日期表示式。datepart 引數可以是下列的值 datepart縮寫年 yy...