주차정보 구하기
CREATE FUNCTION [mirae].[fn_GetWeekDayInfo]
(
@P_indate NVARCHAR(10) -- 검색입력날짜
)
/*
SELECT startdate AS WEEK_BEG, enddate AS WEEK_END, weekOfMonth AS WEEK_NUM, weekOfYear AS WEEK_YEAR FROM [mirae].[fn_GetWeekDayInfo]('20211228')
*/
RETURNS @ReturnTable TABLE
(
startdate DATETIME,
enddate DATETIME,
weekOfMonth INT,
weekOfYear INT
)
AS
BEGIN
DECLARE @P_date DATETIME -- 검색일
DECLARE @P_startdate DATETIME -- 시작일
DECLARE @P_enddate DATETIME -- 마감일
DECLARE @P_weekOfMonth INT -- 월주차
DECLARE @P_weekOfYear INT -- 년주차
--SET @P_indate = '20110101'
SET @P_date = CONVERT(DATETIME, @P_indate, 112) -- 입력받은 문자열 날짜변환
-- 검색일자의 해당주에 마지막날 ( 토요일 )
SET @P_enddate = DATEADD(DD, ( 7 - DATEPART(DW, @P_date)), @P_date)
-- 검색일자의 해당주에 시작일
SET @P_startdate = DATEADD(DD, -6, @P_enddate)
-- 주차
SET @P_weekOfMonth = DATEPART(WW, @P_date) - DATEPART(WW, CONVERT(DATETIME, LEFT(@P_indate, 6) + '01', 112)) + 1
--DECLARE @YYYYMMDD AS VARCHAR(8)='20210104'
--SELECT @P_weekOfYear = LEFT(@P_date, 4) AS 년도, DATEPART(WK, CONVERT(DATE,@P_date)) AS 주차
SELECT @P_weekOfYear = DATEPART(WK, CONVERT(DATE,@P_date))
INSERT INTO @ReturnTable
(
startdate, enddate, weekOfMonth, weekOfYear
)
VALUES
(
@P_startdate, @P_enddate, @P_weekOfMonth, @P_weekOfYear
)
RETURN
END