473,378 Members | 1,527 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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.

Apr 16 '07 #1
5 10385
<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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Vincent Jones | last post by:
I already have a calendar from VBScript but I need it to bold on the dates when there is an event. I've created the Database connection but I can't seem to figure out how to integrate the two. ...
5
by: JP SIngh | last post by:
Hi All I have a tricky question wonder if anyone can help. I store dates in my table as FDate TDate NoDays Where Fdate is From Date
3
by: Matt | last post by:
Hello, I have a query that I would like to schedule in DTS. The criteria of this query checks for records in the table that are within the current quarter. Here is what I have. WHERE...
9
by: Rich | last post by:
Thanks for the Help in my previous post. I've been working on this and it's almost what I want. I want to obtain the user's current age by comparing their date of birth (user inputs) to the...
2
by: toedipper | last post by:
Hello, MYsql and PHP If I want to extract data with todays date then it's 'where blab blah = current_date()' For yesterday it's 'where blah blah = current_date()-1' But can anyone tell...
3
by: David | last post by:
I'm new to DB2 and I need to write a query that will allow me to find specific dates instead of me having a date range asked for, I want it to be calculated. I've done this in Access by coding...
3
by: SJH | last post by:
I currently have reports (mostly graphs) that utilize dates entered into a form. The dates are pretty baisc from the standpoint of the start and end of the current fiscal year, the start and end...
1
by: Likhith Areekkal | last post by:
Hi, Language: C#, asp .net application This is a room reservations web application. I have two Calendars on my website: Calendar1 & Calendar2 Calendar1 shows the current month and Calendar2...
9
by: rinmanb70 | last post by:
I have a table of transactions, some with past dates, some dated the current date, and some dated in the near future. On a report, I'm looking for a way to get four different sums using the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.