Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing the output of Getdate

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!

like image 660
pauloya Avatar asked Apr 07 '10 14:04

pauloya


People also ask

What is the format of Getdate ()?

The GETDATE() function returns the current database system date and time, in a 'YYYY-MM-DD hh:mm:ss.mmm' format.

How do I change the date format in SQL?

You can specify the format of the dates in your statements using CONVERT and FORMAT. For example: select convert(varchar(max), DateColumn, 13), format(DateColumn, 'dd-MMM-yyyy')


2 Answers

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.

like image 152
Andomar Avatar answered Sep 20 '22 14:09

Andomar


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

like image 29
Klaus Byskov Pedersen Avatar answered Sep 21 '22 14:09

Klaus Byskov Pedersen