Changing the output of Getdate

2019-01-20 08:20发布

Is it possible to deceive SQL Server to return a different date on GetDate() without actually changing the machine date?
This would be great, since we have a database with old data and I'm trying to test some queries that use getdate().
I can change my machine date but that brings some other problems with other applications...
Any tips?
Thanks!

4条回答
唯我独甜
2楼-- · 2019-01-20 08:51

You can always wrap GetDate() in a custom function and use that everywhere, although it's not an optimal solution.

查看更多
Evening l夕情丶
3楼-- · 2019-01-20 08:56

No, there is not much you can do other than something like this:

SELECT GETDATE()-7  --get date time 7 days ago
查看更多
The star\"
4楼-- · 2019-01-20 09:04

According to the documentation for getdate():

This value is derived from the operating system of the computer on which the instance of SQL Server is running.

Since it's derived from the OS, I don't think you can change it separately.

查看更多
手持菜刀,她持情操
5楼-- · 2019-01-20 09:13
SELECT DATEADD(dd, -7, GETDATE())
查看更多
登录 后发表回答