sql server 獲取上週幾和本週幾

2022-09-06 00:24:22 字數 2469 閱讀 2008

--

sql server 獲取上週幾和本週幾

--@@datefirst 每週以周幾開始,周一是 1 ,週日是 7 ,有些機器預設是7,有些機器預設是1

select

@@datefirst

--修改 @@datefirst 的值

set datefirst 7

--查詢當前每週是以周幾開始,今天是一周的第幾天

select

@@datefirstas'

startday

', datepart(dw,getdate()) as

'weekday'--

通過 weekday 和 @@datefirst 聯合計算,抹平 @@datefirst 設定的差異

--上週

select

convert(date, dateadd(dd, -

datepart (weekday ,getdate() +

@@datefirst

-1) -

6, getdate()) ) as

'上週一',

convert(date, dateadd(dd, -

datepart (weekday ,getdate() +

@@datefirst

-1) -

5, getdate()) ) as

'上周二',

convert(date, dateadd(dd, -

datepart (weekday ,getdate() +

@@datefirst

-1) -

4, getdate()) ) as

'上周三',

convert(date, dateadd(dd, -

datepart (weekday ,getdate() +

@@datefirst

-1) -

3, getdate()) ) as

'上周四',

convert(date, dateadd(dd, -

datepart (weekday ,getdate() +

@@datefirst

-1) -

2, getdate()) ) as

'上周五',

convert(date, dateadd(dd, -

datepart (weekday ,getdate() +

@@datefirst

-1) -

1, getdate()) ) as

'上週六',

convert(date, dateadd(dd, -

datepart (weekday ,getdate() +

@@datefirst

-1) -

0, getdate()) ) as

'上週日'--

本週select

convert(date, dateadd(dd, -

datepart (weekday ,getdate() +

@@datefirst

-1) +

1, getdate()) ) as

'本周一',

convert(date, dateadd(dd, -

datepart (weekday ,getdate() +

@@datefirst

-1) +

2, getdate()) ) as

'本周二',

convert(date, dateadd(dd, -

datepart (weekday ,getdate() +

@@datefirst

-1) +

3, getdate()) ) as

'本週三',

convert(date, dateadd(dd, -

datepart (weekday ,getdate() +

@@datefirst

-1) +

4, getdate()) ) as

'本周四',

convert(date, dateadd(dd, -

datepart (weekday ,getdate() +

@@datefirst

-1) +

5, getdate()) ) as

'本周五',

convert(date, dateadd(dd, -

datepart (weekday ,getdate() +

@@datefirst

-1) +

6, getdate()) ) as

'本週六',

convert(date, dateadd(dd, -

datepart (weekday ,getdate() +

@@datefirst

-1) +

7, getdate()) ) as

'本週日

'

獲取上週時間

當前時間 select getdate 當前時間周的起始日期 以周一為例 select dateadd week,datediff week,0,getdate 0 上週起始 select dateadd week,1,dateadd week,datediff week,0,getdate 0 上...

SQL SERVER 獲取儲存過程返回值的幾種方法

1 不帶任何引數的儲存過程 儲存過程語句中含有return 建立儲存過程 create procedure testreturn asreturn 145go 執行儲存過程 declare rcint exec rc testreturn select rc 說明 查詢結果為145 2 帶輸入引數的...

Oracle 獲取星期幾

方法為 select to char sysdate,day dayth from dual 星期三 可以設定語言 select to char sysdate,day nls date language american dayth from dual wednesday 需要說明 查詢出來的內容...