SQL Server 2005 使用 DateAdd 向日期添加一天
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/167491/
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
SQL Server 2005 Using DateAdd to add a day to a date
提问by test
How do I in SQL Server 2005 use the DateAdd function to add a day to a date
如何在 SQL Server 2005 中使用 DateAdd 函数向日期添加一天
回答by Ilya Kochetov
Use the following function:
使用以下函数:
DATEADD(type, value, date)
dateis the date you want to manipulate
valueis the integere value you want to add (or subtract if you provide a negative number)
typeis one of:
- yy, yyyy: year
- qq, q: quarter
- mm, m: month
- dy, y: day of year
- dd, d: day
- wk, ww: week
- dw, w: weekday
- hh: hour
- mi, n: minute
- ss or s: second
- ms: millisecond
- mcs: microsecond
- ns: nanosecond
date是您要操作的日期
value是要添加的整数值(如果提供负数,则减去)
类型是以下之一:
- yy, yyyy: 年
- qq, q: 季度
- mm, m: 月
- dy, y: 一年中的哪一天
- dd, d: 天
- wk, ww: 周
- dw, w: 工作日
- hh:小时
- mi, n: 分钟
- ss 或 s:第二
- ms:毫秒
- mcs:微秒
- ns:纳秒
SELECT DATEADD(dd, 1, GETDATE()) will return a current date + 1 day
SELECT DATEADD(dd, 1, GETDATE()) 将返回当前日期 + 1 天
回答by Joel Coehoorn
DECLARE @MyDate datetime
-- ... set your datetime's initial value ...'
DATEADD(d, 1, @MyDate)
回答by BJ Patel
Try following code will Add one day to current date
尝试使用以下代码将一天添加到当前日期
select DateAdd(day, 1, GetDate())
And in the same way can use Year, Month, Hour, Second etc. instead of day in the same function
并以同样的方式可以在同一函数中使用年、月、小时、秒等代替日
回答by Dilip Kr Singh
The following query i have used in sql-server 2008, it may be help you.
我在 sql-server 2008 中使用了以下查询,它可能对您有所帮助。
For add day DATEADD(DAY,20,GETDATE())
*20 is the day quantity
*20为当天数量
回答by Dana
DECLARE @date DateTime
SET @date = GetDate()
SET @date = DateAdd(day, 1, @date)
SELECT @date
回答by Lakshmanan From INDIA
Select getdate() -- 2010-02-05 10:03:44.527
-- To get all date format
select CONVERT(VARCHAR(12),getdate(),100) +' '+ 'Date -100- MMM DD YYYY' -- Feb 5 2010
union
select CONVERT(VARCHAR(10),getdate(),101) +' '+ 'Date -101- MM/DDYYYY'
Union
select CONVERT(VARCHAR(10),getdate(),102) +' '+ 'Date -102- YYYY.MM.DD'
Union
select CONVERT(VARCHAR(10),getdate(),103) +' '+ 'Date -103- DD/MM/YYYY'
Union
select CONVERT(VARCHAR(10),getdate(),104) +' '+ 'Date -104- DD.MM.YYYY'
Union
select CONVERT(VARCHAR(10),getdate(),105) +' '+ 'Date -105- DD-MM-YYYY'
Union
select CONVERT(VARCHAR(11),getdate(),106) +' '+ 'Date -106- DD MMM YYYY' --ex: 03 Jan 2007
Union
select CONVERT(VARCHAR(12),getdate(),107) +' '+ 'Date -107- MMM DD,YYYY' --ex: Jan 03, 2007
union
select CONVERT(VARCHAR(12),getdate(),109) +' '+ 'Date -108- MMM DD YYYY' -- Feb 5 2010
union
select CONVERT(VARCHAR(12),getdate(),110) +' '+ 'Date -110- MM-DD-YYYY' --02-05-2010
union
select CONVERT(VARCHAR(10),getdate(),111) +' '+ 'Date -111- YYYY/MM/DD'
union
select CONVERT(VARCHAR(12),getdate(),112) +' '+ 'Date -112- YYYYMMDD' -- 20100205
union
select CONVERT(VARCHAR(12),getdate(),113) +' '+ 'Date -113- DD MMM YYYY' -- 05 Feb 2010
SELECT convert(varchar, getdate(), 20) -- 2010-02-05 10:25:14
SELECT convert(varchar, getdate(), 23) -- 2010-02-05
SELECT convert(varchar, getdate(), 24) -- 10:24:20
SELECT convert(varchar, getdate(), 25) -- 2010-02-05 10:24:34.913
SELECT convert(varchar, getdate(), 21) -- 2010-02-05 10:25:02.990
---==================================
-- To get the time
select CONVERT(VARCHAR(12),getdate(),108) +' '+ 'Date -108- HH:MM:SS' -- 10:05:53
select CONVERT(VARCHAR(12),getdate(),114) +' '+ 'Date -114- HH:MM:SS:MS' -- 10:09:46:223
SELECT convert(varchar, getdate(), 22) -- 02/05/10 10:23:11 AM
----=============================================
SELECT getdate()+1
SELECT month(getdate())+1
SELECT year(getdate())+1