計算每週第一天和最後一天

2021-10-04 23:08:14 字數 3448 閱讀 1057

計算每週第一天和最後一天

select

day,dayofweek(

day)

as dw1

,date_add(

day,

1- dayofweek(

day)

)as su_s -- 週日_start

,date_add(

day,

7- dayofweek(

day)

)as sa_e -- 週六_end

,case

when dayofweek(

day)=1

then

7else dayofweek(

day)-1

endas dw2

,date_add(

day,1-

case

when dayofweek(

day)=1

then

7else dayofweek(

day)-1

end)

as mo_s -- 周一_start

,date_add(

day,7-

case

when dayofweek(

day)=1

then

7else dayofweek(

day)-1

end)

as su_e -- 週日_end

,to_date(concat(

year

(day),

'-',lpad(ceil(

month

(day)/

3)*3

-2,2

,0),

'-01'))

as season_first_day -- 當季第一天

,trunc(

day,

'mm'

)as month_first_day -- 當月第一天

,trunc(

day,

'yy'

)as year_first_day -- 當年第一天

,last_day(add_months(trunc(

day,

'yy'),

12))as year_last_day -- 當年最後一天

,last_day(

day)

as month_last_day -- 當月最後一天

,last_day(to_date(concat(

year

(day),

'-',lpad(ceil(

month

(day)/

3)*3

,2,0

),'-01'))

)as season_last_day -- 當季最後一天

,weekofyear(

day)

as weekofyear1 -- 當年第幾周

,weekofyear(

'2019-08-07'

)- weekofyear(trunc(

'2019-08-07'

,'mm'))

+1as weekofmonth1 --當月第幾周

,second

(day)as

second

-- 秒鐘

,minute

(day)as

minute

-- 分鐘

,hour

(day)as

hour

-- 小時

,day

(day)as

day-- 日期

,month

(day)as

month

-- 月份

,lpad(ceil(

month

(day)/

3),2

,0)as season -- 季度

,year

(day)as

year

-- 年份

from

(select

'2020-04-01'

asday

union

allselect

'2020-04-02'

asday

union

allselect

'2020-04-03'

asday

union

allselect

'2020-04-04'

asday

union

allselect

'2020-04-05'

asday

union

allselect

'2020-04-06'

asday

union

allselect

'2020-04-07'

asday

union

allselect

'2020-04-08'

asday

union

allselect

'2020-04-09'

asday

union

allselect

'2020-04-10'

asday

union

allselect

'2020-04-11'

asday

union

allselect

'2020-04-12'

asday

union

allselect

'2020-04-13'

asday

union

allselect

'2020-04-14'

asday

union

allselect

'2020-04-15'

asday

union

allselect

'2020-04-16'

asday

union

allselect

'2020-04-17'

asday

union

allselect

'2020-04-18'

asday

union

allselect

'2020-04-19'

asday

union

allselect

'2020-04-20'

asday

union

allselect

'2020-04-21'

asday

union

allselect

'2020-04-22'

asday

) t1

SQL獲取第一天和最後一天

sql獲取第一天和最後一天 declare dt datetime set dt getdate declare number int set number 3 1 指定日期該年的第一天或最後一天 a.年的第一天 select convert char 5 dt,120 1 1 b.年的最後一天 s...

第一天和最後一天的日期

1.某星期第一天和最後一天的日期 dateformat sd new dateformat yyyy mm dd calendar calendar calendar.getinstance locale.china calendar.setfirstdayofweek calendar.monda...

Oracle之第一天和最後一天

取當前日期前乙個月的第一天的日期 select last day add months sysdate,2 1 from dual 取當前日期前乙個月的最後一天的日期 select last day add months sysdate,1 from dual 本月的第一天和最後一天 select ...