get only dates within current month?

  • Thread starter Thread starter soni2926
  • Start date Start date
S

soni2926

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.
 
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)
 
You might want to try the following:

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

Note: EventDate is a datetime column
 
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.
 
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?
 
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top