Je veux obtenir toutes les dates en déclarant le mois et l'année dans SQL Server.
Quelqu'un peut-il s'il vous plaît partager des lignes faciles de code SQL pour l'obtenir.
Par exemple:
DECLARE @month AS INT = 5
DECLARE @Year AS INT = 2016
SELECT * from Something
J'ai essayé ci-dessous des choses,
DECLARE @month TINYINT=5
;WITH CTE_Days AS (
SELECT DATEADD(
MONTH,
@month,
DATEADD(
MONTH,
-MONTH(GETDATE()),
DATEADD(
DAY,
-DAY(GETDATE()) + 1,
CAST(FLOOR(CAST(GETDATE() AS FLOAT)) AS DATETIME)
)
)
) Dates
UNION ALL
SELECT DATEADD(DAY, 1, Dates)
FROM CTE_Days
WHERE Dates < DATEADD(
DAY,
-1,
DATEADD(
MONTH,
1,
DATEADD(
MONTH,
@month,
DATEADD(
MONTH,
-MONTH(GETDATE()),
DATEADD(
DAY,
-DAY(GETDATE()) + 1,
CAST(FLOOR(CAST(GETDATE() AS FLOAT)) AS DATETIME)
)
)
)
)
)
)
SELECT Dates
FROM CTE_Days
Mais je cherche une solution facile avec moins de lignes et une réponse courte
Vous ne pouvez pas obtenir tous les jours en déclarant simplement le mois, vous devez également ajouter l'année en raison des années bissextiles:
DECLARE @date DATE = getdate()
;WITH N(N)AS
(SELECT 1 FROM(VALUES(1),(1),(1),(1),(1),(1))M(N)),
tally(N)AS(SELECT ROW_NUMBER()OVER(ORDER BY N.N)FROM N,N a)
SELECT top(day(EOMONTH(@date)))
N day,
dateadd(d,N-1, @date) date
FROM tally
Une autre solution différente (par t @ clausen):
DECLARE @month AS INT = 5
DECLARE @Year AS INT = 2016
;WITH N(N)AS
(SELECT 1 FROM(VALUES(1),(1),(1),(1),(1),(1))M(N)),
tally(N)AS(SELECT ROW_NUMBER()OVER(ORDER BY N.N)FROM N,N a)
SELECT N day,datefromparts(@year,@month,N) date FROM tally
WHERE N <= day(EOMONTH(datefromparts(@year,@month,1)))
Même approche que t-clausen, mais une plus compacte:
Declare @year int = 2017, @month int = 11;
WITH numbers
as
(
Select 1 as value
UNion ALL
Select value + 1 from numbers
where value + 1 <= Day(EOMONTH(datefromparts(@year,@month,1)))
)
SELECT datefromparts(@year,@month,numbers.value) Datum FROM numbers
Si vous avez une colonne de date/heure, utilisez la fonction month()
:
select t.*
from t
where month(datecol) = 5;
DECLARE @MonthNo TINYINT = 03 -- set your month
,@WOYEAR SMALLINT = 2018; -- set your year
IF OBJECT_ID('TEMPDB..#TMP') IS NOT NULL
DROP TABLE #TMP
DECLARE @START_DATE DATETIME
,@END_DATE DATETIME
,@CUR_DATE DATETIME
SET @START_DATE = DATEADD(month, @MonthNo - 1, DATEADD(year, @WOYEAR - 1900, 0))
SET @END_DATE = DATEADD(day, - 1, DATEADD(month, @MonthNo, DATEADD(year, @WOYEAR - 1900, 0)))
SET @CUR_DATE = @START_DATE
CREATE TABLE #TMP (
WEEKDAY VARCHAR(10)
,DATE INT
,MONTH VARCHAR(10)
,YEAR INT
,dates VARCHAR(25)
)
WHILE @CUR_DATE <= @END_DATE
BEGIN
INSERT INTO #TMP
SELECT DATENAME(DW, @CUR_DATE)
,DATEPART(DAY, @CUR_DATE)
,DATENAME(MONTH, @CUR_DATE)
,DATEPART(YEAR, @CUR_DATE)
,REPLACE(CONVERT(VARCHAR(9), @CUR_DATE, 6), ' ', '-')
SET @CUR_DATE = DATEADD(DD, 1, @CUR_DATE)
END
SELECT *
FROM #TMP
Vous pouvez obtenir toutes les dates d'un mois en utilisant la requête suivante.
declare @month int, @year int
set @month = 2
set @year = 2008
SELECT
CAST(CAST(@year AS VARCHAR) + '-' + CAST(@Month AS VARCHAR) + '-01' AS DATETIME) + Number
FROM master..spt_values
WHERE type = 'P'
AND
(CAST(CAST(@year AS VARCHAR) + '-' + CAST(@Month AS VARCHAR) + '-01' AS DATETIME) + Number )
<
DATEADD(mm,1,CAST(CAST(@year AS VARCHAR) + '-' + CAST(@Month AS VARCHAR) + '-01' AS DATETIME) )
J'espère que ceci vous aidera.
DECLARE @Today DATE= GETDATE() ,
@StartOfMonth DATE ,
@EndOfMonth DATE;
DECLARE @DateList TABLE ( DateLabel VARCHAR(10) );
SET @EndOfMonth = EOMONTH(GETDATE());
SET @StartOfMonth = DATEFROMPARTS(YEAR(@Today), MONTH(@Today), 1);
WHILE @StartOfMonth <= @EndOfMonth
BEGIN
INSERT INTO @DateList
VALUES ( @StartOfMonth );
SET @StartOfMonth = DATEADD(DAY, 1, @StartOfMonth);
END;
SELECT DateLabel
FROM @DateList;
WHERE Dates LIKE '2018-12%'
Dans un horodatage ou une date/heure, il passe Année-Mois, ainsi, tout ce qui correspond à 2018 sera extrait. Vous pouvez modifier ceci pour utiliser également vos variables.
@month = 12;
@year = 2018;
@searchQuery = @year + @month + '%';
WHERE Dates LIKE @searchQuery
Une autre version basée sur CTE
DECLARE @Month INT = 2, @Year INT = 2019
;WITH MonthDays_CTE(DayNum)
AS
(
SELECT DATEFROMPARTS(@Year, @Month, 1) AS DayNum
UNION ALL
SELECT DATEADD(DAY, 1, DayNum)
FROM MonthDays_CTE
WHERE DayNum < EOMONTH(DATEFROMPARTS(@Year, @Month, 1))
)
SELECT *
FROM MonthDays_CTE