简体   繁体   中英

Why do GETDATE() yield different result when run on SSMS vs. C# Microsoft.Practices.EnterpriseLibrary.Data?

What could be the cause why GETDATE() is yielding different result when executed on Query Editor in SSMS (SQL Server Management Studio) versus from the one executed through Microsoft.Practices.EnterpriseLibrary.Data by Database.ExecuteNonQuery() function?

This GETDATE() is inside a Stored Procedure that UPDATE the date in a table.


Debug by us developers on SSMS:

EXEC [dbo].[SP_NameOfTheSPWithGetDate]()

= 11/4/2016 7:43 PM



Being run through the ASP.net webpage by end-user:

Database SampleDB;
SampleDB.ExecuteNonQuery('[dbo].[SP_NameOfTheSPWithGetDate]');

= 11/4/2016 11:43 AM


I suspect the difference might be because of the +8 offset of the GMT (our server is in GMT+8 ) they run the same exact SP but why the different result from GETDATE()?

We tried all sorts of functions to get the current date like:

  • GETUTCDATE()
  • CURRENT_TIMESTAMP
  • GETDATE()
  • GETSYSTEMDATE()

But still we have the same result.

This problem is weird since I don't really pass any value to the SP that may ever affect the GETDATE() function.

If you need additional info, feel free to ask to help solve this problem.

Additional Information (Update) :

  • The one hosting the ASP.Net page server is different from the SQL Server Database.

Maybe try SYSDATETIMEOFFSET()

So you could check if different timezones causes the problem - if yes:

complete solution/all credits: Getdate() function to get date for my timezone

Since both will be executed on SQL Server, the result will be the same, but the second one will be returned and handled according to the .Net current culture and data format, which in your case seems different.

If you set the culture of your .Net application to something else, for ex: ar-sa, it will show the data in Hijri format which will be totally something different :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM