Author |
Topic |
chahat_mca
Starting Member
1 Post |
Posted - 2006-06-23 : 00:58:57
|
Hello to all i am facing a problem. i want to fetch the no of days in month if i pass the month as interger.pls help me chahat |
|
harshal_in
Aged Yak Warrior
633 Posts |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-06-23 : 02:49:06
|
You will also need to define the year.declare @year int, @month intselect @year = 2006, @month = 6select datediff(day, dateadd(day, 0, dateadd(month, ((@year - 1900) * 12) + @month - 1, 0)), dateadd(day, 0, dateadd(month, ((@year - 1900) * 12) + @month, 0)) ) KH |
|
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2006-06-23 : 10:27:24
|
Easier to just start with a date.declare @dt datetimeset @dt = getdate()select DT = @dt, [Days in Month] =day(dateadd(mm,datediff(mm,-1,@dt),-1)) Results:DT Days in Month ------------------------------------------------------ ------------- 2006-06-23 10:24:23.347 30(1 row(s) affected) CODO ERGO SUM |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-06-23 : 11:05:48
|
quote: Originally posted by Michael Valentine Jones Easier to just start with a date.declare @dt datetimeset @dt = getdate()select DT = @dt, [Days in Month] =day(dateadd(mm,datediff(mm,-1,@dt),-1)) Results:DT Days in Month ------------------------------------------------------ ------------- 2006-06-23 10:24:23.347 30(1 row(s) affected) CODO ERGO SUM
That's just excellent ! KH |
|
|
shah134pk
Starting Member
1 Post |
Posted - 2012-04-24 : 01:13:41
|
This way is very short and best to get DayOfMonth. if u like then plz tell me.DECLARE @SystemDate DateTime, @StartDate DateTime, @EndDate DateTimeSET @SystemDate = '26-Apr-2012'SELECT @StartDate = DATEADD(dd, -Day(@SystemDate) + 1, @SystemDate)SELECT @EndDate = CONVERT(VARCHAR(20), DATEADD(dd, -(DAY(DATEADD(mm, 1, @SystemDate))),DATEADD(mm, 1, @SystemDate)),101)-–SELECT @StartDate StartDate, @EndDate EndDateSELECT DateDiff(WeekDay,@StartDate,@EndDate) + 1 AS DayOfMonthfrom shah… |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2012-04-24 : 06:07:45
|
quote: Originally posted by khtan You will also need to define the year.declare @year int, @month intselect @year = 2006, @month = 6select datediff(day, dateadd(day, 0, dateadd(month, ((@year - 1900) * 12) + @month - 1, 0)), dateadd(day, 0, dateadd(month, ((@year - 1900) * 12) + @month, 0)) ) KH
This is enoughdeclare @year int, @month intselect @year = 2006, @month = 6select day(dateadd(day, 0, dateadd(month, ((@year - 1900) * 12) + @month, 0))-1)MadhivananFailing to plan is Planning to fail |
|
|
|
|
|