Solved

pull numeric beginning part of a column value, ignore letters, sort on the results

Posted on 2008-10-10
7
189 Views
Last Modified: 2013-11-05
Values of column "acreage":

5
10
20
1 to 5
3 to 8

I want to numeric sort on either the number (in the case of the first three), or the first number (case of last two). So ignore the " to 5" and " to 8", but otherwise it would be convert (int, acreage). How can I do this?

Thank you!
0
Comment
Question by:bbdesign
  • 3
  • 2
  • 2
7 Comments
 
LVL 60

Expert Comment

by:chapmandew
Comment Utility
declare @x varchar(max)
set @x = '3'

select left(@x, case when charindex(' ', @x) = 0 then len(@x) else charindex(' ', @x) end)

so, for a table:

select left(fieldname, case when charindex(' ', fieldname) = 0 then len(fieldname) else charindex(' ', fieldname) end)
from table
0
 
LVL 60

Expert Comment

by:chapmandew
Comment Utility
then just throw your cast around that value:


select cast(left(fieldname, case when charindex(' ', fieldname) = 0 then len(fieldname) else charindex(' ', fieldname) end) as int)
from table
0
 
LVL 92

Expert Comment

by:Patrick Matthews
Comment Utility
Assuming your column *always* starts with at least one numeral...


SELECT *
FROM SomeTable
ORDER BY CAST(LEFT(LEFT(acreage, PATINDEX('%[^0-9]%', acreage) - 1), 3) AS int)
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

Author Comment

by:bbdesign
Comment Utility
chapmandew: I'm trying to put this in an "order by" claus, not a select.

matthewspatrick: I get this error: "Invalid length parameter passed to the SUBSTRING function"

The column always starts with at least one numeral. Sometimes there are other characters (i.e., spaces and/or letters) after the numeral (which can be any number of characters in length), sometimes not.
0
 
LVL 60

Accepted Solution

by:
chapmandew earned 250 total points
Comment Utility
select *
from table
order by cast(left(fieldname, case when charindex(' ', fieldname) = 0 then len(fieldname) else charindex(' ', fieldname) end) as int)
0
 
LVL 92

Assisted Solution

by:Patrick Matthews
Patrick Matthews earned 250 total points
Comment Utility
SELECT *
FROM SomeTable
ORDER BY CAST(LEFT(LEFT(acreage, CASE WHEN PATINDEX('%[^0-9]%', acreage) > 0 THEN (PATINDEX('%[^0-9]%', acreage) - 1) ELSE LEN(acreage) END), 3) AS int)
0
 

Author Comment

by:bbdesign
Comment Utility
Thanks!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now