Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

retrieve information based on the second character of a string 1

Status
Not open for further replies.

dwg23

Technical User
Oct 21, 2002
151
US
I am trying to pull information from a table based on the second character of a string.
for example I have information that is listed as.
MM11
MT9
FC4
FT12

I am trying to pull only information that has a T in the second position.

Any ideas?
 
Given TABLE.FIELD

SQL:
where substring(TABLE.FIELD, 2, 1) = 'T'
 
Great! now what if I want to remove the letters and save just the numbers later in the statement.
For example an FT12 and a FT2 would be saved as a 12 and a 2 and then used to say if the 12 is more than the original rating of a 10 then....
 
As long as you are sure that the remaining characters form a numeric, then:
(Given table TBL with field FLD.)

SQL:
select substring(FLD, 3, len(rtrim(FLD))-2) as NumericField
from TBL
where substring(FLD, 2, 1) = 'T'

If the remaining characters may not be a numeric, then use something like:
SQL:
select case 
         when isnumeric(substring(FLD, 3, len(rtrim(FLD))-2)) > 0 then substring(FLD, 3, len(rtrim(FLD))-2) 
         else 0
       end as NumericField
from TBL
where substring(FLD, 2, 1) = 'T'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top