SQL Server UTC string comparison not working

烈酒焚心 提交于 2021-02-17 06:53:06

问题


we have two SQL Servers.

We have the following example SQL to recreate an issue we are facing.

select * from (

select '2020-11-04 08:00:00' as start
union all
select '2020-11-04 08:00:00' as start

) a

where cast(convert(date, a.start) as datetime) + cast(Cast(a.start AS time) as datetime) > '2020-11-04 08:00:00'

when we run this query of the servers, one server returns two records and the other returns zero records.

Both should should return zero records.

We think there is some setting that needs to be adjusted on the server to correct this. Could you help us out? Thanks


回答1:


I suspect the servers have different base languages/settings for dates - though obviously this is just a guess.

One may consider 2020-11-04 08:00:00 as 8:00am on November 4, whereas the other treats it as 11 April.

These can be based on

  • Windows regional settings
  • Default language of SQL Server

You should use a universal datetime format e.g., 20201104 08:00:00. If you change the query above to use these, and the servers return the same answer as each other, I think you've found the issue.

Of course, the servers could be in different timezones and/or haven't accounted for daylight savings - but SELECT getdate() should tell you what the server dates are.



来源:https://stackoverflow.com/questions/64679099/sql-server-utc-string-comparison-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!