ANSI SQL减去一天 [英] ANSI SQL for subtracting a day

查看:89
本文介绍了ANSI SQL减去一天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

同时支持MySQL和SQL Server我需要从日期中减去几天.可以使用ANSI SQL完成此操作,还是需要维护两个版本?

Supporting both MySQL and SQL Server I need to subtract a number of days from a date. Can this be done with ANSI SQL or do I need to maintain two version of this?

例如.我有这个用于MySQL:

Eg. I have this for MySQL:

SELECT
  CURRENT_DATE - INTERVAL '30' DAY
FROM dual;

推荐答案

ANSI SQL 2003规范没有公开可用的版本,但是没有厂商完全支持它.无论如何,这两者之间的语法有足够的差异,可以为每一个编写不同的代码.

There's no publicly available version for the ANSI SQL 2003 specs, but no vendor fully support it to begin with. Any how, there are enough differences in the syntax between these two to write different code for each.

MySQL具有DATE_ADD功能:

MySQL has a DATE_ADD function:

SELECT DATE_ADD(CURRENT_DATE, INTERVAL 30 DAY)
FROM dual;

SQL Server具有DATEADD函数(不带下划线):

SQL Server has a DATEADD function (without the underscore):

SELECT DATEADD(DAY, 30, CAST(GETDATE() AS date))

GETDATE()返回日期&时间.演员将时间部分剥离掉.

GETDATE() return both date & time. The cast strips the time portion off.

这篇关于ANSI SQL减去一天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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