Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in SQL by (20.3k points)
edited by

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 a user-defined function?

1 Answer

0 votes
by (40.7k points)

Try using the below code for the first day of the specified month:

datediff(day, @date, dateadd(month, 1, @date))

Code given below will work for every date:

datediff(day, dateadd(day, 1-day(@date), @date),

              dateadd(month, 1, dateadd(day, 1-day(@date), @date)))

Related questions

0 votes
2 answers
0 votes
3 answers
0 votes
2 answers
0 votes
2 answers
asked Jul 30, 2019 in SQL by Tech4ever (20.3k points)

1.2k questions

2.7k answers

501 comments

693 users

Browse Categories

...