SQL 获取明天的日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16033993/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get tomorrows date
提问by user1292656
I am trying to get tomorrows date in a sql statement for a date comparison but it is not working.
我试图在 sql 语句中获取明天的日期以进行日期比较,但它不起作用。
Below is my code:
下面是我的代码:
select *
from tblcalendarentries
where convert(varchar,tblcalendarentries.[Start Time],101)
= convert(varchar, GETDATE() +1, 101)
回答by Tanner
To get tomorrows date you can use the below code that will add 1 day to the current system date:
要获得明天的日期,您可以使用以下代码将 1 天添加到当前系统日期:
SELECT DATEADD(day, 1, GETDATE())
Returns the current database system timestamp as a datetime value without the database time zone offset. This value is derived from the operating system of the computer on which the instance of SQL Server is running.
DATEADD(datepart , number , date)
Returns a specified date with the specified number interval (signed integer) added to a specified datepart of that date.
将当前数据库系统时间戳作为不带数据库时区偏移量的日期时间值返回。此值源自运行 SQL Server 实例的计算机的操作系统。
返回具有指定数字间隔(有符号整数)的指定日期添加到该日期的指定日期部分。
So adding this to your code in the WHERE
clause:
因此,将其添加到WHERE
子句中的代码中:
WHERE CONVERT(VARCHAR, tblcalendarentries.[Start Time], 101) =
CONVERT(VARCHAR, DATEADD(DAY, 1, GETDATE()), 101);
First off, GETDATE()
will get you today's date in the following format:
首先,GETDATE()
将以以下格式为您提供今天的日期:
2013-04-16 10:10:02.047
Then using DATEADD()
, allows you to add (or subtract if required) a date or time interval from a specified date. So the interval could be: year, month, day, hour, minute etc.
然后使用DATEADD()
, 允许您从指定日期添加(或根据需要减去)日期或时间间隔。所以间隔可以是:年、月、日、小时、分钟等。
Working with Timezones?
使用时区?
If you are working with systems that cross timezones, you may also want to consider using GETUTCDATE()
:
如果您正在使用跨时区的系统,您可能还需要考虑使用GETUTCDATE()
:
Returns the current database system timestamp as a datetime value. The database time zone offset is not included. This value represents the current UTC time (Coordinated Universal Time). This value is derived from the operating system of the computer on which the instance of SQL Server is running.
将当前数据库系统时间戳作为日期时间值返回。不包括数据库时区偏移量。该值表示当前的 UTC 时间(协调世界时)。此值源自运行 SQL Server 实例的计算机的操作系统。
回答by SMS
Try the below:
试试下面的:
SELECT GETDATE() + 1
This adds one day to current date
这将在当前日期增加一天
回答by Mudassir Hasan
Specify size of varchar
in convert()
指定varchar
in 的大小convert()
where convert(varchar(11),tblcalendarentries.[Start Time],101) = convert(varchar(11), GETDATE() +1, 101)
回答by Damien_The_Unbeliever
I would write:
我会写:
where
DATEADD(day,DATEDIFF(day,0,tblcalendarentries.[Start Time]),0) =
DATEADD(day,DATEDIFF(day,0,GETDATE()),1)
This avoids converting dates to strings entirely, whilst also removing the time portion from both values.
这避免了将日期完全转换为字符串,同时还从两个值中删除了时间部分。
回答by Santhosh
I think if you add one to the date, it will give the next day's date.
我想如果你在日期上加一个,它会给出第二天的日期。
DATE+1
That should be fine. Sorry I do not have access to code SQL SERVER here to provide code
那应该没问题。抱歉,我无权访问代码 SQL SERVER 此处提供代码