473,395 Members | 1,675 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Get number of days in a month

rsrinivasan
221 100+
Hi all,

Is SQLSERVER have any built in function to get number of days for a given month?

Thanks,
May 22 '08 #1
2 9036
deepuv04
227 Expert 100+
Hi all,

Is SQLSERVER have any built in function to get number of days for a given month?

Thanks,
use the following query to get number of days in a given month
Expand|Select|Wrap|Line Numbers
  1. declare @Date datetime
  2. select @Date = Getdate()
  3.  
  4. select datepart(dd,dateadd(dd,-1,dateadd(mm,1,cast(cast(year(@Date) as varchar)+
  5.         '-'+cast(month(@Date) as varchar)+'-01' as datetime))))
  6.  
my logic is i am taking the year and month of the given date and adding 1 to the month, then append 01 at the end so that we are moving to the 1 st of the next month. from the result i am moving one day back which is last day of the given month. if we take the day part from the final value which is number of days in a given month
May 22 '08 #2
ck9663
2,878 Expert 2GB
Here's another one:

Expand|Select|Wrap|Line Numbers
  1. DECLARE @dtDate DATETIME
  2.     SET @dtDate = getdate()
  3. SELECT DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,@dtDate)+1,0))
  4.  
-- CK
May 22 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: The Biscuit Eater | last post by:
Greetings from a second day newbie to php. I think I have figured out a way to explode a field in a csv file (like 11-08-03) and implode it as 031108 and then compare it to the current date(ymd)...
5
by: Adam Knight | last post by:
I am wanting to create my own calendar asp module. Are there any functions in vbscript that return the number of days in a particular month depending on what year it is ??
1
by: Matt Karp | last post by:
DECLARE @varDate datetime DECLARE @varMonthDate datetime DECLARE @varYear datetime DECLARE @varFOM varchar(10) DECLARE @NumDaysInMonth as int select @varDate = '2/1/2004' select...
4
by: Ronald Celis | last post by:
Hi, is there anyway to get the Number of days in a given month and Year in C# thanks Ronald Celis
29
by: james | last post by:
I have a problem that at first glance seems not that hard to figure out. But, so far, the answer has escaped me. I have an old database file that has the date(s) stored in it as number of days. An...
3
by: LimaCharlie | last post by:
Good Day to Everyone, Need help! I have this SQL table w/ fieldname "PayPeriod". Sample value of PayPeriod are SEP06-1, SEP06-2, OCT06-1, OCT06-2... Wherein the first 3 chars is the month, 06...
5
by: Beemer Biker | last post by:
I cant seem to get that date into any DateTime to make my calculation directly by subtracting "01-01-0000" from "now". After reading this:...
8
xtianixm
by: xtianixm | last post by:
is there sum1 hu can help me in trapping the number of days in a particular month in a year? SAMPLE: FEBRUARY sometimes have 28 days and sometimes have a 29 days
4
chandru8
by: chandru8 | last post by:
hi to all if the current month is jan,i need to calculate the number of days of feb is it possible without assigning number of days manually to a variable
14
by: Tommy Jakobsen | last post by:
Hi. Is there a method in .NET that takes "year" as an argument and returns the total number of weeks in that year? For culture da-DK (Danish). Thanks in advance. Tommy.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.