Monday, March 21, 2011

Time from an SQL server

My latest application does time based queries from a Microsoft SQL Server. After half a day of debugging, I found out that the SQL Server (which I don't have more than just read access to) and my computer had different system times, causing my queries to be invalid.

Luckily, there's a way to query a database and return the time.
SELECT getdate() AS time
I put that into my datareader before any query, and bam! There's the time of the SQL server returned as a single tuple in a column named time. I'm not sure on the semantics of it, but in C# it was easily cast to a DateTime object using Convert.ToDateTime().

Updated because now I use MySQL. And the equal call is:
SELECT NOW()

No comments:

Post a Comment