如何比较存储为String(varchar)在数据库中的日期? [英] how to compare dates which is stored as String(varchar) in database?

查看:130
本文介绍了如何比较存储为String(varchar)在数据库中的日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据库(表),其中有2个字段: fromdate varchar(20) 至今varchar(20)

日期以这种方式存储 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆