0

I have a column named CreatedDate of type DateTimeOffset, and I need to query to see rows created today. If this column was of type DateTime I would do this:

SELECT * 
FROM MyTable 
WHERE CreatedDate >= GETDATE()

How does one accomplish this with a DateTimeOffset column, however?


Environment: SQL Server 2014

user7560542
  • 527
  • 1
  • 5
  • 14
  • 1
    Does this help? https://stackoverflow.com/questions/4331189/datetime-vs-datetimeoffset – James L. Jun 05 '18 at 21:03
  • 2
    Are you concerned with the current time of day? Or just today's date? GETDATE() will return the current time so even as a regular datetime field, that wouldn't get all rows for "today". – user1011627 Jun 05 '18 at 21:43

1 Answers1

1

Take a look at the TODATETIMEOFFSET function that is built into SQL Server.

Here is an example of how it is used (-5 is my timezone offset...your usage may vary)...again, this also considers you are only worried about >= current time as your original question suggested. You would need to adjust usage of GETDATE() if you care about the entire day (see comment on original question).

select * from TestingDates d where d.CreatedDate >= TODATETIMEOFFSET(GETDATE(), '-05:00')
user1011627
  • 1,741
  • 1
  • 17
  • 25