how to get number of months between 2 dates sql server

Solutions on MaxInterview for how to get number of months between 2 dates sql server by the best coders in the world

showing results for - "how to get number of months between 2 dates sql server"
Emilio
18 Feb 2017
1CREATE FUNCTION FullMonthsSeparation 
2(
3    @DateA DATETIME,
4    @DateB DATETIME
5)
6RETURNS INT
7AS
8BEGIN
9    DECLARE @Result INT
10
11    DECLARE @DateX DATETIME
12    DECLARE @DateY DATETIME
13
14    IF(@DateA < @DateB)
15    BEGIN
16        SET @DateX = @DateA
17        SET @DateY = @DateB
18    END
19    ELSE
20    BEGIN
21        SET @DateX = @DateB
22        SET @DateY = @DateA
23    END
24
25    SET @Result = (
26                    SELECT 
27                    CASE 
28                        WHEN DATEPART(DAY, @DateX) > DATEPART(DAY, @DateY)
29                        THEN DATEDIFF(MONTH, @DateX, @DateY) - 1
30                        ELSE DATEDIFF(MONTH, @DateX, @DateY)
31                    END
32                    )
33
34    RETURN @Result
35END
36GO
37
38SELECT dbo.FullMonthsSeparation('2009-04-16', '2009-05-15') as MonthSep -- =0
39SELECT dbo.FullMonthsSeparation('2009-04-16', '2009-05-16') as MonthSep -- =1
40SELECT dbo.FullMonthsSeparation('2009-04-16', '2009-06-16') as MonthSep -- =2
queries leading to this page
how to get dates which are gereater than past two months in sqlsql query to find no of months between 2 datescalculate months between two dates sql serverbetween month sql queryhow to get number of months betweeen two dates sqlhow to get difference between two dates in years 2c months and days in sql serversql server check for data 3 months since nowsql get number of years between two datescount months between dates sqlmssql date between two dates for 6 monthssql calculate number of sales for each months between two datesget number of months since the data sqlsql select months between datesget all the month between the given 2 dates in sql serversql datediff 6 monthssql select select get number of months between two datesdifference between two months in sqlsql server month two digitscheck months between two dates sql serversql server get number of days between two dateshow to get days between to months in sqlsql server get records for each months between two dates for loopcalculate months between 2 dates sqldifference of months between two dates sql from different yearsmonths between two dates in sqlsql select months between two datesmonths between two dates sqlsql server get month total days sql select counts of months between two datesdate between months sqlhow can i get the month difference between two dates sql servernumber of months between two dates sql querycount months between current date and other dates sqlhow to calculate date in years and months in sqlbetween months in sqlsql query for getting months name and number between two datesget month difference from two dates in sql serverget number of years between two dates sqlsql query for getting months and years name and number between two datesmonths between sqlmonths between 2 dates sqlhow to get months between two dates in sqlsql server get months of n yearshow to get difference between two dates in years months and days in sql serversql count number of months between two datesget all months and year between two dates sqlcounting months between two dates in sqlsql select get number of months between two datessql date between two dates is 12 months differenceif months between two dates sql then give a sequence of months with 0 and 1how to work out months from two dates sqlcalculate number of months in sqldate diff to get month and days in sql serversql select all months between two datesno of months between two dates sqlmonths between two dates in sql servercalculate number of months between two dates sqlget number of months between two dates sql serversql count month between two datessql server get no of months between two dates more than 15 dayssql query to find number of weeks between two datesmonths between in sqlsql function returns number of months between two dates sql date difference in years months and dayshow to count number of months between two dates in sql server querysql find months between datesnumber of months between two datessql months betweennumber of months between two dates in sqlsearch between two dates in sqlfind value within 1 month and 2 months of date sqlhow to count months between two dates in sqlget month number in sql server in 2 digit monthget month days in between two dates in sql serverhow to find months between two dates in sqlsql get months between two datescount months in a time period between two dates sqlhow to get one month records between two dates in that month in sql servermonths between in sql querysql server get no of months between two dateshow to get correct differnce between months sqlcount months in a time period between two dates sql servercalculate number of months between two dates in sqlsql get all months between two datessql server select get number of months between two dateshow to get months between two dates coulmn in sqlhow to get number of months between 2 dates sql server