SQL Server 2005 Using DateAdd to add a day to a date - sql

How do I in SQL Server 2005 use the DateAdd function to add a day to a date

Use the following function:
DATEADD(type, value, date)
date is the date you want to manipulate
value is the integere value you want to add (or subtract if you provide a negative number)
type is 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
SELECT DATEADD(dd, 1, GETDATE()) -- will return a current date + 1 day
http://msdn.microsoft.com/en-us/library/ms186819.aspx

DECLARE #MyDate datetime
-- ... set your datetime's initial value ...'
DATEADD(d, 1, #MyDate)

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

The following query I have used in SQL Server 2008, it may be help you.
For add day
DATEADD(DAY,20,GETDATE())
*20 is the day quantity

DECLARE #date DateTime
SET #date = GetDate()
SET #date = DateAdd(day, 1, #date)
SELECT #date

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

Related

select query to display Date format as DAY MMM DD YYYY in SQL server 2008

I tried like this
SELECT DATENAME(DW,GETDATE()) + ' ' + CONVERT(VARCHAR(12), SYSDATETIME(), 107)
result is
Thursday Dec 11, 2014
Required OutPut:
SELECT QUERY TO DISPLAY DATE AS SHOWN BELOW
Thu Dec 11, 2014
All you need is to take only left 3 symbols of your day of the week name:
SELECT LEFT(DATENAME(DW,GETDATE()),3) + ' ' + CONVERT(VARCHAR(12), SYSDATETIME(), 107)
SELECT CONVERT(VARCHAR(3), DATENAME(DW,GETDATE())) + ' '
+ CONVERT(VARCHAR(12), SYSDATETIME(), 107)
SELECT LEFT(DATENAME(DW,GETDATE()),3) + ' ' + CONVERT(VARCHAR(12), SYSDATETIME(), 107)
FROM yourtable

I want to add 5 hours on my current time

I want to have an output like this
MM-DD-YYYY +5hours
Mine is Feb 24, 2014 4:05PM
Here's my code
SELECT CAST(DATEADD(hour,5,getdate()) AS nvarchar(30))
DATEADD (datepart , number , date )
Declare #myDate Datetime
Set #myDate = dateadd(HOUR, 5, getdate())
SELECT Left(Convert(varchar(10),#myDate,21),10) +
stuff(right(convert(varchar(26), #myDate, 109 ), 15 ), 7, 7, ' ')
Fiddle Demo
Use CONVERT passing the Style:
SELECT CONVERT(NVARCHAR(30), DATEADD(hour,5,getdate()), 101)
http://msdn.microsoft.com/en-us/library/ms187928.aspx

SQL Get data in this year

i have a DB with a atribute "Date" which is a string ...
its format is:
"Jan 5 2014 6:26 PM"
and would like to get the number of rows where the date is this year.
already know how to convert the date:
SELECT convert(datetime, 'Oct 23 2012 11:01AM')
i found this code but I do not know how to join the two
select * from datetimes2
where dtm2 >= CAST(CURRENT_TIMESTAMP AS DATE)
and dtm2 < DATEADD(DD, 1, CAST(CURRENT_TIMESTAMP AS DATE))
;
Now I do not know how to do what I want :(
Is this what you looking for ?
DECLARE #datetime DATETIME = 'Jan 5 2014 6:26 PM'
SELECT
*
FROM datetimes2
WHERE [Date] >= CONVERT(DATETIME, CAST(DATEPART(YEAR, #datetime) AS VARCHAR) + '-01-01')
AND [Date] <= CONVERT(DATETIME, CAST(DATEPART(YEAR, #datetime) AS VARCHAR) + '-12-31')
This will get the records for complete year 2014. Hope this helps!
As simple as (assuming SQL server, given convert() )
select * from mytable where year( convert(datetime, date) ) = year( getDate() )
?

sql server Get the FULL month name from a date

How do I use sql to get the whole month name in sql server?
I did't find a way using DATEPART(mm, mydate) or CONVERT(VARCHAR(12), CreatedFor, 107).
Basically I need in the format: April 1 2009.
SELECT DATENAME(MONTH, GETDATE())
+ RIGHT(CONVERT(VARCHAR(12), GETDATE(), 107), 9) AS [Month DD, YYYY]
OR Date without Comma Between date and year, you can use the following
SELECT DATENAME(MONTH, GETDATE()) + ' ' + CAST(DAY(GETDATE()) AS VARCHAR(2))
+ ' ' + CAST(YEAR(GETDATE()) AS VARCHAR(4)) AS [Month DD YYYY]
If you are using SQL Server 2012 or later, you can use:
SELECT FORMAT(MyDate, 'MMMM dd yyyy')
You can view the documentation for more information on the format.
Most answers are a bit more complicated than necessary, or don't provide the exact format requested.
select Format(getdate(), 'MMMM dd yyyy') --returns 'October 01 2020', note the leading zero
select Format(getdate(), 'MMMM d yyyy') --returns the desired format with out the leading zero: 'October 1 2020'
If you want a comma, as you normally would, use:
select Format(getdate(), 'MMMM d, yyyy') --returns 'October 1, 2020'
Note: even though there is only one 'd' for the day, it will become a 2 digit day when needed.
109 - mon dd yyyy (In SQL conversion)
The required format is April 1 2009
so
SELECT DATENAME(MONTH, GETDATE()) + RIGHT(CONVERT(VARCHAR(12), GETDATE(), 109), 9)
Result is:
select datename(DAY,GETDATE()) +'-'+ datename(MONTH,GETDATE()) +'- '+
datename(YEAR,GETDATE()) as 'yourcolumnname'

convert datetime to display full month

I have searched and i cannot seem to find the answer. I have tried using different styles, but none of them give me exactly what i want.
I am trying to convert a datetime, that I have calculated. However I need it to display the full month.
SELECT CONVERT(VARCHAR(25),DATEADD(dd, #codelife, getdate()),107)
the above line works fine, EXCEPT it displays the date like
Feb 27, 2014
I need it to display date like
February 27, 2014 etc.....
any suggestions?
Nathan's answer seems solid, however if you're using SQL Server 2012 you can make use of the FORMAT() function:
SELECT FORMAT(GETDATE(),'MMMM dd, yyyy')
Or in your case:
SELECT FORMAT(DATEADD(dd, #codelife, getdate()),'MMMM dd, yyyy')
I always use http://www.sql-server-helper.com/tips/date-formats.aspx as my reference
SELECT DATENAME(MM, GETDATE()) + RIGHT(CONVERT(VARCHAR(12), GETDATE(), 107), 9) AS [Month DD, YYYY]
I would declare your date as a variable and do something like:
DECLARE #myDate DATETIME
SET #myDate = DateAdd(dd, #codelife, getdate());
SELECT DATENAME(MM, #myDate) + RIGHT(CONVERT(VARCHAR(12), #myDate, 107), 9) AS [Month DD, YYYY]
You may have to do it in 2 steps.
DECLARE #codelife INT = 120
DECLARE #codedate DATETIME = (SELECT (DATEADD(D, #codelife, GETDATE())))
SELECT DATENAME(MM, #codedate) + SUBSTRING(CONVERT(VARCHAR(25), #codedate, 107), 4, 25)
SELECT DATENAME(MONTH, GETDATE())
+ RIGHT(CONVERT(VARCHAR(12), GETDATE(), 107), 9) AS [Month DD, YYYY]
OR Date without Comma Between date and year, you can use the following
SELECT DATENAME(MONTH, GETDATE()) + ' ' + CAST(DAY(GETDATE()) AS VARCHAR(2))
+ ' ' + CAST(YEAR(GETDATE()) AS VARCHAR(4)) AS [Month DD YYYY]
I answered the same question a couple of days ago have a look here sql server Get the FULL month name from a date