dbTalk Databases Forums  

selecting records based on date

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss selecting records based on date in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Eugene Anthony
 
Posts: n/a

Default selecting records based on date - 03-15-2007 , 12:44 PM






I have a table that has a DateTime column which uses a DataTime
datatype. How do I retrieve a range of records based on the month and
year using ms sql?

Eugene Anthony

*** Sent via Developersdex http://www.developersdex.com ***

Reply With Quote
  #2  
Old   
Ed Murphy
 
Posts: n/a

Default Re: selecting records based on date - 03-15-2007 , 01:27 PM






Eugene Anthony wrote:

Quote:
I have a table that has a DateTime column which uses a DataTime
datatype. How do I retrieve a range of records based on the month and
year using ms sql?
Google is your friend.

Results 1 - 10 of about 12,100 for
"microsoft sql server" "date functions". (0.27 seconds)


Reply With Quote
  #3  
Old   
Eugene Anthony
 
Posts: n/a

Default Re: selecting records based on date - 03-15-2007 , 08:16 PM



This works.

SELECT DATEPART(mm, Dates) AS month,DATEPART(yy, Dates) AS year FROM
testing

Eugene Anthony

*** Sent via Developersdex http://www.developersdex.com ***

Reply With Quote
  #4  
Old   
Utahduck@hotmail.com
 
Posts: n/a

Default Re: selecting records based on date - 03-16-2007 , 08:36 AM



On Mar 15, 12:44 pm, Eugene Anthony <solomon_13... (AT) yahoo (DOT) com> wrote:
Quote:
I have a table that has a DateTime column which uses a DataTime
datatype. How do I retrieve a range of records based on the month and
year using ms sql?

Eugene Anthony

*** Sent via Developersdexhttp://www.developersdex.com***
I've always hated this one. Searching for a date like that is pretty
simple but looking for a range can often times return unwanted
results. Though I don't know if it is right or not, I've done the
following before in the past:

SELECT *
FROM table
WHERE CONVERT(char(2), DatePart(yy, table.datefield)) +
CONVERT(char(2), DatePart(mm, table.datefield)) >= CONVERT(char(2),
DatePart(yy, BeginDate)) + CONVERT(char(2), DatePart(mm, BeginDate))
AND CONVERT(char(2), DatePart(yy, table.datefield)) + CONVERT(char(2),
DatePart(mm, table.datefield)) <= CONVERT(char(2), DatePart(yy,
EndDate)) + CONVERT(char(2), DatePart(mm, EndDate))

This way, if begin date is 1/1/07 and end date is today it will
evaluate between 0701 and 0703. The problem with the above is that
since you can't index it it needs to do a full table scan and if it is
a large table this can sometimes take some time. If that is the case
and you run this often you might want to add a computed field that
carries the converted date over and index that sucker.

I hope that helps.

Utah



Reply With Quote
  #5  
Old   
Jack Vamvas
 
Posts: n/a

Default Re: selecting records based on date - 03-18-2007 , 02:50 AM



Something like : SELECT myCol1 FROM myTable WHERE DATEPART(mm, Dates) =
'<insert month>',DATEPART(yy, Dates) = '<insert year>'



--

Jack Vamvas
___________________________________
Advertise your IT vacancies for free at - http://www.ITjobfeed.com



"Eugene Anthony" <solomon_13000 (AT) yahoo (DOT) com> wrote

Quote:
I have a table that has a DateTime column which uses a DataTime
datatype. How do I retrieve a range of records based on the month and
year using ms sql?

Eugene Anthony

*** Sent via Developersdex http://www.developersdex.com ***



Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.