Hsbc - NJ CoLocation - Loans - Credit Cards - Credit Cards

PDA

View Full Version : Having problems with SELECT statement...


Amilo
Jan 21st 2007, 7:44 pm
I have a simple database with one table and 2 columns (id,company).

These statements do not work:

SELECT * FROM names where company = '%A'
SELECT * FROM names where company LIKE '%A'

But the query,s below pull everything from the table including names begining withe the letter A

SELECT * FROM names where company <= '%A'
SELECT * FROM names where company NOT LIKE '%A'

I have around 50 companies starting with the letter A
Any ideas please ?

kashem
Jan 21st 2007, 10:34 pm
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 21st 2007, 10:58 pm
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

Amilo
Jan 22nd 2007, 2:24 am
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.

kashem
Jan 22nd 2007, 7:14 am
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))

toby
Jan 22nd 2007, 9:55 am
try>
SELECT * FROM names where company LIKE 'A%'