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 |
|
agarwasa2008
Posting Yak Master
109 Posts |
Posted - 2009-08-31 : 21:28:40
|
| Hi,I have a old table Orders with 2 columnsuserName(nvarchar(100),null)dateCreated (nvarchar(50),null)Some of the data values for the dateCreated column are:========================================================6/3/2008 6:13:45 PM12/07/1990select userNamefrom Orderswhere dateCreated is in the last 2 yearsHow would I do it given the varied data in this column and also it is of type varchar?Any suggestions ideas inputs would be greatly appreciated.Thanks,SASA |
|
|
waterduck
Aged Yak Warrior
982 Posts |
Posted - 2009-08-31 : 22:34:53
|
[code]SELECT userNameFROM OrdersWHERE dateCreated >= 'Your date'[/code]something like this? Hope can help...but advise to wait pros with confirmation... |
 |
|
|
agarwasa2008
Posting Yak Master
109 Posts |
Posted - 2009-09-01 : 01:01:05
|
| Hi,How would I say in the last 2 year with the mixed up data set for dateCreatedSA |
 |
|
|
agarwasa2008
Posting Yak Master
109 Posts |
Posted - 2009-09-01 : 01:12:43
|
| SELECT userNameFROM OrdersWHERE dateCreated like '%2009%'SELECT userNameFROM OrdersWHERE dateCreated like '%2008%'Would this pull out the right data set for dateCreated in the last 2 years esp with the mixed up dataset and dateCreated is varchar. Also is there a way to1) Combine this two queries and 2) Can we output the result set into a text file so that I can use it for further manipulation.Thanks and looking forward to further inputs!!SA |
 |
|
|
matty
Posting Yak Master
161 Posts |
Posted - 2009-09-01 : 01:40:53
|
| DECLARE @StartDate DATETIME,@EndDate DATETIMESET @StartDate = '01/01/2008'SET @EndDate = '01/01/2010'SELECT userNameFROM OrdersWHERE dateCreated >= @StartDate AND dateCreated < @EndDate |
 |
|
|
agarwasa2008
Posting Yak Master
109 Posts |
Posted - 2009-09-01 : 02:08:17
|
| I get the following error - (please note that the dateCreated is nvarchar)Msg 8115, Level 16, State 2, Line 5Arithmetic overflow error converting expression to data type datetime.SA |
 |
|
|
ra.shinde
Posting Yak Master
103 Posts |
Posted - 2009-09-01 : 02:43:48
|
| SELECT userNameFROM OrdersWHERE dateCreated like '%2009%' OR dateCreated like '%2008%'Rahul Shinde |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-09-01 : 05:26:47
|
| 1 Always use proper DATEITME datatype 2 DECLARE @StartDate DATETIME,@EndDate DATETIMESET @StartDate = '01/01/2008'SET @EndDate = '01/01/2010'SELECT userNameFROM OrdersWHERE convert(datetime,dateCreated,103) >= @StartDate AND convert(datetime,dateCreated,103) < @EndDateMadhivananFailing to plan is Planning to fail |
 |
|
|
|
|
|
|
|