Sql 日期简写

发布于 2024-08-13 07:15:03 字数 280 浏览 11 评论 0原文

有没有一种方法可以编写一个相当于

select * from log_table where dt >= 'nov-27-2009' and dt < 'nov-28-2009';

but 的查询,您可以只指定 1 个日期,并说您想要这一整天的结果,直到下一天。

我只是编造这个,但形式如下:

select * from log_table where dt = 'nov-27-2009':+1;

Is there a way to write a query equivalent to

select * from log_table where dt >= 'nov-27-2009' and dt < 'nov-28-2009';

but where you could specify only 1 date and say you want the results for that entire day until the next one.

I'm just making this up, but something of the form:

select * from log_table where dt = 'nov-27-2009':+1;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

何处潇湘 2024-08-20 07:15:04

只要您处理相应数据类型的日期数据类型,以下操作就可以工作:

t.date_column + 1

...将为给定日期添加一天。但我还没有找到一个允许将隐式数据类型转换为日期的数据库。

SELECT '12-10-2009' + 1

...在 SQL Server 上将失败,因为 SQL Server 仅在与日期时间数据类型列进行比较时执行隐式转换。因此,您需要使用:

SELECT CONVERT(DATETIME, '12-10-2009') + 1

对于 Oracle,您必须使用 TO_DATE函数; MySQL 会使用类似 STR_TO_DATE

As long as you are dealing with the date data type for the respective data type, the following will work:

t.date_column + 1

...will add one day to the given date. But I have yet to find a db that allows for implicit data type conversion into a date.

SELECT '12-10-2009' + 1

...will fail on SQL Server because SQL Server only performs the implicit conversion when comparing to a datetime data type column. So you need to use:

SELECT CONVERT(DATETIME, '12-10-2009') + 1

For Oracle, you'd have to use the TO_DATE function; MySQL would use something like STR_TO_DATE, etc.

め七分饶幸 2024-08-20 07:15:04

有一个只有日期部分的列(时间为 00:00:00.000),然后您可以添加一个 where 子句: WHERE dt = '2009-11-27'

Have a column that just has the date part (time is 00:00:00.000) and then you can add a where clause: WHERE dt = '2009-11-27'

一页 2024-08-20 07:15:03

我不相信有一种方法可以移植到所有 RDBMS。

检查我的一篇参考文献(SQL Cookbook)表明没有一个 RDBMS 能以完全相同的方式解决该问题方式。我建议您查看该书的第 8 章,其中涵盖了 DB2、Oracle、PostgreSQL、MySQL 的所有不同方法。

不过,我必须在 SQLite 和 SQL Cookbook 中处理这个问题没有涉及 RDBMS,所以我将在这里提及一些。 SQLite 没有日期/时间数据类型;您必须通过将所有日期/时间数据存储为 TEXT 来创建自己的日期/时间数据,并确保您的应用程序强制执行其格式。 SQLite 确实有一组日期/时间转换函数,允许您添加标称日期/时间同时将数据保持为字符串。但是,如果您需要根据存储在文本列中并视为日期/时间数据的数据,将两个持续时间 (HH:MM:SS) 相互添加,则必须 编写自己的函数(搜索“定义 SQLite 用户函数”)并在运行时将它们附加到数据库对sqlite3_create_function()的调用。如果您想要一些添加时间值的用户函数的示例,请告诉我。

I do not believe there is one method that is portable to all RDBMSes.

A check in one of my references (SQL Cookbook) shows that no one RDBMS solves the problem quite the same way. I would recommend checking out Chapter 8 of that book, which covers all of the different methods for DB2, Oracle, PostgreSQL, MySQL.

I've had to deal with this issue in SQLite, though, and SQL Cookbook doesn't address that RDBMS, so I'll mention a bit about it here. SQLite doesn't have a date/time data type; you have to create your own by storing all date/time data as TEXT and ensure that your application enforces its formatting. SQLite does have a set of date/time conversion functions that allow you to add nominal date/times while maintaining the data as strings. If you need to add two time durations (HH:MM:SS) to each other, though, based upon data that you've stored in text columns that you are treating as date/time data, you'll have to write your own functions (search for "Defining SQLite User Functions") and attach them to the database at runtime via a call to sqlite3_create_function(). If you want an example of some user functions that add time values, let me know.

独﹏钓一江月 2024-08-20 07:15:03

对于 MS SQL Server,请查看 DATEPART

/* dy = Day of Year */
select * from log_table where datepart(dy, dt) = datepart(dy, '2009-nov-27');

For MS SQL Server, check out DATEPART.

/* dy = Day of Year */
select * from log_table where datepart(dy, dt) = datepart(dy, '2009-nov-27');
哭泣的笑容 2024-08-20 07:15:03

借助 SQL Server,您可以

   Select * From table
   Where dt >= DateAdd(day, DateDiff(day, 0, @ParamDate), 0)
      And dt < DateAdd(day, DateDiff(day, 0, @ParamDate), 1)

With SQL Server, you could

   Select * From table
   Where dt >= DateAdd(day, DateDiff(day, 0, @ParamDate), 0)
      And dt < DateAdd(day, DateDiff(day, 0, @ParamDate), 1)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文