Date type comparison in SQL (accurate to hours, minutes and seconds)

Time type comparison

I encountered a time type comparison problem today. The previous process was to pass in a String type date format (2020-10-27 10:08:00), and then execute this sql

to_char(a.end_time,'yyyy-MM-dd HH:mm:ss') <= #{endTime}

However, in the actual process, I found that it seems that the hours, minutes and seconds after the space cannot be compared with the String type for comparison, so some problems occurred. After searching some information, I changed the sql to

to_number(to_date(a.end_time, 'YYYY-MM-DD HH24:MI:SS') - to_date(#{endTime}, 'YYYY-MM-DD HH24:MI:SS')) <= 0

But our database is quite special, it is a gold warehouse database, mysql should be

timestampdiff(SECOND,a.end_time,#{endTime}) <= 0

Get the time comparison accurate to the hour, minute and second by subtracting the date.

Guess you like

Origin blog.csdn.net/badarker/article/details/109307320