Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


select house_no order


select house_no order

Author
Message
shashianireddy
shashianireddy
SSC Rookie
SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)

Group: General Forum Members
Points: 34 Visits: 135
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
shashianireddy
shashianireddy
SSC Rookie
SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)

Group: General Forum Members
Points: 34 Visits: 135
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
ChrisM@Work
ChrisM@Work
SSCrazy Eights
SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)

Group: General Forum Members
Points: 8941 Visits: 19009
shashianireddy (12/20/2013)
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


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



“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
shashianireddy
shashianireddy
SSC Rookie
SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)

Group: General Forum Members
Points: 34 Visits: 135
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
shashianireddy
shashianireddy
SSC Rookie
SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)

Group: General Forum Members
Points: 34 Visits: 135
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
ChrisM@Work
ChrisM@Work
SSCrazy Eights
SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)

Group: General Forum Members
Points: 8941 Visits: 19009
;WITH MyAddresses (HouseNo) AS ( -- meagre sample data set
   SELECT '2/4' UNION ALL
   SELECT '2-4' UNION ALL
   SELECT '3-1-92/4 hyd' UNION ALL
   SELECT '3-1-99/7' UNION ALL
   SELECT '3-1-900' UNION ALL
   SELECT '3-4-' UNION ALL
   SELECT '3-4-53 . HYDERABAD' UNION ALL
   SELECT '3-49-vizag' UNION ALL
   SELECT '3-53-22,nagar' UNION ALL
   SELECT '3-400-199/1.ASVV' UNION ALL
   SELECT '3-5098-hasd' UNION ALL
   SELECT '3-6van' UNION ALL
   SELECT '4-5-123asvr' UNION ALL
   SELECT '4-5-1300.old colony' UNION ALL
   SELECT '4-500-120'
)

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)-1 -- Changed 20131220
   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(x.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 x.Element1 NOT LIKE '%/%' THEN x.Element1+CHAR(255) ELSE x.Element1 END,
      Element2 = RIGHT('000000'+x.Element2,6), -- Changed 20131220       
      Element3 = CASE
         WHEN y.n IS NULL THEN x.Element3
         ELSE CHAR(255)+RIGHT('000000'+LEFT(x.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
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search