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 |
|
elpuerco
Starting Member
10 Posts |
Posted - 2007-03-28 : 06:52:05
|
Could someone tell me why this fails based on the following setup please as it is driving me bonkers INSERT INTO #T (part_no, description, pk_date)SELECTi.part_no,LEFT(i.description,40),CONVERT(datetime, ia.fld, 103)FROM items i INNER JOIN itemsadded ia ON i.part_no = ia.part_noWHERE CONVERT(datetime, ia.fld, 103) BETWEEN @StartDate AND COALESCE(@EndDate, ia.fld) In #T pk_date is a datetime fieldIn itemsadded fld is a varchar@StartDate and @EndDate are datetimeI keep getting the error 'The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value'I have tried all manner of ways but cannot get past this error!Any help please?Thanks |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-03-28 : 06:53:18
|
Add either aSET DATEFORMAT MDYorSET DATEFORMAT DMYfirst in your code.Peter LarssonHelsingborg, Sweden |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-03-28 : 06:56:07
|
| [code]INSERT #T ( part_no, description, pk_date )SELECT i.part_no, LEFT(i.description,40), DATEADD(DAY, DATEDIFF(DAY, 0 ,ia.fld), 0)FROM items AS iINNER JOIN itemsadded AS ia ON i.part_no = ia.part_noWHERE ia.fld >= @StartDate AND (@EndDate IS NULL OR ia.fld <= @EndDate)[/code]Peter LarssonHelsingborg, Sweden |
 |
|
|
elpuerco
Starting Member
10 Posts |
Posted - 2007-03-28 : 09:37:51
|
| Hi, thanks Peso.I found too that this works![Code]INSERT INTO #T (part_no, description, pk_date)SELECTi.part_no,LEFT(i.description,40),CONVERT(datetime, ia.fld, 103)FROM items i INNER JOIN itemsadded ia ON i.part_no = ia.part_noWHERE CONVERT(datetime, ia.fld, 103) BETWEEN @StartDate AND COALESCE(@EndDate, CONVERT(datetime, ia.fld, 103))[/Code] |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-03-28 : 09:45:45
|
| Great!Good luck.Peter LarssonHelsingborg, Sweden |
 |
|
|
LoztInSpace
Aged Yak Warrior
940 Posts |
Posted - 2007-03-28 : 21:26:00
|
| This post should have subject "Cursed database developers that use varchar when they mean dates!!!!"Why do people do this? (appreciate it might not have been you). |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-04-02 : 03:13:55
|
| http://sql-server-performance.com/fk_datetime.aspMadhivananFailing to plan is Planning to fail |
 |
|
|
|
|
|
|
|