且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何比较存储为String(varchar)在数据库中的日期?

更新时间:2023-01-29 11:18:11

只需比较字符串即可,而不会产生额外开销.

此格式"YYYY-MM-DD HH:mm:ss"按时间顺序和文字字母顺序

SELECT * FROM someTable
WHERE fromdate >= '2014-09-10 10:10:10' AND todate <= '2014-10-10 10:10:10'

此外,我会在这些列上创建索引.

i have a database(table), in which 2 fields are: fromdate varchar(20) todate varchar(20)

dates are stored in this fashion YYYY-MM-DD HH:mm:ss

for eg :"2014-10-30 10:10:10" in database.

Now i want to compare two dates and fetch records from database by using query, 2014-09-10 10:10:10(fromdate) to 2014-10-10 10:10:10(todate)

how to fetch all accurate records.. Is there any kind of solution..

thanks.

Just compare the string without extra overhead.

This format "YYYY-MM-DD HH:mm:ss" shares chronological and literal alphabetical order

SELECT * FROM someTable
WHERE fromdate >= '2014-09-10 10:10:10' AND todate <= '2014-10-10 10:10:10'

Also, I would create an index on those columns.