问题
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