Solved

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

Posted on 2008-10-10
7
229 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 60

Expert Comment

by:chapmandew
ID: 22686586
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
ID: 22686627
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
ID: 22686646
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
Webinar: Security & Encryption in the MySQL world

Join Percona’s Solutions Engineer, Dimitri Vanoverbeke as he presents “Security and Encryption in the MySQL world” on Thursday, July 6, 2017 at 7:00 am PDT / 10:00 am EDT (UTC-7).

 

Author Comment

by:bbdesign
ID: 22686672
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
ID: 22686688
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
ID: 22686705
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
ID: 22686864
Thanks!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

717 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