Having problems with SELECT statement...

Discussion in 'Databases' started by Amilo, Jan 21, 2007.

  1. #1
    I have a simple database with one table and 2 columns (id,company).

    These statements do not work:

    SELECT  *  FROM  names  where company = '%A'
    PHP:
    SELECT  *  FROM  names  where company LIKE '%A'
    PHP:
    But the query,s below pull everything from the table including names begining withe the letter A

    SELECT  *  FROM  names  where company <= '%A'
    PHP:
    SELECT  *  FROM  names  where company NOT LIKE '%A'
    PHP:
    I have around 50 companies starting with the letter A
    Any ideas please ?
     
    Amilo, Jan 21, 2007 IP
  2. kashem

    kashem Banned

    Messages:
    1,250
    Likes Received:
    76
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Hi
    Here is a shortcut way to get all the company having name starting with 'A'

    SELECT * FROM names where len(company,1) = 'A'
     
    kashem, Jan 21, 2007 IP
  3. kashem

    kashem Banned

    Messages:
    1,250
    Likes Received:
    76
    Best Answers:
    0
    Trophy Points:
    0
    #3
    besides you did a little mistake to fetch starting with 'A'
    the condition should be 'A%' rather than '%A'.

    '%A' will fetch all the records that last letter is A.

    You could read sql wild issue here
    http://www.techonthenet.com/sql/like.php
     
    kashem, Jan 21, 2007 IP
  4. Amilo

    Amilo Peon

    Messages:
    624
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Not able to execute the query
    Either the table doesnot exist or a wrong query.

    Query is : SELECT * FROM names where len(company,1) = 'A'

    :(


    Resolved **

    Looking at the data in the database, the problem was when I uploaded the company names I had a blank space at the begining and ending of each word.
     
    Amilo, Jan 22, 2007 IP
  5. kashem

    kashem Banned

    Messages:
    1,250
    Likes Received:
    76
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Then you could do two things.
    1. SELECT * FROM names where len(company,2) = ' A'
    2. SELECT * FROM names where len(ltrim(company),1) = 'A'

    for pemanently removing spaces from both sites do the following

    update names set company=rtrim(ltrim(company))
     
    kashem, Jan 22, 2007 IP
  6. toby

    toby Notable Member

    Messages:
    6,923
    Likes Received:
    269
    Best Answers:
    0
    Trophy Points:
    285
    #6
    try>
    SELECT  *  FROM  names  where company LIKE 'A%'
    Code (markup):
     
    toby, Jan 22, 2007 IP