Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

select house_no order Expand / Collapse
Author
Message
Posted Sunday, December 15, 2013 3:40 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Sunday, April 6, 2014 12:03 AM
Points: 30, Visits: 119
i have table coloum like below
House_no
3-4-53 . HYDERABAD
3-4-199/1.ASVV
3-4-vizag
3-4-22,nagar
3-4-45.old colony
3-4-66/99
3-4-
2/4
2-4
3-5-hasd
3-6van
4-5-123asvr

select house no order
i want output like below format
House_no
2/4
2-4
3-4-
3-4-vizag
3-4-22,nagar
3-4-45.old colony
3-4-53.HYDERABAD
3-4-66/99
3-4-199/1.ASVV
3-5-hasd
3-6van
4-5-123asvr
Post #1523015
Posted Sunday, December 15, 2013 4:46 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: 2 days ago @ 4:06 AM
Points: 3,129, Visits: 3,252
You can use

order by house_no asc

and get the desired ordering




Igor Micev,
SQL Server developer at Seavus
www.seavus.com
Post #1523016
Posted Sunday, December 15, 2013 5:39 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Sunday, April 6, 2014 12:03 AM
Points: 30, Visits: 119
Thanks for replay ...but ur quarie is not work for above format
Post #1523020
Posted Sunday, December 15, 2013 8:56 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Yesterday @ 8:54 PM
Points: 20,901, Visits: 32,940
Since your data is character based and inconsistent, you are going to need to figure out a way to parse/transform the data into a consistent format that can be sorted.

Not sure based on the sample data if that fully represents all of your data.



Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1523040
Posted Sunday, December 15, 2013 11:06 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Yesterday @ 12:23 PM
Points: 1,037, Visits: 7,037
Each one of your five current threads

select quarie between houseno ..........
select house_no order
select only up to first '-' only
display order by like 1,2,3,4,5...............plz write quarie
display order by houseno

relate to the same issue. Help us and you will help yourself. Please provide a sample data set which is properly representative of your data. Your data doesn't all look like "3-9-55". If it did, any one of several solutions already posted would work just fine.
Is "3-9-55" just a Hyderabad house number or is it three data elements combined into one?



Low-hanging fruit picker and defender of the moggies





For better assistance in answering your questions, please read this.




Understanding and using APPLY, (I) and (II) Paul White

Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Post #1523052
Posted Monday, December 16, 2013 6:24 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Wednesday, August 20, 2014 5:24 AM
Points: 128, Visits: 490
You may want to look at how you store the numbers since having them with text makes it difficult to sort but this is a start:

declare @TTable Table(
House_no varchar(100)
)

insert into @TTable(House_no)
values ('3-4-53 . HYDERABAD'),
('3-4-199/1.ASVV'),
('3-4-vizag'),
('3-4-22,nagar'),
('3-4-45.old colony'),
('3-4-66/99'),
('3-4-'),
('2/4'),
('2-4'),
('3-5-hasd'),
('3-6van'),
('4-5-123asvr')

select * from @TTable
order by left(House_no, 1), left(House_no, 3)

Mark



Post #1523183
Posted Monday, December 16, 2013 9:42 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Thursday, December 18, 2014 1:43 AM
Points: 6,890, Visits: 14,254
;WITH MyAddresses (HouseNo) AS ( -- meagre sample data set
SELECT '3-4-53 . HYDERABAD' UNION ALL
SELECT '3-4-199/1.ASVV' UNION ALL
SELECT '3-4-vizag' UNION ALL
SELECT '3-4-22,nagar' UNION ALL
SELECT '3-4-45.old colony' UNION ALL
SELECT '3-4-66/99' UNION ALL
SELECT '3-4-' UNION ALL
SELECT '2/4' UNION ALL
SELECT '2-4' UNION ALL
SELECT '3-5-hasd' UNION ALL
SELECT '3-6van' UNION ALL
SELECT '4-5-123asvr'
)

SELECT a.*, x.*, y.*, z.*
FROM MyAddresses a

-- get the position of the hyphens
CROSS APPLY (SELECT n = NULLIF(CHARINDEX('-',a.HouseNo,1),0)) p1
CROSS APPLY (SELECT n = NULLIF(CHARINDEX('-',a.HouseNo,p1.n+1),0)) p2

CROSS APPLY ( -- split the elements from the string
SELECT
Element1 = LEFT(a.HouseNo,ISNULL(p1.n-1,8000)),
Element2 = SUBSTRING(a.HouseNo,p1.n+1,ISNULL(p2.n-p1.n-1,8000)),
Element3 = SUBSTRING(a.HouseNo,p2.n+1,8000)
) x

CROSS APPLY ( -- calculate the length of the numeric - first - part of element 3
SELECT n = NULLIF(MIN(n),1)
FROM (SELECT n = ROW_NUMBER() OVER(ORDER BY (SELECT NULL))
FROM (VALUES (0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) d (n)) t
WHERE SUBSTRING(Element3,n,1) NOT LIKE '[0-9]'
) y

CROSS APPLY ( -- jig around with the elements to force the desired sort order
SELECT
Element1 = CASE WHEN Element1 NOT LIKE '%/%' THEN Element1+CHAR(255) ELSE Element1 END,
Element2,
Element3 = CASE WHEN y.n IS NULL THEN Element3 ELSE CHAR(255)+RIGHT('000000'+LEFT(Element3,y.n),6) END
) z
ORDER BY z.Element1, z.Element2, z.Element3




“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Post #1523316
Posted Monday, December 16, 2013 10:33 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: 2 days ago @ 1:36 AM
Points: 1,950, Visits: 20,197
ChrisM@home (12/15/2013)
Each one of your five current threads

select quarie between houseno ..........
select house_no order
select only up to first '-' only
display order by like 1,2,3,4,5...............plz write quarie
display order by houseno

relate to the same issue. Help us and you will help yourself. Please provide a sample data set which is properly representative of your data. Your data doesn't all look like "3-9-55". If it did, any one of several solutions already posted would work just fine.
Is "3-9-55" just a Hyderabad house number or is it three data elements combined into one?



looking at all the OP posts on this requirement...I still don't understand why the address has to be sorted by house name/number?......what is the business benefit?


______________________________________________________________
you can lead a user to data....but you cannot make them think
and remember....every day is a school day
Post #1523338
Posted Friday, December 20, 2013 2:13 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Sunday, April 6, 2014 12:03 AM
Points: 30, Visits: 119
Thanks for replay ......


ur quarie is working ....... thank u sir
Post #1524899
Posted Friday, December 20, 2013 2:26 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Thursday, December 18, 2014 1:43 AM
Points: 6,890, Visits: 14,254
shashianireddy (12/20/2013)
Thanks for replay ......


ur quarie is working ....... thank u sir


Not so fast! It won't work for all cases. I recommend you use the "stare and compare" method rigorously and monitor for failures. The code was knocked up in a few minutes and should be tweaked for functionality, performance, and maintainability - which means documentation. If there's anything you are unsure of, post back for an explanation.


“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Post #1524900
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse