By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,759 Members | 1,778 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,759 IT Pros & Developers. It's quick & easy.

get only dates within current month?

P: n/a
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.

Apr 16 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
<so******@yahoo.comwrote in message
news:11*********************@n76g2000hsh.googlegro ups.com...
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)
Apr 16 '07 #2

P: n/a
You might want to try the following:

SELECT *
FROM Events
WHERE DATEDIFF(month, EventDate, GETDATE()) = 0

Note: EventDate is a datetime column

"so******@yahoo.com" wrote:
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.

Apr 16 '07 #3

P: n/a
so******@yahoo.com wrote:
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
Apr 16 '07 #4

P: n/a
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:
soni2...@yahoo.com wrote:
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

Apr 16 '07 #5

P: n/a
so******@yahoo.com wrote:
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.
On Apr 16, 12:24 pm, Göran Andersson <g...@guffa.comwrote:
>soni2...@yahoo.com wrote:
>>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
Apr 16 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.