Connecting Tech Pros Worldwide Help | Site Map

get only dates within current month?

soni2926@yahoo.com
Guest
 
Posts: n/a
#1: Apr 16 '07
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.

Mark Rae
Guest
 
Posts: n/a
#2: Apr 16 '07

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)


=?Utf-8?B?TmFlZW0=?=
Guest
 
Posts: n/a
#3: Apr 16 '07

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.
>
>
=?ISO-8859-1?Q?G=F6ran_Andersson?=
Guest
 
Posts: n/a
#4: Apr 16 '07

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
soni2926@yahoo.com
Guest
 
Posts: n/a
#5: Apr 16 '07

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?
>
Quote:
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

=?ISO-8859-1?Q?G=F6ran_Andersson?=
Guest
 
Posts: n/a
#6: Apr 16 '07

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
Closed Thread