SQL Tip: TRUNC vs TO_CHAR

Document created by frean04 Employee on Nov 28, 2017
Version 1Show Document
  • View in full screen mode

Sometimes you need to compare dates in SQL but you don't want the hours and minutes to count. 

 

2017-01-01 08:30:11

 

I have seen quite some SQL code using the TO_CHAR function: TO_CHAR(date, 'yyyy-mm-dd'). But this is quite a performance expansive function. Especially if used a lot in the query.

 

You can use another (much more performant function) : TRUNC

 

TRUNC(2017-01-01 08:30:11) will give: 2017-01-01 00:00:00 as a result and will allow you to compare dates independently from the hours and minutes as well.

 

Hope this helps,

1 person found this helpful

Attachments

    Outcomes