Answered by:
No Of Days

Question
-
User-1499457942 posted
Hi
I have below code and i want to get no of days in that month . I am using c#
Select @Month = Month from [Period]
Select @Year = Year from [Period]Now i want to create date like this @m_date = Noofdays/07/2018 . If @month = 7 , @Year = 2018 then 31/07/2018
Thanks
Sunday, November 4, 2018 1:58 PM
Answers
-
User475983607 posted
I want in c#As I suspected, the code is elsewhere and not even T-SQL.
The number of days in a month.
int days = DateTime.DaysInMonth(year, month);
https://docs.microsoft.com/en-us/dotnet/api/system.datetime.daysinmonth?view=netframework-4.7.2
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Sunday, November 4, 2018 4:48 PM -
User-1716253493 posted
Hi
I have below code and i want to get no of days in that month . I am using c#
Select @Month = Month from [Period]
Select @Year = Year from [Period]Now i want to create date like this @m_date = Noofdays/07/2018 . If @month = 7 , @Year = 2018 then 31/07/2018
Thanks
Sometime your question is not clear, You provide SQL code but you ask C# code
int m = 2; int y = 2018; DateTime dt = new DateTime(y, m, DateTime.DaysInMonth(y, m));
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, November 5, 2018 3:16 AM
All replies
-
User753101303 posted
Hi,
Which version? You have now https://docs.microsoft.com/en-us/sql/t-sql/functions/eomonth-transact-sql?view=sql-server-2017
If using an older version the simplest approach is likely to start from day 1, add one month and subtract one day (ie the last day of a month is before the first day of the next month).
Edit: for example :
-- SQL Server 2012 DECLARE @y INT=2018,@m INT=7 SELECT EOMONTH(DATEFROMPARTS(@y,@m,1)) -- If EOMONTH DATEFROMPARTS not yet available DECLARE @d DATE SET @d=CAST(CAST(@y AS VARCHAR)+'0101' AS DATE) -- 01/01/YYYY SET @d=DATEADD(month,@m,@d) -- first day of next month SELECT DATEADD(day,-1,@d) -- previous day ie last day of this month
If using an old SQL Server version you could create your own version of the later functions to ease a later upgrade.
Sunday, November 4, 2018 2:13 PM -
User475983607 posted
Hi
I have below code and i want to get no of days in that month . I am using c#
Select @Month = Month from [Period]
Select @Year = Year from [Period]Now i want to create date like this @m_date = Noofdays/07/2018 . If @month = 7 , @Year = 2018 then 31/07/2018
Thanks
This exact subject was covered in your post a few days ago with sample code and links to SQL reference documentation. If you are still having a problem then post the code that you tried and explain the issue you are having. My best guess is you have logical bugs elsewhere in the code.
Sunday, November 4, 2018 3:08 PM -
User-1499457942 posted
Hi PatriceSC
I want in c#
Thanks
Sunday, November 4, 2018 4:04 PM -
User475983607 posted
I want in c#As I suspected, the code is elsewhere and not even T-SQL.
The number of days in a month.
int days = DateTime.DaysInMonth(year, month);
https://docs.microsoft.com/en-us/dotnet/api/system.datetime.daysinmonth?view=netframework-4.7.2
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Sunday, November 4, 2018 4:48 PM -
User-1716253493 posted
Hi
I have below code and i want to get no of days in that month . I am using c#
Select @Month = Month from [Period]
Select @Year = Year from [Period]Now i want to create date like this @m_date = Noofdays/07/2018 . If @month = 7 , @Year = 2018 then 31/07/2018
Thanks
Sometime your question is not clear, You provide SQL code but you ask C# code
int m = 2; int y = 2018; DateTime dt = new DateTime(y, m, DateTime.DaysInMonth(y, m));
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, November 5, 2018 3:16 AM