user7560542
user7560542

Reputation: 547

How does one find rows created today if the CreatedDate column is of type datetimeoffset?

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

Upvotes: 0

Views: 120

Answers (1)

user1011627
user1011627

Reputation: 1811

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')

Upvotes: 1

Related Questions