如何比较存储为String(varchar)在数据库中的日期? [英] how to compare dates which is stored as String(varchar) in database?
问题描述
日期以这种方式存储 YYYY-MM-DD HH:mm:ss
例如:数据库中的"2014-10-30 10:10:10".
现在,我想比较两个日期并通过使用查询从数据库中获取记录, 2014-09-10 10:10:10(fromdate)至2014-10-10 10:10:10(todate)
如何获取所有准确的记录. 有什么解决办法吗?
谢谢.
只需比较字符串即可,而不会产生额外开销.
此格式"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.
这篇关于如何比较存储为String(varchar)在数据库中的日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!