Ask a Question related to ASP Database, Design and Development.

  1. #1

    Default SQL Query - Help

    Can someone please help me with query.

    We are using this query in one of our asp pages. As I didn't write this
    query in the firstplace I am finding it hard to add the two conditions I
    want.

    I want to add two conditions in the where clause like the one below

    EmpProfile.Left = False and Holidayrequest.Status = 3 to the overall
    condition


    sqlstr = "SELECT [FirstName] & ' ' & [LastName] AS
    Expr1,IIf(DateSerial(Year([fdate]),Month([fdate]),1)<DateSerial(" & selyear
    & "," & selmonth & ",1),1,CInt(Day([FDate]))) AS StartDay, IIf(
    DateSerial(Year([TDate]),Month([TDate]),1)>DateSerial(" & selyear & "," &
    selmonth & ",1),Day(DateSerial(" & selyear & "," & selmonth &
    ",0))+CInt(Day([tDate])),CInt(Day([tDate]))) AS EndDay,
    HolidayRequests.FDate, HolidayRequests.TDate, CInt(Month([FDate])) AS
    StartMonth, CInt(Month([tDate])) AS EndMonth "

    sqlstr = sqlstr & "FROM EmpProfile INNER JOIN HolidayRequests ON
    EmpProfile.EmployeeNumber = HolidayRequests.Userid "

    sqlstr = sqlstr & "WHERE (((Month([FDate]))=" & selmonth & ") AND
    ((Year([FDate]))=" & selyear & ") AND EmpProfile.Department='" & seldept &
    "') OR (((Month([FDate]))="


    The table structure is as below

    Empprofile

    EmployeeNumber (Number)
    FirstName (Text)
    LastName (Text)
    Department (Text)
    Left (Boolean)

    HolidayRequests

    UsedId (number)
    Fdate (Date)
    Tdate (Date)
    Status (Number)


    J P Singh Guest

  2. Similar Questions and Discussions

    1. Query of Queries on query New type query
      In CF5 we have a page that creates a query, using queryNew and querySetCell and the like, we then used dbtype="query" and gave it's name so we could...
    2. query of query throwing weird exception
      One workaround: I had added rows to a cfsearch query, and set a numeric value in custom1 field that that query provides. A query of queries...
    3. Convert a query to a list, or find an item in a query
      Hi All, I am using CFPOP to retrieve mail from a server, then delete each message after I retrieve it. What I want to do is to check that I don;t...
    4. CAML Query: Multiple Query Fields Issue
      I need to Create a CAML Query Dynamically with VB to a Sharepoint WebService GetListItems Method. The User Could Select 1 to X Number of IDs...
    5. BCP query out executed by xp_cmdshell works fine from query analyzer but fails from VB Component
      Hi all, I have a stored procedure which returns a vast number of record and i have to write the output into a csv file. I'm using BCP utility to...
  3. #2

    Default Re: SQL Query - Help

    Good lord.

    modified snippet:

    sqlstr = sqlstr & "WHERE (((Month([FDate]))=" & selmonth & ") AND
    ((Year([FDate]))=" & selyear & ") AND EmpProfile.Department='" & seldept &
    "') OR (((Month([FDate]))=" ThereShouldBeAVariableHere & " AND
    (EmpProfile.Left = FALSE AND Holidayrequests.status=3)"

    Ray at work

    "J P Singh" <noemail@asIhatespam> wrote in message
    news:uE3dkeWcDHA.2820@tk2msftngp13.phx.gbl...
    > Can someone please help me with query.
    >
    > We are using this query in one of our asp pages. As I didn't write this
    > query in the firstplace I am finding it hard to add the two conditions I
    > want.
    >
    > I want to add two conditions in the where clause like the one below
    >
    > EmpProfile.Left = False and Holidayrequest.Status = 3 to the overall
    > condition
    >
    >
    > sqlstr = "SELECT [FirstName] & ' ' & [LastName] AS
    > Expr1,IIf(DateSerial(Year([fdate]),Month([fdate]),1)<DateSerial(" &
    selyear
    > & "," & selmonth & ",1),1,CInt(Day([FDate]))) AS StartDay, IIf(
    > DateSerial(Year([TDate]),Month([TDate]),1)>DateSerial(" & selyear & "," &
    > selmonth & ",1),Day(DateSerial(" & selyear & "," & selmonth &
    > ",0))+CInt(Day([tDate])),CInt(Day([tDate]))) AS EndDay,
    > HolidayRequests.FDate, HolidayRequests.TDate, CInt(Month([FDate])) AS
    > StartMonth, CInt(Month([tDate])) AS EndMonth "
    >
    > sqlstr = sqlstr & "FROM EmpProfile INNER JOIN HolidayRequests ON
    > EmpProfile.EmployeeNumber = HolidayRequests.Userid "
    >
    > sqlstr = sqlstr & "WHERE (((Month([FDate]))=" & selmonth & ") AND
    > ((Year([FDate]))=" & selyear & ") AND EmpProfile.Department='" & seldept
    &
    > "') OR (((Month([FDate]))="
    >
    >
    > The table structure is as below
    >
    > Empprofile
    >
    > EmployeeNumber (Number)
    > FirstName (Text)
    > LastName (Text)
    > Department (Text)
    > Left (Boolean)
    >
    > HolidayRequests
    >
    > UsedId (number)
    > Fdate (Date)
    > Tdate (Date)
    > Status (Number)
    >
    >

    Ray at Guest

  4. #3

    Default SQL query - HELP

    I?m trying to get a SQL query to return data from 3 tables.

    I need all the data in my tbl_producs table to be returned, the other two
    table are linked but some fields are empty i.e. product 1 might only have a
    cat3ID and not a brandID. Product 2 might have both etc.

    I keep getting a ? ?Syntax error (missing operator) in query expression
    'tbl_category_3.cat3ID = tbl_products.cat3ID RIGHT JOIN tbl_brands ON
    tbl_brands.brandID = tbl_products.brandID'. ?

    Here?s my query ?

    <cfquery datasource="#REQUEST.DataSource#" name="ProdList">
    SELECT tbl_products.productsID,
    tbl_products.cat3ID,
    tbl_products.branchID,
    tbl_products.brandID,
    tbl_products.prod_name,
    tbl_category_3.cat3ID,
    tbl_category_3.cat3_name,
    tbl_brands.brandID,
    tbl_brands.brand_name

    FROM tbl_category_3
    LEFT JOIN tbl_products
    ON tbl_category_3.cat3ID = tbl_products.cat3ID
    RIGHT JOIN tbl_brands
    ON tbl_brands.brandID = tbl_products.brandID

    WHERE tbl_products.branchID = '#branchID#'
    ORDER BY prod_name
    </cfquery>

    Any help is greatly appreciated


    CoffeeCup Guest

  5. #4

    Default Re: SQL query - HELP

    What database are you using?
    mxstu Guest

  6. #5

    Default Re: SQL query - HELP

    I'm using MS Access
    CoffeeCup Guest

  7. #6

    Default Re: SQL query - HELP

    I think Access requires parenthesis around each JOIN in the FROM statement I don't have Access on this machine so I cannot give you the correct syntax.
    mxstu Guest

  8. #7

    Default Re: SQL query - HELP

    Thanks "mxstu"

    I will give it a go see what happens...
    CoffeeCup Guest

  9. #8

    Default Re: SQL query - HELP

    You may want to use the query builder in Access just to give you an idea of the syntax. I wouldn't use the Access generated code for your cfquery though... it's usually quite ugly ;-)
    mxstu Guest

  10. #9

    Default Re: SQL query - HELP

    I tried the query builder in access.

    It returns the data, but only the recordes that have data in all filed. So
    catID fields where there is not data it does not return the data???

    Here's the query..

    <cfquery datasource="#REQUEST.DataSource#" name="ProdList">
    SELECT tbl_category_2.cat2ID AS tbl_category_2_cat2ID,
    tbl_category_2.cat2_name,
    tbl_brands.brandID AS tbl_brands_brandID,
    tbl_brands.brand_name,
    tbl_category_3.cat3ID AS tbl_category_3_cat3ID,
    tbl_category_3.cat3_name,
    tbl_products.productsID,
    tbl_products.cat2ID AS tbl_products_cat2ID,
    tbl_products.cat3ID AS tbl_products_cat3ID,
    tbl_products.branchID, tbl_products.brandID AS tbl_products_brandID,
    tbl_products.prod_name
    FROM tbl_category_3
    INNER JOIN (tbl_category_2 INNER JOIN (tbl_brands INNER JOIN tbl_products ON
    tbl_brands.brandID = tbl_products.brandID) ON tbl_category_2.cat2ID =
    tbl_products.cat2ID) ON tbl_category_3.cat3ID = tbl_brands.cat3ID

    WHERE tbl_products.branchID = '#branchID#'
    ORDER BY prod_name
    </cfquery>

    CoffeeCup Guest

  11. #10

    Default Re: SQL query - HELP

    That's because you're using all INNER joins. Now that you understand what parenthesis are required, uou need to change the joins to the approriate type.
    mxstu Guest

  12. #11

    Default Re: SQL query - HELP

    Thanks "mxstu"

    Changed the joins to right joins and it works.

    By the way, if i change the database to SQL will i need to remover the parenthesis?

    Thank for your help
    CoffeeCup Guest

  13. #12

    Default Re: SQL query - HELP

    If you mean sql server, then no you should not *have* to remove the parenthesis, but it would be cleaner if you did.
    mxstu Guest

  14. #13

    Default Re: SQL query - HELP

    Thanks will work on this
    CoffeeCup 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