sql查询中的分割字符串

发布于 2024-08-18 19:53:38 字数 308 浏览 11 评论 0原文

我在名为“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

━╋う一瞬間旳綻放 2024-08-25 19:53:38

正如您现在所看到的,尝试对表示日期的字符串列执行任何类型的查询都是一个问题。您有几个选项:

  1. 将postingdate 列转换为某种DATE 或TIMESTAMP 数据类型。我认为这是您的最佳选择,因为它将使使用此字段查询表更快、更灵活且不易出错。

  2. 将postingdate保留为字符串,并在进行比较时使用函数将其转换回日期。这将是一个性能问题,因为除非您的数据库支持基于函数的索引,否则大多数查询将变成全表扫描。

  3. 将发布日期保留为字符串并将其与其他字符串进行比较。这不是一个好的选择,因为很难想出一种方法来以这种方式进行范围查询,正如我认为您已经发现的那样。

如果是我的话我会转换数据。祝你好运。

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:

  1. 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.

  2. 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.

  3. 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.

ヤ经典坏疍 2024-08-25 19:53:38

在 SQL Server 中你可以说

  Select postingdate from post 
  where postingdate between '6/16/1969' and '6/16/1991'

In SQL Server you can say

  Select postingdate from post 
  where postingdate between '6/16/1969' and '6/16/1991'
蒲公英的约定 2024-08-25 19:53:38

如果它确实是一个字符串,那么很幸运它是 YYYY-MM-DD 格式。您可以将该格式作为字符串进行排序和比较,因为最重要的数字位于左侧。例如:

select *
from Posts
where StringDateCol between '2010-01-01' and '2010-01-02'

不需要将字符串转换为日期,这样的比较不受, 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:

select *
from Posts
where StringDateCol between '2010-01-01' and '2010-01-02'

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 :)

只是一片海 2024-08-25 19:53:38

在对其运行日期范围查询之前,您需要将字符串转换为日期。如果您对时间部分不感兴趣,您可能只使用字符串就可以了。

实际功能取决于您的 RDBMS

for strings only
select * from posts
where LEFT(postingDate,10) > '2010-01-21'

or
for datetime ( Sybase example)
select * from posts
where convert(DateTime,postingDate) between '2010-01-21' and '2010-01-31'

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

for strings only
select * from posts
where LEFT(postingDate,10) > '2010-01-21'

or
for datetime ( Sybase example)
select * from posts
where convert(DateTime,postingDate) between '2010-01-21' and '2010-01-31'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文