SQL Between Statement

Ask a Question related to Coldfusion Database Access, Design and Development.

  1. #1

    Default SQL Between Statement

    I have a table that has two fields called 'DateEnding' and 'Period'. There
    could be up to 3 different periods for each DateEnding. The problem I'm having
    is selecting a range using the BETWEEN. It's easy to say, 'DateEnding BETWEEN
    FromDate AND ToDate'. However I may want to select from DateEnding, Period 3
    to DateEnding Period 2. Is this possible or do I have to create a field in the
    Database that will represent the DatePeriodEnding and use the BETWEEN on that
    field. I know that's one way but that also means I have to maintain the extra
    field.

    Thanks in advance for your help.
    Dan

    Dan100 Guest

  2. Similar Questions and Discussions

    1. If Statement???
      guys i was wondering if anybody here could help me. I have a page done up with all dynamic text and attributes on it that come from a management...
    2. Use of FOR statement
      I am reading through a book on Objects and References and I don't understand this statement: $sum += $_ for split //; I thought a FOR...
    3. AW: if-else-statement
      --On Wednesday, September 03, 2003 11:56 PM +0200 "B. Fongo" <mygrps@fongo.de> wrote: Look at the docs for CGI.pm under pragmas, the -nosticky...
    4. if statement
      I'm trying to write an if statement i have two field, Status and Attendance. "status" value can be active or inactive "Attendance" value is active...
    5. IIF statement
      Assuming the data is being entered via a form, then use the AfterUpdate event of the control that is bound to the signature field. The code you need...
  3. #2

    Default Re: SQL Between Statement

    The easiest way to handle this is to concatenate the two fields into a single
    text field, and run a WHERE clause on the concatenation. Syntax varies widely
    amongst the various DB's. You will also have to get into DB functions to
    convert non-text values to text. In Access it would be something like the
    following assuming Period is one character text. If number you'd need a CSTR
    function also, or maybe another FORMAT to tack on leading zeros for a fixed
    length text result to be compared):

    <cfset varFrom = DateFormat(Form.FromDate,"YYMMDD") & Form.FromPeriod>
    <cfset varThru = DateFormat(Form.ThruDate,"YYMMDD") & Form.ThruPeriod>

    WHERE FORMAT(DateEnding,"YYMMDD")&Period BETWEEN '#varFrom#' AND '#varThru#'

    JMGibson3 Guest

  4. #3

    Default Re: SQL Between Statement

    JMGibson,

    I'm using SQL 2000 Server and it does not like the & before the Period. Is there another way to join the two fields?

    Thanks
    Dan
    Dan100 Guest

  5. #4

    Default Re: SQL Between Statement

    In sql server you use the "+" to concat.

    can I ask for some sample values of what's in the "period" column and what is
    being passed from the form to the query.
    Then I may be able to suggest a solution

    Ken

    The ScareCrow Guest

  6. #5

    Default Re: SQL Between Statement

    Ken,

    Period is an 'int' and can be 1, 2 or 3. DateEnding is a 'datetime'

    Just prior to the Query I'm building the varFrom and varTo as follows
    <cfset varFrom=CreateODBCDateTime(form.FromDateEnding)&st r(form.ToPeriod)>
    <cfset varTo=CreateODBCDateTime(form.ToDateEnding)&str(fo rm.ToPeriod)>
    form.FromDateEnding and form.ToDateEnding are both selected from a dropdown of
    dates pulled from the database.

    Here's the current Where:
    where ((DateEnding+convert(varchar(8),period)) between '#varFrom#' AND
    '#varTo#')

    Thanks
    Dan

    Dan100 Guest

  7. #6

    Default Re: SQL Between Statement

    If I understand this correctly, then you don't need to concat the values

    The following should work

    WHERE DateEnding BETWEEN #CreateODBCDate(Form.FromDate)# AND
    #CreateODBCDate(Form.ThruDate)#
    AND Period = #Form.ThruPeriod#

    Note:
    If the DateEnding has time values as well, that is the values where inserted
    using CreateODBCDateTime or getdate()
    Then you will need to add a day to the thrudate. This is because when a date
    is passed to createodbcdate it automatically sets the time to 00:00:00
    (midnight)

    Ken

    The ScareCrow Guest

  8. #7

    Default Re: SQL Between Statement

    Ken,

    That was a typo...it should have been form.FromPeriod since Period can be 1, 2
    or 3.
    <cfset varFrom=CreateODBCDateTime(form.FromDateEnding)&st r(form.FromPeriod)>
    <cfset varTo=CreateODBCDateTime(form.ToDateEnding)&str(fo rm.ToPeriod)>

    I do need to concat the values but a Date type and string type don't seem to
    mix.

    Thanks
    Dan

    Dan100 Guest

  9. #8

    Default Re: SQL Between Statement

    Sample Data
    DateEnding Period
    05/30/2005 1
    05/30/2005 2
    05/30/2005 3
    06/14/2005 1
    06/14/2005 2
    06/14/2005 3
    06/30/2005 1
    06/30/2005 2
    06/30/2005 3
    etc..

    Dan100 Guest

  10. #9

    Default Re: SQL Between Statement

    Okay, I'm not really sure why you need to concat (I would need to see the db
    structure), but the following is what you want.

    The form values
    <cfset varFrom = DateFormat(Form.FromDate,"mm/dd/yyyy") & Form.FromPeriod>
    <cfset varThru = DateFormat(Form.ThruDate,"mm/dd/yyyy") & Form.ThruPeriod>

    The where clause

    WHERE Convert(varchar(10), DateEnding, 101) + Period BETWEEN '#varFrom#' AND
    '#varThru#'

    The convert function converts the date to a string. The "101" is the format
    this string takes (in this case "mm/dd/yyyy")
    Look at the sql server BOL for the convert function to see the other formats
    this can have.

    The problem is that you are now doing this between on a string value (so there
    could be inconsistant results)

    Ken


    The ScareCrow Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139