Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
gmunky
Starting Member
5 Posts |
Posted - 2007-03-27 : 10:59:00
|
| I'm having problem writing a query that retrieves the dates(mm-dd-yy) that matches both the month and the year that a user inputs.for instance:date table01-31-0502-23-0501-22-0503-25-05I want to be able to retrieve these dates:01-31-0501-23-05i tried using LIKE approach:select from table where date like '%05'but this query retrieves all the '05 dates. is there a way to use LIKE for both the month and the year?Thanks for any help! |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-03-27 : 11:01:13
|
| select * from [date] where datepart(month, col1) = 5 and datepart(year, col1) = 7Peter LarssonHelsingborg, Sweden |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-03-27 : 11:01:37
|
| Do not use VARCHAR to store dates.Peter LarssonHelsingborg, Sweden |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-03-27 : 11:05:58
|
quote: Originally posted by gmunky I'm having problem writing a query that retrieves the dates(mm-dd-yy) that matches both the month and the year that a user inputs.
Peter LarssonHelsingborg, Sweden |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-03-27 : 12:03:20
|
| I see now. I just put 7 for year to not confuse with 5 as I used for month, forgot about 2007!The record with date 20070513 will be fetched when using select * from [date] where datepart(month, col1) = 5 and datepart(year, col1) = 2007But since OP intends to only use the last two digitsselect * from [date] where datepart(month, col1) = 5 and datepart(year, col1) % 100 = 7Thanks!Peter LarssonHelsingborg, Sweden |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-04-04 : 06:06:01
|
quote: Originally posted by gmunky I'm having problem writing a query that retrieves the dates(mm-dd-yy) that matches both the month and the year that a user inputs.for instance:date table01-31-0502-23-0501-22-0503-25-05I want to be able to retrieve these dates:01-31-0501-23-05i tried using LIKE approach:select from table where date like '%05'but this query retrieves all the '05 dates. is there a way to use LIKE for both the month and the year?Thanks for any help!
Why dont you make your life easy by storing dates in proper DATETIME datatype?MadhivananFailing to plan is Planning to fail |
 |
|
|
|
|
|
|
|