473,471 Members | 1,883 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to Use 'Between' in SQL Statement?

jbt007
40 New Member
I have a SLQ Statement using a prompt to get the value of two codes from the user. This should be an easy thing but the result is not what I expect. Can anyone spot the problem? When I type in 8000 for the start ID and 8999 for the end ID, I want only ActID between 8000 and 8999 but I get codes between 8000 and 8999 as well as codes between 80000 and 89999, and codes between 800000 and 899999! Very odd. The ActID field is a text field and all ID's have leading spaces " 8000" is the Activity ID for 8000 and " 80000" is the ID for 80000. This query should only evaluate the right 5 characters of the ActID.

Any help would be appreciated!

Expand|Select|Wrap|Line Numbers
  1. PARAMETERS [Start Activity ID:] Value, [End Activity ID:] Value;
  2. SELECT qryMnth.Job, qryMnth.Phase, tblCftSum.Srt1 AS Craft, qryMnth.ActID, qryMnth.Act_Title, tblProjInfo.Period, [qryMnth]![PeriodFinish] AS RptDte, Sum(qryMnth.BHrs) AS BHrs, Sum(qryMnth.AHrs_td) AS [AHrs ToDate], Sum([qryMnth].[AHrs_td]-[qryMnth].[AHrs_lm]) AS [AHrs Period], Sum(qryMnth.EHrs_td) AS [EHrs ToDate], Sum([qryMnth].[EHrs_td]-[qryMnth].[EHrs_lm]) AS [EHrs Period], Sum(qryProj.PHrs_sl) AS SLHrs, Sum(qryProj.PHrs_wm) AS MPHrs, Sum(qryMnth.BTot) AS BCst, Sum(qryMnth.ATot_td) AS [ACst ToDate], Sum([qryMnth].[ATot_td]-[qryMnth].[ATot_lm]) AS [ACst Period], Sum(qryMnth.ETot_td) AS [ECst ToDate], Sum([qryMnth].[ETot_td]-[qryMnth].[ETot_lm]) AS [ECst Period], Sum(qryProj.PTot_sl) AS SLCst, Sum(qryProj.PTot_wm) AS PMCst, 1 AS PFT, IIf([AHrs ToDate]=0 Or [EHrs ToDate]=0,0,[AHrs ToDate]/[EHrs ToDate]) AS PFTD, IIf([EHrs Period]=0,0,[AHrs Period]/[EHrs Period]) AS PFTP, IIf([AHrs ToDate]=0 Or [EHrs ToDate]=0,0,([MPHrs]-[AHrs ToDate])/([BHrs]-[EHrs ToDate])) AS PFTG, IIf([AHrs ToDate]=0 Or [EHrs ToDate]=0,0,[MPHrs]/[BHrs]) AS PFAC
  3. FROM tblUser_Sort RIGHT JOIN (tblCftSum RIGHT JOIN (((qryMnth LEFT JOIN tblActMap ON qryMnth.S_Key = tblActMap.S_Key) LEFT JOIN tblProjInfo ON qryMnth.PeriodFinish = tblProjInfo.Period_Dte) LEFT JOIN qryProj ON qryMnth.P_Key = qryProj.P_Key) ON tblCftSum.Srt1 = tblActMap.Srt1) ON tblUser_Sort.Srt4 = tblActMap.Srt4
  4. WHERE (((Val(Right(Trim([qryMnth]![ActID]),5))) Between [Start Activity ID:] And [End Activity ID:]))
  5. GROUP BY qryMnth.Job, qryMnth.Phase, tblCftSum.Srt1, qryMnth.ActID, qryMnth.Act_Title, tblProjInfo.Period, [qryMnth]![PeriodFinish]
  6. HAVING (((tblCftSum.Srt1)<>"07") AND ((tblProjInfo.Period)>=0))
  7. ORDER BY qryMnth.Job, qryMnth.Phase, tblCftSum.Srt1, qryMnth.ActID;
Apr 28 '11 #1
11 2063
pod
298 Contributor
try this:
Expand|Select|Wrap|Line Numbers
  1. ...
  2. WHERE (((Val(Right(Trim([qryMnth]![ActID]),5))) >= [Start Activity ID:] 
  3. And (((Val(Right(Trim([qryMnth]![ActID]),5))) <=  [End Activity ID:]))
Apr 28 '11 #2
jbt007
40 New Member
Ya - I should have said this in my original post. This gives me the same results as the 'Between...' statement.

Expand|Select|Wrap|Line Numbers
  1. WHERE (((Val(Right(Trim([qryMnth]![ActID]),5)))>=[Start Activity ID:] And (Val(Right(Trim([qryMnth]![ActID]),5)))<=[End Activity ID:]))
Apr 28 '11 #3
TheSmileyCoder
2,322 Recognized Expert Moderator Top Contributor
I dont have a good explanation of whats going on, but I can suggest to try:
Expand|Select|Wrap|Line Numbers
  1. WHERE (((cint([qryMnth]![ActID])) Between [Start Activity ID:] And [End Activity ID:]))
OR
Expand|Select|Wrap|Line Numbers
  1. WHERE (((Cint([qryMnth]![ActID])) Between cint([Start Activity ID:]) And cint([End Activity ID:])))
Apr 28 '11 #4
jbt007
40 New Member
I don't know why but this seems to have fixed it:

Expand|Select|Wrap|Line Numbers
  1. WHERE (((Val(Right(Trim([qryMnth]![ActID]),5)))>[Start Activity ID:]-1 And (Val(Right(Trim([qryMnth]![ActID]),5)))<[End Activity ID:]+1))
I guess if you 'force' a calculation in the paramater value, Access decides it's ok to do the math.
Apr 28 '11 #5
TheSmileyCoder
2,322 Recognized Expert Moderator Top Contributor
Did you try the suggestion I made?

On another note, unless there is a need for the ID to be a string, I would suggest converting it to a numeric value. For one, they are faster to perform queries on.
Apr 28 '11 #6
NeoPa
32,556 Recognized Expert Moderator MVP
The answer to your question is simply that you are using strings. You expect the results to reflect numbers (clearly from your question) yet you work in strings. The code is behaving exactly as it aught to when it has three strings to compare. If you want the comparison to behave as if it's dealing with numbers then you need to design it to use numbers.
Apr 29 '11 #7
jbt007
40 New Member
Smiley - Yes I did try your suggestion and the result was the same. The reason for strings is that the ID Code is 10 digits and it may contain an alpha numeric number. The root (right 5) will always be numeric.
May 2 '11 #8
jbt007
40 New Member
NeoPa - I don't follow your response. If a string is converted to a number with Val('string') or CInt('string'), should not the SQL treat the result as a number, not a string? And if it does not, why did this seem to work?

Expand|Select|Wrap|Line Numbers
  1. ... 
  2. WHERE (((Val(Right(Trim([qryMnth]![ActID]),5)))>[Start Activity ID:]-1 And (Val(Right(Trim([qryMnth]![ActID]),5)))<[End Activity ID:]+1))
  3. ...
Thanks for the feedback...
May 2 '11 #9
NeoPa
32,556 Recognized Expert Moderator MVP
The problem you describe in the OP is a classic illustration of text comparisons. The reason it's difficult to see is that the entered parameters are determined by SQL to be strings (seem to be deemed to be strings. Probably because it defaults to that, though I can't speak from much experience there as I very rarely use prompts in SQL. Such an approach is too undefined and can easily lead to misunderstandings such as this).

Why did your example seem to work?
Simply because SQL does a best guess to determine the type for you. - 1 & + 1 are pretty strong clues that the item should be numeric.

Between and Val() used together should work for you too. Fundamentally, the idea is to use the most appropriate types when defining the tables.
May 2 '11 #10
jbt007
40 New Member
So - knowing that changing the ID to a numeric field is not an option, is there a better approach to pulling a sub-set of data between two ranges?
May 4 '11 #11
NeoPa
32,556 Recognized Expert Moderator MVP
JBT007:
So - knowing that changing the ID to a numeric field is not an option, is there a better approach to pulling a sub-set of data between two ranges?
This depends on what you really mean. It seems to me you may be referring specifically to the field definition itself (as the question indicates - but that would be a strange question in the circumstances) or the processing of the data within the query.

If it's the former then I'm thoroughly confused that you'd ask this - as I've already given a clear (I thought) answer that handles just that scenario (You didn't indicate you'd had trouble understanding my post so I can only reasonably assume you didn't).

If it's the latter then frankly, if the option to convert the data to numeric is invalid, then I would suggest wanting to follow that approach indicates a fundamental misunderstanding of your data. Unless the data can be considered as numeric, how can it make sense to even want to treat it as numeric (and sort it or filter it as such)?

Maybe I've missed something here. I don't feel either interpretation for what you ask makes enough sense to be likely, yet I see no other interpretation.
May 4 '11 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Jane | last post by:
Hi, I have the following problem: I have a table of users and a table of appointments that references these users and has start and end times. I want to select those users that have...
1
by: mirth | last post by:
I would like to update a decimal column in a temporary table based on a set of Glcodes from another table. I search for a set of codes and then want to sum the value for each row matching the...
7
by: mark | last post by:
Access 2000: I creating a report that has a record source built by the user who selects the WHERE values. An example is: SELECT * FROM CHARGELOG WHERE STDATE Between #10/27/2003# And...
6
by: Tony Williams | last post by:
I have a query with this Between statement as the criteria: "Between And " If I key in June 2002 as the first quarter date and June 2004 as the second quarter date I get data for June 2002 upto...
14
by: Siv | last post by:
Hi, I just discovered that if in an ADO.NET query I use: "Select * from Invoices Where InvoiceDate BETWEEN StartDate AND EndDate;" In this case StartDate would be 1st of month and EndDate...
13
by: Jim Armstrong | last post by:
Hi all - This problem has been driving me crazy, and I'm hoping the answer is something stupid I am neglecting to see.... The procedure posted below is part of an Access/SQL database I have...
3
by: Remaniak | last post by:
Hi all, In my query I am trying to select values from my table ("data table") which lie between two values that are stored in a different table ("Contract list double 71") My Code is: ...
18
by: dspfun | last post by:
Hi! The words "expression" and "statement" are often used in C99 and C- textbooks, however, I am not sure of the clear defintion of these words with respect to C. Can somebody provide a sharp...
3
by: DanBWeb | last post by:
I am trying to query SQL, based on a date For each term I have a record in the "TermDate" table: TemName StartDate EndDate Term 1 9/1/2007 11/21/2007 Term 2 11/22/2007 1/29/2008 Term...
2
by: dowlingm815 | last post by:
I have a between statement within a query for a date range as follows: between #6/1/2010# and 6/30/2010# What are the between statement parameters rules? I am dropping the records on the...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...
1
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...
0
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...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
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...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.