Adding Numbers To Date/Time Values

Posted August 4, 2006 at 2:25 PM by Ben Nadel

Tags: SQL

Ok ok, last SQL date/time post I swear. But, while testing my last entry idea, I realized you can add numbers to your date/time values. Wicked cool:

  • SELECT
  • -- Query for yesterday's date.
  • (
  • getDate() - 1
  • ) AS yesterday,
  •  
  • -- Query for tomorrow's date.
  • (
  • getDate() + 1
  • ) AS tomorrow

Just note that this leaves in the TIME value as-is.



Reader Comments

Aug 23, 2012 at 7:40 AM // reply »
4 Comments

Hi,
I need to know how to Subtract "3 days 4 hours and 22 minutes" from the current date and print the date.


Aug 23, 2012 at 9:34 AM // reply »
11,314 Comments

@Santhosh,

Have you tried looking at the Date/Time functions built into SQL server? They must have something for it.


Aug 24, 2012 at 1:03 AM // reply »
4 Comments

Hi,
I have tried using date functions. But couldn't find exact.

The thing is if I type the date as such 24-08-2012 the output shd be 21-08-2012.
It shd be applicable for all the dates !!

Note : 3 days 4 hours and 22 minutes shd be included

I need the query for the scenario as its imp for one application.


Aug 24, 2012 at 8:14 AM // reply »
1 Comments

@santosh you can use dateAdd function to add or subtarct date time
<cfset date=dateAdd("d",-3,now() )>


Aug 25, 2012 at 7:44 AM // reply »
1 Comments

@keshav,
Thank u keshav. N also ive asked fr "3 days 4 hours and 22 minutes" in the format to be included !


Aug 26, 2012 at 11:41 PM // reply »
4 Comments

Thank u keshav. N also ive asked fr "3 days 4 hours and 22 minutes" in the format to be included !
Pl suggest !


Aug 27, 2012 at 3:02 AM // reply »
1 Comments

hello ben,
I need to create one exercise by using the Date function. Please help me.
Jus needed a sample query so tat i can wrk it out


Aug 28, 2012 at 3:10 AM // reply »
4 Comments

Hi,
This was the question:

"If the day is (Mon/Wed/Fri) print 'Date is Mon/Wed/Fri' "

THE ANSWER:
create procedure exe
@da_Yl date
as
begin
select day=case DATEPART(DW,@da_Yl)
when '1'then 'na'
when '2'then 'monday'
when '3'then 'na'
when '4'then 'wednes'
when '5'then 'na'
when '6'then 'fri'
when '7'then 'na'
end
end


Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
Jun 18, 2013 at 3:39 PM
Experimenting With The Amazon Simple Storage Service (S3) API Using ColdFusion
Hi Ben, THANKS! While not bleeding edge, it is new to me & I like learning new things every day! ... read »
Jun 18, 2013 at 12:30 PM
Disabling Auto-Correct And Auto-Capitalize Features On iPhone Inputs
Also spellcheck="false" should be mentioned as part of html5 specs ... read »
Jun 18, 2013 at 8:40 AM
Using Named Functions Within Self-Executing Function Blocks In Javascript
Hi Ben, you forgot to mention the most important thing for named self-executing functions - they can be referenced by name ONLY inside their execution context (which is parens in this case), it mean ... read »
dee
Jun 18, 2013 at 7:01 AM
My Safari Browser SQLite Database Hello World Example
hai ben, this program is really good i could understand the concept but i dint know how to save it and how to open it as you have done in the video can u give that details pls ... read »
Jun 18, 2013 at 6:04 AM
Clearing Inline CSS Properties With jQuery
Thanks a lot for for post! It helped me a lot... after being stuck since 24 hrs.. found solution from your post. Thanks again! ... read »
Jun 18, 2013 at 2:31 AM
SOTR 2013 - The Best Conference I Never Went To
I keep watching it, should keep me happily distracted until SotR14 ;) ... read »
Jun 17, 2013 at 9:45 PM
What If All User Interface (UI) Data Came In Reports?
@Jonah, As I was reading what you wrote, it occurred to me that maybe I do something similar to that in some of my client-side code. In an application I'm working on, there are a bunch of unrelated ... read »
Jun 17, 2013 at 9:36 PM
Object Thinking By David West
@Jonah, Please, don't feel bad at all. I appreciate all that you have contributed to the conversation. And, the more points of view I get, the more confident I am that I will some day, some how und ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools