How to get month (in string) diff in sql server - sql

How can I get month diff as integer based on month names in string (January, February, March, etc.) ?
For example, I have a variable declare #month varchar(20) = 'May';
and Select DATEDIFF(month, month(getdate()), #month) should be 7 (the
difference between October and May).

In SQL Server, you would use datediff():
select datediff(month, '2010-01-15', '2011-02-28')
This counts the number of month boundaries between two dates. That sounds like a reasonable interpretation of your question.

Declare #M1 varchar(25) = 'October'
Declare #M2 varchar(25) = 'May'
Select DateDiff(MONTH,#M1+' 01 1980',#M2+' 01 1980')
+case when DateDiff(MONTH,#M1+' 01 1980',#M2+' 01 1980')<0 then 12 else 0 end
Returns 7

DECLARE #M1 as varchar(20) = 'October';
DECLARE #M2 as varchar(20) = 'May';
SELECT ABS(datediff(mm,
convert(datetime, #M1 + ' 1 2012 11:01AM', 100),
convert(datetime, #M2 + ' 1 2012 11:01AM', 100)
)
);
this doesn't allow for years ,which you didn't mention previously

Assuming input1,input2, there you go:
declare #input1 nvarchar(max)='October'
declare #input2 nvarchar(max)='May'
select
case
when DATEPART(MM,#input1+' 01 2011')>DATEPART(MM,#input2+' 01 2011')
then 12
else 0
end-DATEPART(MM,#input1+' 01 2011')+DATEPART(MM,#input2+' 01 2011')

try this
select DATEPART(MM,'march 01 2011') - DATEPART(MM,'january 01 2011')

Not sure what you are trying to do, but something like this might work:
select MONTH(STR_TO_DATE('October', '%M')) - MONTH(STR_TO_DATE('May', '%M')) -- returns 5

Try this
DECLARE #Date1 VARCHAR(10),#Date2 VARCHAR(10)
SET #Date1='January'
SET #Date2='DECEMBER'
;WITH CTE
AS
(
SELECT Number AS EnglishMOnthNumber,
DATENAME(MONTH,DATEADD(MONTH,NUMBER-DATEPART(MONTH,GETDATE()),GETDATE())) AS EnglishMOnth
FROM master.dbo.spt_values
WHERE NUmber Between 1 and 12
AND type='P'
)
SELECT MAX(CASE WHEN EnglishMOnth=#Date2 THEN EnglishMOnthNumber END)
- MAX( CASE WHEN EnglishMOnth=#Date1 THEN EnglishMOnthNumber END ) AS DiffInMonths
FROM CTE

I would get the difference between the required month (in your case May) and current month, If the difference is less than 0 then I would add the number of months (12) to it to get the calculation else nothing would be added. This is just one more way better answers above
declare #RequiredMonth varchar(25) = 'February';
declare #difference as int;
set #difference = MONTH(cast(#RequiredMonth+'1 2016' as datetime)) - MONTH(getdate())
select case when #difference >= 0 then #difference else #difference + 12 end

You could use something like this :
DECLARE
#month varchar(20) = 'May'
SELECT DATEDIFF(MONTH, GETDATE(), EndDate)
FROM (SELECT CAST(#month + CAST( YEAR(GETDATE()) + 1 AS VARCHAR(4)) AS DATE) EndDate) D

Related

Calculating age in a certain month in SQL Server

I need to extraxt a list of users who turn 15 years old in any day in a certain month (e.g. in June or in July) and I am trying to use string comparison in SQL Server (stored procedure) but it is not working.
I get #Month parameter from a SSRS which is given from a dropdown list of next 10 months. (some of this month will be in the next year).
I have an Age function which converts a date format from 27/07/2003 (BirthDate) to a string 15 years,2 months,27 days. Naturally, There are people aged 9 years, 0 months, 2 days as well.
So far, I could write the code to check if the person will be 15 and at least 1 month (15 years,1 months) in next July (#Month + 1) supposing we want to know if they turn 15 in June (#Month) but still it is not working because of string comparison.
Age(BirthDate, GETDATE()) is the function which turns age in this format:
15 years,2 months,27 days as string.
I hope it is clear what I mean.
declare #Age varchar (20) = 15,
#Month varchar (25) = 'June',
#CurrentMonth varchar (20) = null,
SET #CurrentMonth = DATENAME(month, GETDATE()); /* returns current month in string */
SELECT My_ID
,Title
,FirstName
,LastName
,Gender
,CONVERT(VARCHAR, BirthDate, 103) AS BirthDate
,dbo.Age(BirthDate, GETDATE()) AS Age
,LocalityName AS Locality
,GETDATE() AS ReportDate
,MONTH(GETDATE()) AS MONTH
,YEAR(GETDATE()) AS YEAR
FROM dbo.vw_individuals
WHERE (LEFT(dbo.Age(BirthDate, DATEADD(month, 1 +
(SELECT DATEDIFF(MONTH, #CurrentMonth + ' 01 2010', #Month + ' 01 2010')
+ CASE WHEN DATEDIFF(MONTH, #CurrentMonth + ' 01 2010', #Month + ' 01 2010') < 0 THEN 12 ELSE 0 END)
, GETDATE())), 2) = #Age)
AND (LEFT(dbo.Age(BirthDate, DATEADD(month, 1 +
(SELECT DATEDIFF(MONTH, #CurrentMonth + ' 01 2010', #Month + ' 01 2010')
+ CASE WHEN DATEDIFF(MONTH, #CurrentMonth + ' 01 2010', #Month + ' 01 2010') < 0 THEN 12 ELSE 0 END)
, GETDATE())), 16) < #Age + ' years,2 month')
AND (month(convert(DATETIME, BirthDate, 103)) = (SELECT DATEPART(MM, #Month + '01 2010'))) /* Checks if the BirthDate month is the same as the chosen month (#Month) */
so given the #Month and #Year you're interested in, then the people you need are given BY
SELECT ..... WHERE YEAR(BirthDate) = #YEAR - 15 AND Month(BirthDate) = #Month
so all the people returned have a 15th birthday in #Month/#Year
-- bit of a pain though, might not quite work if a person is born 29th February, they turn 15 in March although that will never be a leap year, so you could get away with
SELECT ..... WHERE YEAR(BirthDate) = #YEAR - 15
AND Month(BirthDate)
+ CASE WHEN MONTH(Birthdate) = 2 AND DAY(Birthdate) = 29 THEN 1 ELSE 0 END
= #Month
Below should show you way to go, your query is much too complicated for what you need (I think), just use logic from my IF in your WHERE
declare #dob datetime = '2003-10-13 17:21:45.620'
if(month(#dob) = month(getdate())
AND year(#dob)+15 = year(getdate()))
print '15 years old this month'
else
print 'not 15 years old this month'
The same yoke, just used table variable:
declare #dob datetime = '2003-10-13 17:21:45.620'
declare #t table (dob datetime)
insert into #t
values ('2003-10-13 17:21:45.620'), ('2004-10-13 17:21:45.620'), ('2003-10-30 17:21:45.620')
select *
from #t
where month(dob) = month(getdate())
and year(dob)+15 = year(getdate())
EDIT
Other way to achieve what you need is to use DATEDIFF
declare #dob datetime = '2003-10-13 17:21:45.620'
declare #t table (dob datetime)
insert into #t
values ('2003-10-13 17:21:45.620'),
('2004-10-13 17:21:45.620'),
('2003-10-30 17:21:45.620')
select *
from #t
where datediff(month, dob, getdate()) = 180 --(15 years * 12months)

Working out a date from a single month number SQL

Got 2 parameters #yr and #period, #period is just the month number so July would equal 7 for example.
In my stored procedure table I've got a column called Date which is just a standard datetime field. I need a where clause to work out all dates greater than the current period minus 1 year so if #period = 7 and #yr = 2012 I want the where clause to return all dates greater than '01-07-2011' (UK date format) how can I achieve this with just the 2 numbers from #period and #yr.
WHERE <br>
Date >= '01-07-2011'
You could
Date >= dateadd(month, #period-1, dateadd(year, #yr-1900, 0))
where year(date)>year(getdate()-1) and month(date)>#period
If you want the expression sargable, convert it to datetime:
declare #year int = 2012
declare #month int = 7
select
...
where [Date] >= convert(datetime, convert(varchar(4), #year)
+ right('0' + convert (varchar(2), #month), 2)
+ '01')
After seeing Alex K.'s answer, you might even do this:
dateadd(month, #month - 1 + (#year-1900) * 12, 0)
For the best performance you should do something like this:
declare #yr int = 2012
declare #period int = 7
select ...
from ....
WHERE date >= dateadd(month, (#yr - 1901) * 12 + #period - 1, 0)
We can do it in may ways
try it
DECLARE #a VARCHAR(20),
#b VARCHAR(10),
#c varchar(4)
SET #b='may' /*pass your stored proc value */
SET #c='2011'
SET #a='01'+#b+#c
SET DATEFORMAT YDM
SELECT CAST(#a AS DATE)
FOR uk formate
SELECT CONVERT(char,CAST(#a AS DATE),103)
Just t make sure you compare against an entire date, one solution I'd offer is:
Select *
from TheTable
where date> DateAdd(Year,-1, convert(datetime, '01/'+convert(varchar,#period)+'/' + convert(varchar,#yr)))
To account for regional format differences in SQL Server 2012:
Select *
from TheTable
where date> DateAdd(Year,-1, DateFromParts(#year,#period,1))
For pre-2012:
Select *
from TheTable
Where Date > DateAdd(day, 0, DateAdd(month, #period-1, DateAdd(year, (#yr-1900)-1,0)))
The #yr-1900 is maintained to illustrate the computation of the base date offset from 1900, then subtracting 1 for the one-year-off date computation

Summing and grouping the number of records in a month

I have the following table which has employees' absence:
RecordId EmpID ActivityCode DateFrom DateTo
---------------------------------------------------------------
666542 1511 AB 29/01/2011 02/02/2011
666986 1511 AB 11/11/2011 11/11/2011
666996 1511 EL 13/11/2011 17/11/2011
755485 1787 SL 01/11/2011 14/11/2011
758545 1787 SL 15/11/2011 03/12/2011
796956 1954 AB 09/11/2011 09/11/2011
799656 1367 AB 09/11/2011 09/11/2011
808845 1527 EL 16/11/2011 16/11/2011
823323 1527 EL 17/11/2011 17/11/2011
823669 1527 EL 18/11/2011 18/11/2011
899555 1123 AB 09/11/2011 09/12/2011
990990 1511 AB 12/11/2011 12/11/2011
Now I want a report generated by a stored proc to sum all the absence days for a specific absence code for each month in a given year, for example If i want to know the totals of absence in 2011 from the previous table I will get something similar to:
Month TotalDays
---------------------------------
JAN 2011 201
FEB 2011 36
MAR 2011 67
APR 2011 91
....
The stored proc will have two params (#Year INT, #AbsCode NVARCHAR(3)).
Please Note, Sometimes a record overlaps another month (like the first row in the example table) and that should be counted separately for each month. I have tried using loops but with no luck. I am so weak in TSQL.
UPDATE
Right now I am using a scalar value user function and a stored procedure to do the job, Its ugly and hard to trace. Here it is any way:
The User function:
ALTER FUNCTION [dbo].[GetActivityTotalDaysInMonth]
(
#ActivityCode CHAR(3)
,#Year INT
,#Month INT
)
RETURNS INT
AS
BEGIN
DECLARE #FirstDayOfMonth DATETIME
DECLARE #LastDayOfMonth DATETIME
SET #FirstDayOfMonth = CAST(CAST(#Year AS varchar) + '-' + CAST(#Month AS varchar) + '-' + CAST(1 AS varchar) AS DATETIME)
SET #LastDayOfMonth = DATEADD(s, -1, DATEADD(M, 1, #FirstDayOfMonth))
DECLARE #TotalDays INT
SELECT #TotalDays =
SUM(DATEDIFF(DAY,
(CASE WHEN ActivityDateFrom < #FirstDayOfMonth THEN #FirstDayOfMonth ELSE ActivityDateFrom END)
, (CASE WHEN ActivityDateTo > #LastDayOfMonth THEN #LastDayOfMonth ELSE ActivityDateTo END))+1)
FROM Activities
WHERE
ActivityCode=#ActivityCode
AND ((ActivityDateFrom < #FirstDayOfMonth AND ActivityDateTo >= #FirstDayOfMonth)
OR (ActivityDateFrom >= #FirstDayOfMonth AND ActivityDateTo <= #LastDayOfMonth)
OR (ActivityDateFrom <= #LastDayOfMonth AND ActivityDateTo > #LastDayOfMonth))
RETURN #TotalDays
END
Now, I call this function inside a loop in a stored procedure:
ALTER PROCEDURE GetAnnualActivityTotalDays
(
#ActivityCode CHAR(3)
,#Year INT
)
AS
BEGIN
SET NOCOUNT ON;
DECLARE #Stats TABLE
([Month] NVARCHAR(50), TotalDays INT)
DECLARE #MonthNo INT
DECLARE #Month DATETIME
SET #MonthNo = 1
WHILE #MonthNo <= 12
BEGIN
SET #Month = CAST(CAST(#Year AS varchar) + '-' + CAST(#MonthNo AS varchar) + '-' + CAST(1 AS varchar) AS DATETIME)
INSERT INTO #Stats ([Month], TotalDays)
SELECT UPPER(SUBSTRING(DATENAME(mm, #Month), 1, 3)) + ', ' + CAST(#Year AS NVARCHAR),
dbo.GetActivityTotalDaysInMonth(#ActivityCode
,#Year
,#MonthNo
,#Base)
SET #MonthNo = #MonthNo + 1
END
SELECT * FROM #Stats
END
As you can see, this is ugly code which I believe it can be done in an easier way.. Any suggestions?
You'd need to create a calendar table which will allow you to easily count the days for each month that the start and end dates encompass. For example recordid = 666542 has 3 days in January and 2 days in February. You'd be able to get that number by a query like
select calyear, calmonth, caldate
from calendar
join activities on calendar.caldate between activities.activitydatefrom and activities.activitydateto
where activitycode = 'AB'
If you wrap that in a common table expression you can perform aggregation queries afterwards on the CTE.
with mycte as (
select calyear, calmonth, caldate
from calendar
join activities on calendar.caldate between activities.activitydatefrom and activities.activitydateto
where activitycode = 'AB'
)
select calyear, calmonth, count(caldate)
from mycte
group by calyear, calmonth
order by calyear, calmonth
To generate the calendar table you can use code similar to
create table calendar (calyear, calmonth, caldate)
declare #numdays int --number of days to generate in the calendar
declare #datestart datetime --the date to begin from in the calendar
set #numdays = 365
set #datestart = 'jan 1 2011';
with num as (
select 0 number
union
select 1 number
union
select 2 number
union
select 3 number
union
select 4 number
union
select 5 number
union
select 6 number
union
select 7 number
union
select 8 number
union
select 9 number
),
numberlist as (
select ((hundreds.number * 100) + (tens.number * 10) + ones.number) n
from num hundreds
cross join num tens
cross join num ones
where ((hundreds.number * 100) + (tens.number * 10) + ones.number) < #numdays
)
insert into calendar (calyear, calmonth, caldate)
select
datepart(yy,dateadd(dd,n,#datestart)) calyear,
datepart(mm,dateadd(dd,n,#datestart)) calmonth,
dateadd(dd,n,#datestart)caldate
from numberlist

How to determine the number of days in a month in SQL Server?

I need to determine the number of days in a month for a given date in SQL Server.
Is there a built-in function? If not, what should I use as the user-defined function?
In SQL Server 2012 you can use EOMONTH (Transact-SQL) to get the last day of the month and then you can use DAY (Transact-SQL) to get the number of days in the month.
DECLARE #ADate DATETIME
SET #ADate = GETDATE()
SELECT DAY(EOMONTH(#ADate)) AS DaysInMonth
You can use the following with the first day of the specified month:
datediff(day, #date, dateadd(month, 1, #date))
To make it work for every date:
datediff(day, dateadd(day, 1-day(#date), #date),
dateadd(month, 1, dateadd(day, 1-day(#date), #date)))
Most elegant solution: works for any #DATE
DAY(DATEADD(DD,-1,DATEADD(MM,DATEDIFF(MM,-1,#DATE),0)))
Throw it in a function or just use it inline. This answers the original question without all the extra junk in the other answers.
examples for dates from other answers:
SELECT DAY(DATEADD(DD,-1,DATEADD(MM,DATEDIFF(MM,-1,'1/31/2009'),0))) Returns 31
SELECT DAY(DATEADD(DD,-1,DATEADD(MM,DATEDIFF(MM,-1,'2404-feb-15'),0))) Returns 29
SELECT DAY(DATEADD(DD,-1,DATEADD(MM,DATEDIFF(MM,-1,'2011-12-22'),0))) Returns 31
--Last Day of Previous Month
SELECT DATEPART(day, DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE()),0)))
--Last Day of Current Month
SELECT DATEPART(day, DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE())+1,0)))
--Last Day of Next Month
SELECT DATEPART(day, DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE())+2,0)))
Personally though, I would make a UDF for it if there is not a built in function...
I would suggest:
SELECT DAY(EOMONTH(GETDATE()))
This code gets you the number of days in current month:
SELECT datediff(dd,getdate(),dateadd(mm,1,getdate())) as datas
Change getdate() to the date you need to count days for.
--- sql server below 2012---
select day( dateadd(day,-1,dateadd(month, 1, convert(date,'2019-03-01'))))
-- this for sql server 2012--
select day(EOMONTH(getdate()))
Solution 1: Find the number of days in whatever month we're currently in
DECLARE #dt datetime
SET #dt = getdate()
SELECT #dt AS [DateTime],
DAY(DATEADD(mm, DATEDIFF(mm, -1, #dt), -1)) AS [Days in Month]
Solution 2: Find the number of days in a given month-year combo
DECLARE #y int, #m int
SET #y = 2012
SET #m = 2
SELECT #y AS [Year],
#m AS [Month],
DATEDIFF(DAY,
DATEADD(DAY, 0, DATEADD(m, ((#y - 1900) * 12) + #m - 1, 0)),
DATEADD(DAY, 0, DATEADD(m, ((#y - 1900) * 12) + #m, 0))
) AS [Days in Month]
You do need to add a function, but it's a simple one. I use this:
CREATE FUNCTION [dbo].[ufn_GetDaysInMonth] ( #pDate DATETIME )
RETURNS INT
AS
BEGIN
SET #pDate = CONVERT(VARCHAR(10), #pDate, 101)
SET #pDate = #pDate - DAY(#pDate) + 1
RETURN DATEDIFF(DD, #pDate, DATEADD(MM, 1, #pDate))
END
GO
SELECT Datediff(day,
(Convert(DateTime,Convert(varchar(2),Month(getdate()))+'/01/'+Convert(varchar(4),Year(getdate())))),
(Convert(DateTime,Convert(varchar(2),Month(getdate())+1)+'/01/'+Convert(varchar(4),Year(getdate()))))) as [No.of Days in a Month]
select datediff(day,
dateadd(day, 0, dateadd(month, ((2013 - 1900) * 12) + 3 - 1, 0)),
dateadd(day, 0, dateadd(month, ((2013 - 1900) * 12) + 3, 0))
)
Nice Simple and does not require creating any functions Work Fine
You need to create a function, but it is for your own convenience. It works perfect and I never encountered any faulty computations using this function.
CREATE FUNCTION [dbo].[get_days](#date datetime)
RETURNS int
AS
BEGIN
SET #date = DATEADD(MONTH, 1, #date)
DECLARE #result int = (select DAY(DATEADD(DAY, -DAY(#date), #date)))
RETURN #result
END
How it works: subtracting the date's day number from the date itself gives you the last day of previous month. So, you need to add one month to the given date, subtract the day number and get the day component of the result.
select add_months(trunc(sysdate,'MM'),1) - trunc(sysdate,'MM') from dual;
I upvoted Mehrdad, but this works as well. :)
CREATE function dbo.IsLeapYear
(
#TestYear int
)
RETURNS bit
AS
BEGIN
declare #Result bit
set #Result =
cast(
case when ((#TestYear % 4 = 0) and (#testYear % 100 != 0)) or (#TestYear % 400 = 0)
then 1
else 0
end
as bit )
return #Result
END
GO
CREATE FUNCTION dbo.GetDaysInMonth
(
#TestDT datetime
)
RETURNS INT
AS
BEGIN
DECLARE #Result int
DECLARE #MonthNo int
Set #MonthNo = datepart(m,#TestDT)
Set #Result =
case #MonthNo
when 1 then 31
when 2 then
case
when dbo.IsLeapYear(datepart(yyyy,#TestDT)) = 0
then 28
else 29
end
when 3 then 31
when 4 then 30
when 5 then 31
when 6 then 30
when 7 then 31
when 8 then 31
when 9 then 30
when 10 then 31
when 11 then 30
when 12 then 31
end
RETURN #Result
END
GO
To Test
declare #testDT datetime;
set #testDT = '2404-feb-15';
select dbo.GetDaysInMonth(#testDT)
here's another one...
Select Day(DateAdd(day, -Day(DateAdd(month, 1, getdate())),
DateAdd(month, 1, getdate())))
I know this question is old but I thought I would share what I'm using.
DECLARE #date date = '2011-12-22'
/* FindFirstDayOfMonth - Find the first date of any month */
-- Replace the day part with -01
DECLARE #firstDayOfMonth date = CAST( CAST(YEAR(#date) AS varchar(4)) + '-' +
CAST(MONTH(#date) AS varchar(2)) + '-01' AS date)
SELECT #firstDayOfMonth
and
DECLARE #date date = '2011-12-22'
/* FindLastDayOfMonth - Find what is the last day of a month - Leap year is handled by DATEADD */
-- Get the first day of next month and remove a day from it using DATEADD
DECLARE #lastDayOfMonth date = CAST( DATEADD(dd, -1, DATEADD(mm, 1, FindFirstDayOfMonth(#date))) AS date)
SELECT #lastDayOfMonth
Those could be combine to create a single function to retrieve the number of days in a month if needed.
SELECT DAY(SUBDATE(ADDDATE(CONCAT(YEAR(NOW()), '-', MONTH(NOW()), '-1'), INTERVAL 1 MONTH), INTERVAL 1 DAY))
Nice 'n' Simple and does not require creating any functions
Mehrdad Afshari reply is most accurate one, apart from usual this answer is based on formal mathematical approach given by Curtis McEnroe in his blog https://cmcenroe.me/2014/12/05/days-in-month-formula.html
DECLARE #date DATE= '2015-02-01'
DECLARE #monthNumber TINYINT
DECLARE #dayCount TINYINT
SET #monthNumber = DATEPART(MONTH,#date )
SET #dayCount = 28 + (#monthNumber + floor(#monthNumber/8)) % 2 + 2 % #monthNumber + 2 * floor(1/#monthNumber)
SELECT #dayCount + CASE WHEN #dayCount = 28 AND DATEPART(YEAR,#date)%4 =0 THEN 1 ELSE 0 END -- leap year adjustment
To get the no. of days in a month we can directly use Day() available in SQL.
Follow the link posted at the end of my answer for SQL Server 2005 / 2008.
The following example and the result are from SQL 2012
alter function dbo.[daysinm]
(
#dates nvarchar(12)
)
returns int
as
begin
Declare #dates2 nvarchar(12)
Declare #days int
begin
select #dates2 = (select DAY(EOMONTH(convert(datetime,#dates,103))))
set #days = convert(int,#dates2)
end
return #days
end
--select dbo.daysinm('08/12/2016')
Result in SQL Server SSMS
(no column name)
1 31
Process:
When EOMONTH is used, whichever the date format we use it is converted into DateTime format of SQL-server. Then the date output of EOMONTH() will be 2016-12-31 having 2016 as Year, 12 as Month and 31 as Days.
This output when passed into Day() it gives you the total days count in the month.
If we want to get the instant result for checking we can directly run the below code,
select DAY(EOMONTH(convert(datetime,'08/12/2016',103)))
or
select DAY(EOMONTH(convert(datetime,getdate(),103)))
for reference to work in SQL Server 2005/2008/2012, please follow the following external link ...
Find No. of Days in a Month in SQL
DECLARE #date DATETIME = GETDATE(); --or '12/1/2018' (month/day/year)
SELECT DAY(EOMONTH ( #date )) AS 'This Month';
SELECT DAY(EOMONTH ( #date, 1 )) AS 'Next Month';
result:
This Month
31
Next Month
30
DECLARE #m int
SET #m = 2
SELECT
#m AS [Month],
DATEDIFF(DAY,
DATEADD(DAY, 0, DATEADD(m, +#m -1, 0)),
DATEADD(DAY, 0, DATEADD(m,+ #m, 0))
) AS [Days in Month]
RETURN day(dateadd(month, 12 * #year + #month - 22800, -1))
select day(dateadd(month, 12 * year(date) + month(date) - 22800, -1))
A cleaner way of implementing this is using the datefromparts function to construct the first day of the month, and calculate the days from there.
CREATE FUNCTION [dbo].[fn_DaysInMonth]
(
#year INT,
#month INT
)
RETURNS INT
AS
BEGIN
IF #month < 1 OR #month > 12 RETURN NULL;
IF #year < 1753 OR #year > 9998 RETURN NULL;
DECLARE #firstDay DATE = datefromparts(#year, #month, 1);
DECLARE #lastDay DATE = dateadd(month, 1, #firstDay);
RETURN datediff(day, #firstDay, #lastDay);
END
GO
Similarily, you can calculate the days in a year:
CREATE FUNCTION [dbo].[fn_DaysInYear]
(
#year INT
)
RETURNS INT
AS
BEGIN
IF #year < 1753 OR #year > 9998 RETURN NULL;
DECLARE #firstDay DATE = datefromparts(#year, 1, 1);
DECLARE #lastDay DATE = dateadd(year, 1, #firstDay);
RETURN datediff(day, #firstDay, #lastDay);
END
GO
use SQL Server EOMONTH Function nested with day to get last day of month
select Day(EOMONTH('2020-02-1')) -- Leap Year returns 29
select Day(EOMONTH('2021-02-1')) -- returns 28
select Day(EOMONTH('2021-03-1')) -- returns 31
For any date
select DateDiff(Day,#date,DateAdd(month,1,#date))
select first_day=dateadd(dd,-1*datepart(dd,getdate())+1,getdate()),
last_day=dateadd(dd,-1*datepart(dd,dateadd(mm,1,getdate())),dateadd(mm,1,getdate())),
no_of_days = 1+datediff(dd,dateadd(dd,-1*datepart(dd,getdate())+1,getdate()),dateadd(dd,-1*datepart(dd,dateadd(mm,1,getdate())),dateadd(mm,1,getdate())))
replace any date with getdate to get the no of months in that particular date
DECLARE #Month INT=2,
#Year INT=1989
DECLARE #date DateTime=null
SET #date=CAST(CAST(#Year AS nvarchar) + '-' + CAST(#Month AS nvarchar) + '-' + '1' AS DATETIME);
DECLARE #noofDays TINYINT
DECLARE #CountForDate TINYINT
SET #noofDays = DATEPART(MONTH,#date )
SET #CountForDate = 28 + (#noofDays + floor(#noofDays/8)) % 2 + 2 % #noofDays + 2 * floor(1/#noofDays)
SET #noofDays= #CountForDate + CASE WHEN #CountForDate = 28 AND DATEPART(YEAR,#date)%4 =0 THEN 1 ELSE 0 END
PRINT #noofDays
DECLARE #date nvarchar(20)
SET #date ='2012-02-09 00:00:00'
SELECT DATEDIFF(day,cast(replace(cast(YEAR(#date) as char)+'-'+cast(MONTH(#date) as char)+'-01',' ','')+' 00:00:00' as datetime),dateadd(month,1,cast(replace(cast(YEAR(#date) as char)+'-'+cast(MONTH(#date) as char)+'-01',' ','')+' 00:00:00' as datetime)))
simple query in SQLServer2012 :
select day(('20-05-1951 22:00:00'))
i tested for many dates and it return always a correct result

Convert Month Number to Month Name Function in SQL

I have months stored in SQL Server as 1,2,3,4,...12. I would like to display them as January,February etc. Is there a function in SQL Server like MonthName(1) = January? I am trying to avoid a CASE statement, if possible.
I think this is the best way to get the month name when you have the month number
Select DateName( month , DateAdd( month , #MonthNumber , 0 ) - 1 )
Or
Select DateName( month , DateAdd( month , #MonthNumber , -1 ) )
A little hacky but should work:
SELECT DATENAME(month, DATEADD(month, #mydate-1, CAST('2008-01-01' AS datetime)))
SELECT DATENAME(month, GETDATE()) AS 'Month Name'
SUBSTRING('JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC ', (#intMonth * 4) - 3, 3)
Use the Best way
Select DateName( month , DateAdd( month , #MonthNumber , -1 ))
It is very simple.
select DATENAME(month, getdate())
output : January
Starting with SQL Server 2012, you can use FORMAT and DATEFROMPARTS to solve this problem. (If you want month names from other cultures, change: en-US)
select FORMAT(DATEFROMPARTS(1900, #month_num, 1), 'MMMM', 'en-US')
If you want a three-letter month:
select FORMAT(DATEFROMPARTS(1900, #month_num, 1), 'MMM', 'en-US')
If you really want to, you can create a function for this:
CREATE FUNCTION fn_month_num_to_name
(
#month_num tinyint
)
RETURNS varchar(20)
AS
BEGIN
RETURN FORMAT(DATEFROMPARTS(1900, #month_num, 1), 'MMMM', 'en-US')
END
You can use the inbuilt CONVERT function
select CONVERT(varchar(3), Date, 100) as Month from MyTable.
This will display first 3 characters of month (JAN,FEB etc..)
in addition to original
SELECT DATENAME(m, str(2) + '/1/2011')
you can do this
SELECT DATENAME(m, str([column_name]) + '/1/2011')
this way you get names for all rows in a table. where [column_name] represents a integer column containing numeric value 1 through 12
2 represents any integer, by contact string i created a date where i can extract the month. '/1/2011' can be any date
if you want to do this with variable
DECLARE #integer int;
SET #integer = 6;
SELECT DATENAME(m, str(#integer) + '/1/2011')
In some locales like Hebrew, there are leap months dependant upon the year so to avoid errors in such locales you might consider the following solution:
SELECT DATENAME(month, STR(YEAR(GETDATE()), 4) + REPLACE(STR(#month, 2), ' ', '0') + '01')
The following works for me:
CAST(GETDATE() AS CHAR(3))
Use this statement to convert Month numeric value to Month name.
SELECT CONVERT(CHAR(3), DATENAME(MONTH, GETDATE()))
Just subtract the current month from today's date, then add back your month number. Then use the datename function to give the full name all in 1 line.
print datename(month,dateadd(month,-month(getdate()) + 9,getdate()))
Sure this will work
select datename(M,GETDATE())
SELECT DateName(M, DateAdd(M, #MONTHNUMBER, -1))
To convert month number to month name, try the below
declare #month smallint = 1
select DateName(mm,DATEADD(mm,#month - 1,0))
You can use the convert functin as below
CONVERT(VARCHAR(3), DATENAME(MM, GETDATE()), 100)
i think this is enough to get month name when u have date.
SELECT DATENAME(month ,GETDATE())
SELECT DATENAME(MONTH,dateadd(month, -3,getdate()))
This one worked for me:
#MetricMonthNumber (some number)
SELECT
(DateName( month , DateAdd( month , #MetricMonthNumber - 1 , '1900-01-01' ) )) AS MetricMonthName
FROM TableName
From a post above from #leoinfo and #Valentino Vranken. Just did a quick select and it works.
Declare #MonthNumber int
SET #MonthNumber=DatePart(Month,GETDATE())
Select DateName( month , DateAdd( month , #MonthNumber , 0 ) - 1 )
Explaination:
First Decalre Variable MonthNumber
Get Current Month for DatePart which Return Month Number
Third Query Return Month Name
select monthname(curdate());
OR
select monthname('2013-12-12');
Working for me
SELECT MONTHNAME(<fieldname>) AS "Month Name" FROM <tablename> WHERE <condition>
you can get the date like this.
eg:- Users table
id name created_at
1 abc 2017-09-16
2 xyz 2017-06-10
you can get the monthname like this
select year(created_at), monthname(created_at) from users;
output
+-----------+-------------------------------+
| year(created_at) | monthname(created_at) |
+-----------+-------------------------------+
| 2017 | september |
| 2017 | june |
You can create a function like this to generate the Month and do
SELECT dbo.fn_GetMonthFromDate(date_column) as Month FROM table_name
/****** Object: UserDefinedFunction [dbo].[fn_GetMonthFromDate] Script Date: 11/16/2018 10:26:33 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [dbo].[fn_GetMonthFromDate]
(#date datetime)
RETURNS varchar(50)
AS
BEGIN
DECLARE #monthPart int
SET #monthPart = MONTH(#date)
IF #monthPart = 1
BEGIN
RETURN 'January'
END
ELSE IF #monthPart = 2
BEGIN
RETURN 'February'
END
ELSE IF #monthPart = 3
BEGIN
RETURN 'March'
END
ELSE IF #monthPart = 4
BEGIN
RETURN 'April'
END
ELSE IF #monthPart = 5
BEGIN
RETURN 'May'
END
ELSE IF #monthPart = 6
BEGIN
RETURN 'June'
END
ELSE IF #monthPart = 7
BEGIN
RETURN 'July'
END
ELSE IF #monthPart = 8
BEGIN
RETURN 'August'
END
ELSE IF #monthPart = 9
BEGIN
RETURN 'September'
END
ELSE IF #monthPart = 10
BEGIN
RETURN 'October'
END
ELSE IF #monthPart = 11
BEGIN
RETURN 'November'
END
ELSE IF #monthPart = 12
BEGIN
RETURN 'December'
END
RETURN NULL END
Here is my solution using some information from others to solve a problem.
datename(month,dateadd(month,datepart(month,Help_HelpMain.Ticket_Closed_Date),-1)) as monthname
There is no system defined function in SQL server. But you can create your own user-defined function- a scalar function. You would find scalar functions in the Object Explorer for your database: Programmability->Functions->Scalar-valued Functions. Below, I use a table variable to bring it all together.
--Create the user-defined function
CREATE FUNCTION getmonth (#num int)
RETURNS varchar(9) --since 'September' is the longest string, length 9
AS
BEGIN
DECLARE #intMonth Table (num int PRIMARY KEY IDENTITY(1,1), month varchar(9))
INSERT INTO #intMonth VALUES ('January'), ('February'), ('March'), ('April'), ('May')
, ('June'), ('July'), ('August') ,('September'), ('October')
, ('November'), ('December')
RETURN (SELECT I.month
FROM #intMonth I
WHERE I.num = #num)
END
GO
--Use the function for various months
SELECT dbo.getmonth(4) AS [Month]
SELECT dbo.getmonth(5) AS [Month]
SELECT dbo.getmonth(6) AS [Month]
to_char(to_date(V_MONTH_NUM,'MM'),'MONTH')
where V_MONTH_NUM is the month number
SELECT to_char(to_date(V_MONTH_NUM,'MM'),'MONTH') from dual;
Use this statement for getting month name:
DECLARE #date datetime
SET #date='2015/1/4 00:00:00'
SELECT CAST(DATENAME(month,#date ) AS CHAR(3))AS 'Month Name'
This will give you short month name. Like this: Jan, Feb, Mar, etc.
Try this: SELECT MONTHNAME(concat('1970-',[Month int val],'-01'))
For example- SELECT MONTHNAME(concat('1970-',4,'-01'))
The answer is - April