473,735 Members | 1,896 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

SQL Query - Date, between dateadd problem

Hello...
I am running into a problem while running a query..can some1 help..
this is the query :
**************
SELECT *
from Table S
where S.dtDate1 BETWEEN dateadd(year,1, dateadd(month,-1,getdate()))
AND dateadd(day,-1,(dateadd(mont h,1,dateadd(yea r,1,dateadd(mon th,-1,getdate())))) )
***************
(first part of the date calculation comes out to be '2005-05-01' and
second part is '2005-05-31')
Here is the interesting twist:
The query runs right if the first date in the between clause is
entered directly i.e. the query wud run right if i rite it as

SELECT *
from vCustomerProduc tDetail S
where S.dtDate1 BETWEEN '2005-05-01'
AND dateadd(day,-1,(dateadd(mont h,1,dateadd(yea r,1,dateadd(mon th,-1,getdate())))) )

The funny part is the ending date in the between part is depending on
the starting date if u notice..so if there is somethg rong in the
first part there shud be somethg rong with the second too if u get wat
i am saying but the query runs right with the second part left as it
is..
can some1 please help..

thanks
-Raghu
Jul 20 '05 #1
1 17328
On 1 Jun 2004 11:33:04 -0700, Raghu wrote:
Hello...
I am running into a problem while running a query..can some1 help..
this is the query :
************ **
SELECT *
from Table S
where S.dtDate1 BETWEEN dateadd(year,1, dateadd(month,-1,getdate()))
AND dateadd(day,-1,(dateadd(mont h,1,dateadd(yea r,1,dateadd(mon th,-1,getdate())))) )
************** *
(first part of the date calculation comes out to be '2005-05-01' and
second part is '2005-05-31')
Here is the interesting twist:
The query runs right if the first date in the between clause is
entered directly i.e. the query wud run right if i rite it as

SELECT *
from vCustomerProduc tDetail S
where S.dtDate1 BETWEEN '2005-05-01'
AND dateadd(day,-1,(dateadd(mont h,1,dateadd(yea r,1,dateadd(mon th,-1,getdate())))) )

The funny part is the ending date in the between part is depending on
the starting date if u notice..so if there is somethg rong in the
first part there shud be somethg rong with the second too if u get wat
i am saying but the query runs right with the second part left as it
is..
can some1 please help..

thanks
-Raghu


Hi Raghu,

You forgot to mention what exactly went wrong. That's usually needed to be
able to help.

In this case, however, I think I can guess what your problem is. Either
some or all rows with dtDate1 equal to May 1st are omitted.

The datetime datatype stores a combination of date and time. If you enter
only a date, a default time of midnight is assumed. Try the following:

SELECT convert(datetim e,'2004-05-01')

Your calculation is based on adding and subtracting whole days and months
to getdate(), a function that returns both current date and time. The time
is retained as it is. See for yourself:

SELECT dateadd(year,1, dateadd(month,-1,getdate()))
returns
2005-05-01 21:08:55.937

All dates in your database of May 1st, midnight are before this date, so
they are not selected. To include them as well, you need to trim off time
portion. This can be done by converting to character, using a style code
that leaves the time out. The char value will be converted back to
datetime, but the time portion is lost at that time.

SELECT dateadd(year, 1, convert (varchar,
dateadd(month,-1,getdate()), 106))
returns
2005-05-01 00:00:00.000

This suffices if all dates stored in dtDate1 are stored with the default
time (00:00:00:000). If other times are stored as well, you'll have to do
something about the end date as well.

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Jul 20 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2727
by: Mark | last post by:
I have an Access 2000 database that pulls a file in everyday via the TransferText method. The file is named "psjrnl 25-Sep-2003.txt" (the 25-Sep-2003 will change and have the current days date). My problem is I am trying to give a variable the file name and it just want work. The variable statement is as follows: PSFILENAME = "psjrnl " & Format(Date,"medium date") & ".txt" which returns "psjrnl 25-Sep-03.txt". Is it possible to change the...
8
2250
by: Ishbel Kargar | last post by:
Since upgrading from old laptop (Windows 98) to new laptop (Windows XP), my mail-merge letters are doing strange things with date formats. For instance, my reminder letter for lapsed subs carries the 'expiry' date as a merge field from the database, and this previously was shown as dd/mm/yy (UK format). Now it persists in showing as mm/dd/yy. I've gone to the Windows Control Panel and made sure that my Regional settings show UK format,...
1
977
by: Brian Jorgenson | last post by:
I am looking for a formula to put in my query to pull data based on the last 3 months. It starts with the current day and will go back 3 months. Here is my wrkflow language for example: CURRENT_DATE = current date PAST_DATE = CURRENT_DATE - 3 months
5
7694
by: Ian Davies | last post by:
Dear Access expert I am trying to use an mde application developed in Access2k with Access 2002. I get an Access error box: Function is not available.. Date() etc. Is Access 2002 back compatible if so how do I work around this date function problem? Ian
2
1535
by: Thomas Beyerlein | last post by:
I am binding dates to a textbox, the date is stored in SQL in a datetime field. When it gets bound it turns it into a long date (Sunday, Dec. 25 2005), in SQL when viewing the table it views as a shortdate. When inserted to SQL the datetimepicker format for the date is short. I have not had a problem with the formatting of a date to a textbox before, but this time I am using the CurrencyManager to navigate though several records. This is...
1
1717
by: zsolt | last post by:
Hi, I'm trying to convert a string to date by specifying the format. The value is like this: "03rd of April 2006". Now this is converted fine by using the following format: "dd\r\d \o\f MMMM yyyy". The problem is of course that the string can be 01st of..., 04th of etc., meaning that I can't use the "\r\d" format description for all possible values.
2
39063
by: zdk | last post by:
I have table name "actionlog",and one field in there is "date_time" date_time (Type:datetime) example value : 11/1/2006 11:05:07 if I'd like to query date between 24/07/2006 to 26/07/2006(I don't need time),how to write SQL command? select * from actionlog where date_time 24/07/2006 AND date_time < 24/07/2006 ??
2
1758
by: siyoyok007 | last post by:
Hello, now i have two DTPicker, and one MS calendar, DTPicker1 is for the start date and DTPicker2 is for the end date. My problem is, how can i only enable only the date between DTPicker1 and DTPicker2..... in other word i want the MS Calendar only available for the specified date ( Date between DTPicker1 and DTPicker2)
1
2222
by: IamMacro | last post by:
hulp5 = "02-02-2006" timesynced = Date.Parse(hulp5) Dim msqlDA As New MySqlDataAdapter("select * from Work where DateCreated <' " & timesynced & " ' ", myconnection) I cant seem to get the "timesynced" when i do every date smaller than my Timesynced it returns all the values that have <Datumcreated>0001-01-01T00:00:00-08:00</datecreated> any date Larger than my Timesynced i get the entire table , so my guess is that my Timesynced...
19
6020
by: phill86 | last post by:
Hi I am re-posting this thread because it has become very confusing and I have got some way to solving the problem so it is a slightly different question from the initial thread. here is the original tread http://bytes.com/topic/access/answers/872005-query-date-range Just to clarify what I am trying to achieve....
0
8965
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8786
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9466
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9255
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9202
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8202
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6050
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4823
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2741
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.