我正在寫一個函數,它聲明一個變量@StartDate是一月份的第一個,但是一年是運行腳本的當年(當年),這意味着年份可以改變,一個月不能。日期參數sql
任何人都知道如何做到這一點?
示例代碼:
IF @DATE_TYPE = '第一季度'
BEGIN
SET @START_DATE = 01/01/'當前年份'
SET @END_DATE = 31/03/'當年'
END
我正在寫一個函數,它聲明一個變量@StartDate是一月份的第一個,但是一年是運行腳本的當年(當年),這意味着年份可以改變,一個月不能。日期參數sql
任何人都知道如何做到這一點?
示例代碼:
IF @DATE_TYPE = '第一季度'
BEGIN
SET @START_DATE = 01/01/'當前年份'
SET @END_DATE = 31/03/'當年'
END
對於SQL 2012及以上版本,
SELECT DATEFROMPARTS (YEAR(GETDATE()), 1, 1)
SELECT DATEFROMPARTS (YEAR(GETDATE()), 03, 31)
對於所有版本,
SELECT CAST('01 JAN '+CAST(DATEPART(YY,GETDATE()) AS NVARCHAR) AS DATETIME)
SELECT CAST('31 MAR '+CAST(DATEPART(YY,GETDATE()) AS NVARCHAR) AS DATETIME)
我想你可以d Ø這樣的事情(這是更好,當你確定日期,而不是隱式轉換使用convert()):
SET @START_DATE = CONVERT(datetime, CAST(YEAR(GETDATE()) AS VARCHAR(4)) + '0101', 112)
SET @END_DATE = CONVERT(datetime, CAST(YEAR(GETDATE()) AS VARCHAR(4)) + '0331', 112)
在SQL 2012+,你可以使用Datefromparts
IF @DATE_TYPE = 'FIRST QUARTER'
BEGIN
SET @START_DATE = datefromparts(year(getdate()), 1,1)
SET @END_DATE = datefromparts(year(getdate()), 3,31)
END
如果你想獲得最前一頁日期和季度的最後日期,然後如果你使用的SQLSERVER 2012使用它
DECLARE @CurrentDate datetime = '2017-07-01' -- or getdate() or any date
SELECT datefromparts(year(@CurrentDate),
datepart(quarter, @CurrentDate) * 3 - 2,1) AS StartDate,
EOMONTH(datefromparts(year(@CurrentDate),
datepart(quarter, @CurrentDate) * 3 , 1)) AS EndDate
,您可以使用DATEFROMPARTS
DECLARE @START_DATE DATE,
@END_DATE DATE
SELECT @START_DATE = DATEFROMPARTS(YEAR(GETDATE()), 1, 1) ,
@END_DATE = DATEFROMPARTS(YEAR(GETDATE()), 03, 31)
SELECT @START_DATE [start_date] ,
@END_DATE [end_date]
結果:
start_date end_date
---------- ----------
2017-01-01 2017-03-31
(1 row(s) affected)
DECLARE @START_DATE DATETIME
DECLARE @END_DATE DATETIME
DECLARE @DATE_TYPE VARCHAR(500)
DECLARE @YEAR VARCHAR(4)
SET @DATE_TYPE = 'FIRST QUARTER' -- SAMPLE PARAMETER
SET @YEAR = CAST(YEAR(GETDATE()) AS VARCHAR(4))
DECLARE @tbl TABLE ([QUARTER] VARCHAR(50), StartMonth VARCHAR(2), EndMonth VARCHAR(2))
INSERT INTO @tbl ([QUARTER], StartMonth, EndMonth)
SELECT 'FIRST QUARTER', '1', '3'
UNION ALL
SELECT 'SECOND QUARTER', '4', '6'
UNION ALL
SELECT 'THIRD QUARTER', '7', '9'
UNION ALL
SELECT 'FOURTH QUARTER', '10', '12'
SELECT @START_DATE = StartMonth + '/1/' + @YEAR,
@END_DATE = DATEADD(DD, -1, (DATEADD(mm, 1, EndMonth + '/1/' + @YEAR)))
FROM @tbl
WHERE [QUARTER] = @DATE_TYPE
DECLARE @Day VARCHAR(10) = '1stQUARTER' --Give the quarter name condiations like 1stQUARTER,2ndQUARTER,3rdQUARTER,4thQuarter
IF (@Day = '1stQUARTER')
BEGIN
SELECT CASE
WHEN DATENAME(QUARTER, getdate()) - 1 = 1
THEN DATEADD(qq, DATEDIFF(qq, 0, GETDATE()) - 1, 0)
END AS StartDayQuarter
,CASE
WHEN DATENAME(QUARTER, getdate()) - 1 = 1
THEN DATEADD(dd, - 1, DATEADD(qq, DATEDIFF(qq, 0, GETDATE()), 0))
END AS EndDayQuarter
END
ELSE IF (@Day = '2ndQUARTER')
BEGIN
SELECT CASE
WHEN DATENAME(QUARTER, getdate()) = 2
THEN DATEADD(qq, DATEDIFF(qq, 0, GETDATE()), 0)
END AS FirstDayQuarter
,CASE
WHEN DATENAME(QUARTER, getdate()) = 2
THEN DATEADD(mm, + 3, DATEADD(dd, - 1, DATEADD(qq, DATEDIFF(qq, 0, GETDATE()), 0)))
END AS LasttDayQuarter
END
ELSE IF (@Day = '3rdQUARTER')
BEGIN
SELECT CASE
WHEN DATENAME(QUARTER, getdate()) + 1 = 3
THEN DATEADD(qq, DATEDIFF(qq, 0, GETDATE()) + 1, 0)
END AS FirstDayQuarter
,CASE
WHEN DATENAME(QUARTER, getdate()) + 1 = 3
THEN DATEADD(mm, + 6, DATEADD(dd, - 1, DATEADD(qq, DATEDIFF(qq, 0, GETDATE()), 0)))
END AS LasttDayQuarter
END
ELSE IF (@Day = '4thQuarter')
BEGIN
SELECT CASE
WHEN DATENAME(QUARTER, getdate()) + 2 = 4
THEN DATEADD(qq, DATEDIFF(qq, 0, GETDATE()) + 2, 0)
END AS FirstDayQuarter
,CASE
WHEN DATENAME(QUARTER, getdate()) + 2 = 4
THEN DATEADD(mm, + 9, DATEADD(dd, - 1, DATEADD(qq, DATEDIFF(qq, 0, GETDATE()), 0)))
END AS LasttDayQuarter
END
ELSE
BEGIN
PRINT 'Enter Valid Quarter Name'
END
個
輸出
FirstDayQuarter |LasttDayQuarter
--------------------------------------------
2017-01-01 00:00:00.000 |2017-03-31 00:00:00.000
乾杯這正是我之後,只是我自己的紀錄,究竟是什麼在112到底如何? – farrea
請參閱:https://www.w3schools.com/sql/func_convert.asp – etsa