December 15, 2013 at 3:40 am
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
December 15, 2013 at 4:46 am
You can use
order by house_no asc
and get the desired ordering
Igor Micev,My blog: www.igormicev.com
December 15, 2013 at 5:39 am
Thanks for replay ...but ur quarie is not work for above format
December 15, 2013 at 8:57 am
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.
December 15, 2013 at 11:06 am
Each one of your five current threads
select quarie between houseno ..........
select only up to first '-' only
display order by like 1,2,3,4,5...............plz write quarie
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?
For better assistance in answering your questions, please read this[/url].
Hidden RBAR: Triangular Joins[/url] / The "Numbers" or "Tally" Table: What it is and how it replaces a loop[/url] Jeff Moden[/url]
December 16, 2013 at 6:24 am
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
December 16, 2013 at 9:42 am
;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
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
December 16, 2013 at 10:33 am
ChrisM@home (12/15/2013)
Each one of your five current threadsselect quarie between houseno ..........
select only up to first '-' only
display order by like 1,2,3,4,5...............plz write quarie
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
December 20, 2013 at 2:13 am
Thanks for replay ......
ur quarie is working ....... thank u sir
December 20, 2013 at 2:26 am
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.
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
December 20, 2013 at 3:23 am
k ..thanku
if house no like...
SELECT '3-4-53 . HYDERABAD' UNION ALL
SELECT '3-400-199/1.ASVV' UNION ALL
SELECT '3-49-vizag' UNION ALL
SELECT '3-53-22,nagar' UNION ALL
SELECT '4-5-1300.old colony' UNION ALL
SELECT '4-500-120' UNION ALL
SELECT '3-4-' UNION ALL
SELECT '2/4' UNION ALL
SELECT '2-4' UNION ALL
SELECT '3-5098-hasd' UNION ALL
SELECT '3-6van' UNION ALL
SELECT '4-5-123asvr'
after 1st "-" 2nd ''-'' if number is two or more digits not display in order wise.
plz write quarie if digits two or more display in order wise
December 20, 2013 at 3:37 am
if ex - table like this below format using ur quarie not display order
WITH MyAddresses (HouseNo) AS ( -- meagre sample data set
SELECT '3-12-19 . HYDERABAD' UNION ALL
SELECT '3-1-900' UNION ALL
SELECT '3-1-99/3' UNION ALL
SELECT '3-5-vizag' 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
out put like below
3-1-900
3-1-99/3
3-12-19 . HYDERABAD
3-5-vizag
4-5-123asvr
if after first & second '-' more than two digits display order
December 20, 2013 at 3:42 am
shashianireddy (12/20/2013)
k ..thankuif house no like...
SELECT '3-4-53 . HYDERABAD' UNION ALL
SELECT '3-400-199/1.ASVV' UNION ALL
SELECT '3-49-vizag' UNION ALL
SELECT '3-53-22,nagar' UNION ALL
SELECT '4-5-1300.old colony' UNION ALL
SELECT '4-500-120' UNION ALL
SELECT '3-4-' UNION ALL
SELECT '2/4' UNION ALL
SELECT '2-4' UNION ALL
SELECT '3-5098-hasd' UNION ALL
SELECT '3-6van' UNION ALL
SELECT '4-5-123asvr'
after 1st "-" 2nd ''-'' if number is two or more digits not display in order wise.
plz write quarie if digits two or more display in order wise
No problem. Left-pad the second element with a string of '0's then take a fixed number of chars from the right-hand side, like this:
;WITH MyAddresses (HouseNo) AS ( -- meagre sample data set
SELECT '3-4-53 . HYDERABAD' UNION ALL
SELECT '3-400-199/1.ASVV' UNION ALL
SELECT '3-49-vizag' UNION ALL
SELECT '3-53-22,nagar' UNION ALL
SELECT '4-5-1300.old colony' UNION ALL
SELECT '4-500-120' UNION ALL
SELECT '3-4-' UNION ALL
SELECT '2/4' UNION ALL
SELECT '2-4' UNION ALL
SELECT '3-5098-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 = RIGHT('000000'+Element2,6), -- Changed 20131220 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
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
December 20, 2013 at 4:02 am
if houseno like 3-1-900 ,3-1-92/4 ,.......not display order
output like
3-1-900
3-1-92/4 hyd
3-1-99/7
using.......
WITH MyAddresses (HouseNo) AS ( -- meagre sample data set
SELECT '3-1-900' UNION ALL
SELECT '3-1-92/4 hyd' UNION ALL
SELECT '3-1-99/7'
)
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 = RIGHT('000000'+Element2,6), 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
December 20, 2013 at 4:15 am
if houseno like 3-1-900 ,3-1-92/4 ,.......not display order
output like
3-1-900
3-1-92/4 hyd
3-1-99/7
using.......
WITH MyAddresses (HouseNo) AS ( -- meagre sample data set
SELECT '3-1-900' UNION ALL
SELECT '3-1-92/4 hyd' UNION ALL
SELECT '3-1-99/7'
)
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 = RIGHT('000000'+Element2,6), 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
Viewing 15 posts - 1 through 15 (of 15 total)
You must be logged in to reply to this topic. Login to reply