PDA

Click to See Complete Forum and Search --> : Quick question about SQL


Mithyrl
Aug 15th, 2000, 09:57 AM
Just a quick question...

I am building an SQL statement to pull all values out of a database that begin with the letter S and over...I wrote something like:


"SELECT * FROM Database WHERE Partnumber >= 'S'"


However this statement does not include Partnumbers such as S-2000 or even S1234.

Then I thought of adding an ! to the statement so it would include these values:


"SELECT * FROM Database WHERE Partnumber >= 'S!'"


This would produce the results I wanted....I was just wondering if there was a better way of doing this...without tacking on the !.

Clunietp
Aug 15th, 2000, 11:13 AM
Use the LIKE statement with a wildcard ( % ) for character searching:

SELECT * FROM Database WHERE Partnumber LIKE 'S%'

JHausmann
Aug 15th, 2000, 11:17 AM
And, if you're using Access and the "%" wildcard doesn't work, try "*" in its place

666539
Aug 15th, 2000, 11:17 AM
Try using the like operator with the % and _

the % stands in for a substring of 0 or more characters

example

select * from table where string like 'S%'

the _ stands for exactly one character

example

select * from table where string like 'S__'

I know the statement with like % will probably work for you. I've had that work with Microsoft SQL server. I don't have any experinece using the underscore but from what I've read in SAMS publishing SQL Unleashed that should work.

Mithyrl
Aug 15th, 2000, 12:51 PM
The LIKE statement with operator works nicely to retrieve all of the S items nicely. I thank you all for the help.

I just added it with an OR to my original statement to create the desired results.


SELECT * FROM Database WHERE Partnumber LIKE 'S%' OR Partnumber >= 'S'


Thanks again :)