|
|
 |
| SQL Server ASP Discussions about ASP programming with Microsoft's SQL Server. For more ASP forums, see the ASP forum category. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the SQL Server ASP section of the Wrox p2p Programmer to Programmer discussion community. This is a community of more than 40,000 computer programmers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining our free Wrox p2p community you can post your own programming questions and respond to other programmers’ questions. Registered users also don't have to see the ads that are displayed to guests. Registration is fast, simple and absolutely free so please, join today!
Join today and post to win prizes! Post more to increase your chances of being Wrox’s top poster of the month.
|
 |

December 15th, 2008, 07:07 AM
|
|
Registered User
|
|
Join Date: Dec 2008
Posts: 2
Thanks: 2
Thanked 0 Times in 0 Posts
|
|
how to select from Month
i want select the transaction which are done in the current month alone how to do it..
i think ther are methods to find by date and year but i am not sure how to do it using month, can anybody help me with this?..
|

December 15th, 2008, 09:48 AM
|
|
Wrox Author
Points: 12,827, Level: 49 |
|
|
Join Date: Oct 2005
Location: Akron, Ohio, USA.
Posts: 4,029
Thanks: 1
Thanked 42 Times in 42 Posts
|
|
Something like this would work:
sql Code:
SELECT * FROM [TABLE] WHERE Month(DateColumn) = Month(GetDate())
The obvious problem here is that this query will return all rows where the month of a transaction is equal to the current date's month and does not take into account year. So the above query might possibly return you rows that are several years old.
I typically favor something like this in this scenario:
sql Code:
SELECT * FROM [TABLE] WHERE DateColumn BETWEEN @Date1 AND @Date2
Does this work for you?
__________________
===============================================
Doug Parsons
Wrox online library: Wrox Books 24 x 7
Did someone here help you? Click  on their post!
"Easy is the path to wisdom for those not blinded by themselves."
===============================================
|
|
The Following User Says Thank You to dparsons For This Useful Post:
|
|

December 15th, 2008, 10:33 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,323
Thanks: 3
Thanked 70 Times in 69 Posts
|
|
But there is no reason you can't extend the first answer and do
Code:
SELECT * FROM tablename
WHERE Year(DateColumn) = Year(getDate())
AND Month(DateColumn) = Month(getDate())
If the DateColumn is indexed, using BETWEEN and passing in @Date1 and @Date2 will be somewhat faster. But if the field is not indexed it probably won't make any noticable difference.
*** CAUTION! ***
If DateColumn actually contains both date and time, then doing
Code:
...WHERE DateColumn BETWEEN '1/1/2009' AND '1/31/2009'
will *NOT* give the right answer! You will end up omitting all records from 1/31/2009 except any that happen to have a time of midnight. If you DateColumn does include times, then you are better off doing
Code:
...WHERE DateColumn >= '1/1/2009' AND DateColumn < '2/1/2009'
Notice that we use >= for the start date but only use < for the end, thus ensuring we get all records from 1/31/2009 no matter what time they have.
Last edited by Old Pedant : December 15th, 2008 at 10:38 PM.
|
|
The Following User Says Thank You to Old Pedant For This Useful Post:
|
|

December 16th, 2008, 12:08 AM
|
|
Registered User
|
|
Join Date: Dec 2008
Posts: 2
Thanks: 2
Thanked 0 Times in 0 Posts
|
|
Thanks
this code was perfect for my problem..
|

December 16th, 2008, 09:47 AM
|
|
Wrox Author
Points: 12,827, Level: 49 |
|
|
Join Date: Oct 2005
Location: Akron, Ohio, USA.
Posts: 4,029
Thanks: 1
Thanked 42 Times in 42 Posts
|
|
Quote:
Originally Posted by Old Pedant
*** CAUTION! ***
If DateColumn actually contains both date and time, then doing
Code:
...WHERE DateColumn BETWEEN '1/1/2009' AND '1/31/2009'
will *NOT* give the right answer!
|
The above statement is only relevant to SQL2K8 or another RDBMS such as MySQL since SQL Server's standard way of storing Date and Time data is in a single DateTime field.
-Doug
__________________
===============================================
Doug Parsons
Wrox online library: Wrox Books 24 x 7
Did someone here help you? Click  on their post!
"Easy is the path to wisdom for those not blinded by themselves."
===============================================
|

December 17th, 2008, 10:27 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,323
Thanks: 3
Thanked 70 Times in 69 Posts
|
|
What DB's is it *NOT* true in, Doug? And are you saying there are versions of SQL Server where it is not true?
I don't see how a DB could be ANSI-compliant and not have that situation exist. No matter how the DATETIME value is stored. (Of course, if the *USER* chooses to store the date and time in separate fields, that's a completely different issue/question/solution. But doing that creates its own complexities and I usually recommend against it.)
Anyway, all I wrote is also true in Access (or any JET-accessed DB or pseudo-db, such as Excel or text files).
And of course another way to solve the problem is to convert the date-and-time value to date-only as part of the BETWEEN:
Code:
Access/JET:
... WHERE DATEVALUE(DateColumn) BETWEEN '1/1/2009' AND '1/31/2009'
MySQL:
... WHERE DATE(DateColumn) BETWEEN '1/1/2009' AND '1/31/2009'
SQL Server:
... WHERE CONVERT(DATETIME, CONVERT(VARCHAR, DateColumn, 112), 112) BETWEEN '1/1/2009' AND '1/31/2009'
Only SQL Server is too stupid to give us a nice handy function to do this (and similarly omits other handy datetime functions).
Anyway...You do *NOT* want to use a function like that if the field is indexed, because you just caused a full scan of all values in the index. If you use the >= and < solution that I showed, you can take full advantage of an index on the column.
Last edited by Old Pedant : December 17th, 2008 at 10:34 PM.
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
 |