sql problem more difficult to appear in the forum: 46 (date conditions appear strange questions)

Original: SQL problem more difficult to appear in the forum: 46 (date conditions appear strange questions)

Recently, in the forum, I met a lot more difficult sql problem, although they can be resolved, but found a few days later, they can not remember, forget the solution of.

So, I feel the need to be recorded, so that after the encounter this problem again, and get answers from the idea.



Strange issue date conditions arise.


http://bbs.csdn.net/topics/390498925 

select top 1 ekeyid,ProbeMaterial,Result,LabTestDate from PatientLabTestResults
where ProbeMaterial='Ca'  
  and LabTestDate between convert(datetime,2013/1/1) and
   convert(datetime,'2013/6/24') order by LabTestDate desc


这样查的出结果,变成
select top 1 ekeyid,ProbeMaterial,Result,LabTestDate from PatientLabTestResults 
where ProbeMaterial='Ca'  
  and LabTestDate between convert(datetime,2013/1/1) and
   convert(datetime,2013/6/24) order by LabTestDate desc


So after not find the results, in fact, just removed 2013/6/24's' number only, that we can understand what is the problem?


   
   
  1. if OBJECT_ID('t') is not null
  2. drop table t
  3. go
  4. create table t(d datetime)
  5. insert into t
  6. select cast( '2013-05-01' as datetime) as d
  7. union all
  8. select cast( '2013-05-10' as datetime)
  9. /*
  10. 1.
  11. 通过查询计划能看出,SQL Server把下面的查询转化成了:
  12. select * from t where d >= convert(datetime,2013/5/1,0) and d <= convert(datetime,'2013/6/24',0)
  13. 也就是查询条件:
  14. d >= convert(datetime,2013/5/1,0) and d <= convert(datetime,'2013/6/24',0)
  15. 进一步转化:
  16. d >= '1901-02-07 00:00:00.000' and d <= 2013-06-24 00:00:00.000
  17. 这样就能查询出结果集。
  18. */
  19. select *
  20. from t
  21. where d between convert(datetime, 2013/ 5/ 1) and convert(datetime, '2013/6/24')
  22. /*
  23. 2.
  24. 通过查询计划能看出,SQL Server把下面的查询转化成了:
  25. select * from t where d >= convert(datetime,2013/5/1,0) and d <= convert(datetime,2013/6/24,0)
  26. 也就是查询条件:
  27. d >= convert(datetime,2013/5/1,0) and d <= convert(datetime,2013/6/24,0)
  28. 进一步转化:
  29. d >= '1901-02-07 00:00:00.000' and d <= '1900-01-14 00:00:00.000'
  30. 由于SQL Server 把2013/6/24中的斜杠,当成了除号,也就是按除法计算了,
  31. 比如:2013/6/24 就等于13,那么由于datetime默认值是默认值: 1900-01-01 00:00:00,
  32. 那么加上13后,就是1900-01-14 00:00:00.000,这样后就查不出结果了.
  33. */
  34. select *
  35. from t
  36. where d between convert(datetime, 2013/ 5/ 1) and convert(datetime, 2013/ 6/ 24)

In fact,

2013/5/1 is a division operation, the result is 402.
After 2013/6/24 do the divisions, it is 13.


Since the default data type is datetime: "1900-01-01 00:00:00",

Therefore, the above convert (datetime, 2013/5/ 1) is' 1900-01-01 00:00:00 'plus 402,
that is,' 1901-02-0700: 00: 00.000 ",

And convert (datetime, 2013/6/ 24) that is' 1900-01-01 00:00:00 'plus 13,
that is,' 1900-01-1400: 00: 00.000 ",

So you will not find the results.


Therefore, the above should be rewritten 2013/5/1 "2013/5/1", must be in quotation marks.



Published 416 original articles · won praise 135 · views 950 000 +

Guess you like

Origin www.cnblogs.com/lonelyxmas/p/12020080.html