| Author |
Topic |
|
SQL Red68
Starting Member
8 Posts |
Posted - 2011-01-11 : 05:11:18
|
| SELECT DISTINCT PURN FROM dbo.AllDataWhere Age >50AND StartDate ..........????Hi all,I need to run a query to select all records the are in February regardless of the year. The StartDate ranges from 01.02.2000 to 10.11.2010could someone help me to write the correct query please? |
|
|
MIK_2008
Master Smack Fu Yak Hacker
1054 Posts |
Posted - 2011-01-11 : 05:15:26
|
| check this one!SELECT DISTINCT PURN FROM dbo.AllDataWhere Age >50AND Month(StartDate)=2 |
 |
|
|
RickD
Slow But Sure Yak Herding Master
3608 Posts |
Posted - 2011-01-11 : 05:15:48
|
| use MONTH(StartDate) = 2sniped.. |
 |
|
|
SQL Red68
Starting Member
8 Posts |
Posted - 2011-01-11 : 05:45:24
|
| SELECT DISTINCT PURN FROM dbo.AllDataWhere MONTH (StartDate) =2AND Age >50 or YearBorn >1960And I got this message:msg241. Level 16, State1, Line 1Conversion failed when converting date and/or time from character string.Can you help? |
 |
|
|
RickD
Slow But Sure Yak Herding Master
3608 Posts |
Posted - 2011-01-11 : 06:07:42
|
| Is startDate a datetime or a varchar? |
 |
|
|
SQL Red68
Starting Member
8 Posts |
Posted - 2011-01-11 : 06:08:20
|
| Varchar I suspect |
 |
|
|
MIK_2008
Master Smack Fu Yak Hacker
1054 Posts |
Posted - 2011-01-11 : 06:17:45
|
| if varchar then replace MONTH (StartDate) =2with Month(cast(Startdate as datetime))=2 |
 |
|
|
SQL Red68
Starting Member
8 Posts |
Posted - 2011-01-11 : 06:21:40
|
| I now get this message:Msg 242, Level 16, State 3, Line 1The conversion of a varchar data type to a datetime data type resulted in an out-of-range value. |
 |
|
|
RickD
Slow But Sure Yak Herding Master
3608 Posts |
Posted - 2011-01-11 : 06:49:11
|
| Run:SELECT StartDate FROM dbo.AllDataWHERE isDate(StartDate) = 0Then sort out the bad data that is returned before running the top query. |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2011-01-12 : 02:32:12
|
| What is the format of dates that are stored in the table?Always use proper DATETIME datatype to store datesMadhivananFailing to plan is Planning to fail |
 |
|
|
|