Changing the output of Getdate

萝らか妹 提交于 2019-11-27 07:38:26

问题


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!


回答1:


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.




回答2:


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




回答3:


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

SELECT GETDATE()-7  --get date time 7 days ago



回答4:


SELECT DATEADD(dd, -7, GETDATE())


来源:https://stackoverflow.com/questions/2593047/changing-the-output-of-getdate

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