get only dates within current month? | | |
Hi,
I have a web application, asp.net and c# done in 2.0, which is going
to return rows from the db with dates or certain events. The db is
going to have events dates for the entire year, but on the site we
just want those events for the current month. Is there a way to check
this, check as each date is returned if it falls under the current
month, year?
Thanks. | | | | re: get only dates within current month?
<soni2926@yahoo.comwrote in message
news:1176737299.216250.63580@n76g2000hsh.googlegro ups.com... Quote:
I have a web application, asp.net and c# done in 2.0, which is going
to return rows from the db with dates or certain events. The db is
going to have events dates for the entire year, but on the site we
just want those events for the current month. Is there a way to check
this, check as each date is returned if it falls under the current
month, year?
You don't mention which RDBMS you're using, so I'm going to assume it's SQL
Server...
DECLARE @dtmStartOfMonth smalldatetime
SET @dtmStartOfMonth = (SELECT CAST(CAST(YEAR(getdate()) AS char) + '-' +
CAST(MONTH(getdate()) AS char) + '-01' AS smalldatetime))
SELECT *
FROM MyTable
WHERE DateField >= @dtmStartOfMonth
AND DateField < DATEADD(m, 1, @dtmStartOfMonth) | | | | re: get only dates within current month?
You might want to try the following:
SELECT *
FROM Events
WHERE DATEDIFF(month, EventDate, GETDATE()) = 0
Note: EventDate is a datetime column
"soni2926@yahoo.com" wrote: Quote:
Hi,
I have a web application, asp.net and c# done in 2.0, which is going
to return rows from the db with dates or certain events. The db is
going to have events dates for the entire year, but on the site we
just want those events for the current month. Is there a way to check
this, check as each date is returned if it falls under the current
month, year?
>
Thanks.
>
>
| | | | re: get only dates within current month? soni2926@yahoo.com wrote: Quote:
Hi,
I have a web application, asp.net and c# done in 2.0, which is going
to return rows from the db with dates or certain events. The db is
going to have events dates for the entire year, but on the site we
just want those events for the current month. Is there a way to check
this, check as each date is returned if it falls under the current
month, year?
>
Thanks.
>
Determine the first day of the current month and the first day of the next:
DateTime today = DateTime.Today;
DateTime monthStart = new DateTime(today.Year, today.Month, 1);
DateTime monthEnd = monthStart.AddMonth(1);
Now use these to add a condition to the database query so that you get
the records where the dates are >= monthStart and < monthEnd.
--
Göran Andersson
_____ http://www.guffa.com | | | | re: get only dates within current month?
Thanks that helps! it's actually something i need to do on the c#
side, not the sql as we're getting the data from a web service, is
there anyway to make this format into a datetime:
20-May-07
that's the format we're getting the dates in, i thought about parsing
it, separating it at each - to get the day-month name-year, but wanted
to know if there's a better way?
On Apr 16, 12:24 pm, Göran Andersson <g...@guffa.comwrote: Quote:
soni2...@yahoo.com wrote: Quote:
Hi,
I have a web application, asp.net and c# done in 2.0, which is going
to return rows from the db with dates or certain events. The db is
going to have events dates for the entire year, but on the site we
just want those events for the current month. Is there a way to check
this, check as each date is returned if it falls under the current
month, year?
> >
Determine the first day of the current month and the first day of the next:
>
DateTime today = DateTime.Today;
DateTime monthStart = new DateTime(today.Year, today.Month, 1);
DateTime monthEnd = monthStart.AddMonth(1);
>
Now use these to add a condition to the database query so that you get
the records where the dates are >= monthStart and < monthEnd.
>
--
Göran Andersson
_____http://www.guffa.com
| | | | re: get only dates within current month? soni2926@yahoo.com wrote: Quote:
Thanks that helps! it's actually something i need to do on the c#
side, not the sql as we're getting the data from a web service, is
there anyway to make this format into a datetime:
20-May-07
that's the format we're getting the dates in, i thought about parsing
it, separating it at each - to get the day-month name-year, but wanted
to know if there's a better way?
>
Use the DateTime.ParseExact method with a format string of "dd-MMM-yy",
"dd-MMMM-yy", "d-MMM-yy" or "d-MMMM-yy", depending on if you get
zero-padded days or not, and abbreviated or full month names. Quote:
On Apr 16, 12:24 pm, Göran Andersson <g...@guffa.comwrote: Quote:
>soni2...@yahoo.com wrote: Quote:
>>Hi,
>>I have a web application, asp.net and c# done in 2.0, which is going
>>to return rows from the db with dates or certain events. The db is
>>going to have events dates for the entire year, but on the site we
>>just want those events for the current month. Is there a way to check
>>this, check as each date is returned if it falls under the current
>>month, year?
>>Thanks.
>Determine the first day of the current month and the first day of the next:
>>
>DateTime today = DateTime.Today;
>DateTime monthStart = new DateTime(today.Year, today.Month, 1);
>DateTime monthEnd = monthStart.AddMonth(1);
>>
>Now use these to add a condition to the database query so that you get
>the records where the dates are >= monthStart and < monthEnd.
>>
>--
>Göran Andersson
>_____http://www.guffa.com
>
>
--
Göran Andersson
_____ http://www.guffa.com |  | Similar C# / C Sharp bytes | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,327 network members.
|