Problem with SQL Statement

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Andrew Banks

    Problem with SQL Statement

    Can anyone see anything obviously wrong with this statement? I'm not really
    an SQL kind of guy

    SELECT ProductID, Title FROM Products WHERE PlatformID LIKE % AND CategoryID
    = 'f5e45b55-95de-47f4-a79b-b24969178b52' ORDER BY Title

    PlatformID contains a GUID and I thought passing a wildcard would allow all
    all records to be selected with the matching CategoryID

    Any ideas? I'm assuming I've just formed the SQL incorrectly


  • Simon Hayes

    #2
    Re: Problem with SQL Statement


    "Andrew Banks" <banksy@nojunkb lueyonder.co.uk > wrote in message
    news:iju_b.555$ 1d5.5476581@new s-text.cableinet. net...[color=blue]
    > Can anyone see anything obviously wrong with this statement? I'm not[/color]
    really[color=blue]
    > an SQL kind of guy
    >
    > SELECT ProductID, Title FROM Products WHERE PlatformID LIKE % AND[/color]
    CategoryID[color=blue]
    > = 'f5e45b55-95de-47f4-a79b-b24969178b52' ORDER BY Title
    >
    > PlatformID contains a GUID and I thought passing a wildcard would allow[/color]
    all[color=blue]
    > all records to be selected with the matching CategoryID
    >
    > Any ideas? I'm assuming I've just formed the SQL incorrectly
    >
    >[/color]

    The LIKE is the problem - you can look it up in Books Online - but it
    doesn't seem to be necessary here anyway, as your description suggests that
    the PlatformID is irrelevant:

    SELECT
    ProductID, Title
    FROM
    Products
    WHERE
    CategoryID = 'f5e45b55-95de-47f4-a79b-b24969178b52'
    ORDER BY
    Title

    If this doesn't give the results you expect, then you'll need to post some
    more information about the structure of the table and what your data look
    like.

    Simon


    Comment

    • Andrew Banks

      #3
      Re: Problem with SQL Statement

      Managed to Fix it simon.

      I need the PlatformID as users are selecting both the platform and category
      from a drop down menu.

      1 platform can have many categories


      "Simon Hayes" <sql@hayes.ch > wrote in message
      news:403a7398$1 _1@news.bluewin .ch...[color=blue]
      >
      > "Andrew Banks" <banksy@nojunkb lueyonder.co.uk > wrote in message
      > news:iju_b.555$ 1d5.5476581@new s-text.cableinet. net...[color=green]
      > > Can anyone see anything obviously wrong with this statement? I'm not[/color]
      > really[color=green]
      > > an SQL kind of guy
      > >
      > > SELECT ProductID, Title FROM Products WHERE PlatformID LIKE % AND[/color]
      > CategoryID[color=green]
      > > = 'f5e45b55-95de-47f4-a79b-b24969178b52' ORDER BY Title
      > >
      > > PlatformID contains a GUID and I thought passing a wildcard would allow[/color]
      > all[color=green]
      > > all records to be selected with the matching CategoryID
      > >
      > > Any ideas? I'm assuming I've just formed the SQL incorrectly
      > >
      > >[/color]
      >
      > The LIKE is the problem - you can look it up in Books Online - but it
      > doesn't seem to be necessary here anyway, as your description suggests[/color]
      that[color=blue]
      > the PlatformID is irrelevant:
      >
      > SELECT
      > ProductID, Title
      > FROM
      > Products
      > WHERE
      > CategoryID = 'f5e45b55-95de-47f4-a79b-b24969178b52'
      > ORDER BY
      > Title
      >
      > If this doesn't give the results you expect, then you'll need to post some
      > more information about the structure of the table and what your data look
      > like.
      >
      > Simon
      >
      >[/color]


      Comment

      • Erland Sommarskog

        #4
        Re: Problem with SQL Statement

        Andrew Banks (banksy@nojunkb lueyonder.co.uk ) writes:[color=blue]
        > Can anyone see anything obviously wrong with this statement? I'm not
        > really an SQL kind of guy
        >
        > SELECT ProductID, Title FROM Products WHERE PlatformID LIKE % AND
        > CategoryID
        >= 'f5e45b55-95de-47f4-a79b-b24969178b52' ORDER BY Title
        >
        > PlatformID contains a GUID and I thought passing a wildcard would allow
        > all all records to be selected with the matching CategoryID[/color]

        First a general piece of advice: when you post a question like this, be
        more specific. If there is an error message, please share it. If you
        get unexpected results, please say so, and why they are unexpected.

        In this particular case, the problem is that you cannot have a mod
        operator between LIKE and AND. The percent is presumably meant to be
        a wildcard, but in this case you need to quote it:

        ... LIKE '%' AND ...

        --
        Erland Sommarskog, SQL Server MVP, sommar@algonet. se

        Books Online for SQL Server SP3 at
        SQL Server 2025 redefines what's possible for enterprise data. With developer-first features and integration with analytics and AI models, SQL Server 2025 accelerates AI innovation using the data you already have.

        Comment

        Working...