sql查询中的分割字符串
我在名为“Post”的表中,在名为“postingdate”的字段中有一个值为 2009-11-25, 12:42AM IST
格式的字符串。
我需要查询来根据日期范围获取详细信息。我尝试了以下查询,但它引发了错误。请指导我解决这个问题。提前致谢。
select postingdate
from post
where TO_DATE(postingDate,'YYYY-MM-DD')>61689
and TO_DATE(postingDate,'YYYY-MM-DD')<61691
I have a value in field called "postingdate" as string in 2009-11-25, 12:42AM IST
format, in a table named "Post".
I need the query to fetch the details based on date range. I tried the following query, but it throws an error. Please guide me to fix this issue. Thanks in advance.
select postingdate
from post
where TO_DATE(postingDate,'YYYY-MM-DD')>61689
and TO_DATE(postingDate,'YYYY-MM-DD')<61691
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
正如您现在所看到的,尝试对表示日期的字符串列执行任何类型的查询都是一个问题。您有几个选项:
将postingdate 列转换为某种DATE 或TIMESTAMP 数据类型。我认为这是您的最佳选择,因为它将使使用此字段查询表更快、更灵活且不易出错。
将postingdate保留为字符串,并在进行比较时使用函数将其转换回日期。这将是一个性能问题,因为除非您的数据库支持基于函数的索引,否则大多数查询将变成全表扫描。
将发布日期保留为字符串并将其与其他字符串进行比较。这不是一个好的选择,因为很难想出一种方法来以这种方式进行范围查询,正如我认为您已经发现的那样。
如果是我的话我会转换数据。祝你好运。
As you've now seen, trying to perform any sort of query against a string column which represents a date is a problem. You've got a few options:
Convert the postingdate column to some sort of DATE or TIMESTAMP datatype. I think this is your best choice as it will make querying the table using this field faster, more flexible, and less error prone.
Leave postingdate as a string and use functions to convert it back to a date when doing comparisons. This will be a performance problem as most queries will turn into full table scans unless your database supports function-based indexes.
Leave postingdate as a string and compare it against other strings. Not a good choice as it's tough to come up with a way to do ranged queries this way, as I think you've found.
If it was me I'd convert the data. Good luck.
在 SQL Server 中你可以说
In SQL Server you can say
如果它确实是一个字符串,那么很幸运它是 YYYY-MM-DD 格式。您可以将该格式作为字符串进行排序和比较,因为最重要的数字位于左侧。例如:
不需要将字符串转换为日期,这样的比较不受
, 12:42AM IST
附加符的影响。当然,除非您的表包含来自不同时区的日期:)If it's really a string, you're lucky that it's in YYYY-MM-DD format. You can sort and compare that format as a string, because the most significant numbers are on the left side. For example:
There's no need to convert the string to a date, comparing in this way is not affected by the
, 12:42AM IST
appendage. Unless, of course, your table contains dates from a different time zone :)在对其运行日期范围查询之前,您需要将字符串转换为日期。如果您对时间部分不感兴趣,您可能只使用字符串就可以了。
实际功能取决于您的 RDBMS
You will need to convert your string into a date before you run date range queries on it. You may get away with just using the string if your not interested in the time portion.
The actual functions will depend on your RDBMS