简体   繁体   English

之间和在MySql中工作正常,但在T-SQL中则无法正常工作

[英]Between and is working fine in MySql but not in T-SQL

We had a project with backend MySQL which we recently migrated to SQL Server 2005. A query like the one given below used to work fine when MySQL was used where as it is giving unexpected result. 我们有一个使用后端MySQL的项目,该项目最近已迁移到SQL Server2005。当使用MySQL时,如下面给出的查询可正常使用,因为它给出了意外的结果。

select * from employees where joindate between '2013-05-01' and '2013-05-01'

here the starting and ending dates are same. 这里的开始和结束日期是相同的。

MySQL used to give the records of employees who joined on '2013-05-01' . MySQL过去曾记录过参加'2013-05-01'的员工的记录。 Where as T-SQL is giving the records of employees who joined on '2013-04-30' T-SQL在哪里记录参加“ 2013-04-30”的员工的记录

joindate column in the table has time part as well in which we are recording the exact time when the employee joined along with date. 表中的joindate列也包含时间部分,我们在其中记录员工加入日期的确切时间。

Is there any work around for this problem. 有没有解决此问题的方法。 Please kindy let me know. 请让我知道。

thanks, dpchimmili. 谢谢,dpchimmili。

MySQL used to give the records of employees who joined on '2013-05-01'. Where as T-SQL is giving the records of employees who joined on '2013-04-30'

that is how the different servers implemented the between function differently: for as mySQL includes all dates between and with the same date, T-sql only includes the dates between those dates and not the dates itself. 这就是不同服务器如何实现功能之间的差异:因为mySQL包含介于同一日期之间和具有相同日期的所有日期,所以T-sql仅包括这些日期between的日期,而不包括日期本身。

use Where statements as work around WHERE joindate >='2013-05-01' and joindate <= '2013-05-01' or jsut WHERE joindate ='2013-05-01' 使用Where语句来解决WHERE joindate >='2013-05-01' and joindate <= '2013-05-01'WHERE joindate ='2013-05-01'

You can use BETWEEN with TSQL if you just cast the value to a DATE . 如果只是将值强制转换为DATE则可以将BETWEEN与TSQL一起使用。

select * from employees 
where CONVERT(DATE, joindate) 
between '2013-05-01' and '2013-05-01'

Better though is to check if the date is less than the next date, since that avoids a calculation per row and will hit indexes better; 最好检查日期是否小于下一个日期,因为这样可以避免每行计算,并且可以更好地索引。

select * from employees 
where joindate>='2013-05-01' 
  and joindate<DATEADD(day,1,'2013-05-01')

An SQLfiddle to test with . 要使用进行测试的SQLfiddle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM