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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

query based on 2nd to last number??

Status
Not open for further replies.

stubnski

MIS
Nov 17, 2005
403
0
0
US
Hi, I know this has to be easy, but I just can't get it right. I need to base a query on the 2nd to last number in a field so if I wanted all numbers with a four 2nd to last I would get

2345
6441

Any help would be greatly appreciated
 
You could try something like:

Code:
Select FieldName 
From TableName
Where Substring(Convert(varchar(50), FieldName),
	              Len(Convert(varchar(50), FieldName)) -1, 1) = '4'

A bit long winded, but it allows for values of any number of digits (up to 50 digits that is)

Hope this helps.

[vampire][bat]
 
if it's a numeric field you're working with it's likely easier and possibly even a lot faster to do it mathamatically.

select FieldName from TableName where (FieldName % 100 - FieldName %10)/10 = 4
 
Select FieldName
From TableName
Where Substring(Reverse(FieldName),2,1) = '4'

Shoot Me! Shoot Me NOW!!!
- Daffy Duck
 
found that this works as well

table_name.field_name Like '%4_'
 
If you really hate someone (aka: how to make code unreadable [smile]) and column is an integer, try:

WHERE FieldName/10%10 = 4

------
"There's a man... He's bald and wears a short-sleeved shirt, and somehow he's very important to me. I think his name is Homer."
(Jack O'Neill, Stargate)
[banghead]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top