Irfan's World

January 23, 2011

SQL Server Dates – Part 6 (Year, MI, SS)

Filed under: SQL Server 2000/2005/2008 — Tags: , , , , — Irfan @ 10:34 pm

Today, I am going to show you how to add hours, minutes or seconds to a datetime field GETDATE() function from SQL Server 2008 always returns Current Date and Time.  I am referencing Current Date and Time as "now" in this article.

To display the current date and time, type the following query in SQL Server editor:

SELECT GETDATE() as ‘Current Date and Time’

Hours

Add 4 hours from now.
SELECT DATEADD(HOUR,4, GETDATE()) as ‘Add Four hours from now’

Subtract 6 hours from now.
SELECT DATEADD(HOUR,-6, GETDATE()) as ‘Subtract 6 hours from now’

Minutes

Add 10 minutes to the current date and time
SELECT DATEADD(MI,10, GETDATE()) as ‘Add 10 min to now’

Subtract 60 minutes from the current date and time
SELECT DATEADD(MI,-60, GETDATE()) as ‘Subtract 60 min from now’

Seconds

Add 50 sec to the current date and time
SELECT DATEADD(SS,50, GETDATE()) as ‘Add 50 sec to now’

Subtract 60 minutes from the current date and time
SELECT DATEADD(SS,-40, GETDATE()) as ‘Subtract 40 sec to now’

Following are the results when we run the above queries in SQL Server Management Studio.

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: