July 26, 2005 at 11:14 am
I have a column which has values as
July 26, 2005 at 12:05 pm
Have you tried ORDER BY ColumnX? If YES and that failed you may need to apply an ORDER BY type INTEGER column and then use that for ordering purposes only.....
Good Hunting!
AJ Ahrens
webmaster@kritter.net
July 26, 2005 at 12:16 pm
But how can you have this as an int column when you ahve data as 2.1.1.2 and so on...any further hint please??
July 26, 2005 at 12:55 pm
I think this may work for you
CREATE TABLE #Numbers( Number varchar(15))
INSERT INTO #Numbers VALUES( '1')
INSERT INTO #Numbers VALUES( '2')
INSERT INTO #Numbers VALUES( '2.1')
INSERT INTO #Numbers VALUES( '3')
INSERT INTO #Numbers VALUES( '4')
INSERT INTO #Numbers VALUES( '2.1.1')
INSERT INTO #Numbers VALUES( '2.1.1.1')
INSERT INTO #Numbers VALUES( '2.1.2')
INSERT INTO #Numbers VALUES( '2.1.3')
INSERT INTO #Numbers VALUES( '2.2')
INSERT INTO #Numbers VALUES( '2.1.4')
INSERT INTO #Numbers VALUES( '2.3')
INSERT INTO #Numbers VALUES( '2.99')
INSERT INTO #Numbers VALUES( '2.99.1')
INSERT INTO #Numbers VALUES( '2.1.5')
INSERT INTO #Numbers VALUES( '2.99.1.1')
INSERT INTO #Numbers VALUES( '2.1.6')
INSERT INTO #Numbers VALUES( '2.100')
INSERT INTO #Numbers VALUES( '2.100.1')
INSERT INTO #Numbers VALUES( '2.101')
INSERT INTO #Numbers VALUES( '2.102')
INSERT INTO #Numbers VALUES( '2.1.7')
SELECT * FROM #Numbers
ORDER BY CONVERT( decimal(10,8), SUBSTRING( REPLACE( Number, CHAR(46), ''), 1, 1) + '.' + SUBSTRING( REPLACE( Number, CHAR(46), ''), 2, 14) )
DROP TABLE #Numbers
I wasn't born stupid - I had to study.
July 26, 2005 at 1:04 pm
if you have four dots('.') (IP addresses)
then
Select *
from Table
order by
cast(parsename(ColX,4) as int)
,cast(parsename(ColX,3) as int)
,cast(parsename(ColX,2) as int)
,cast(parsename(ColX,1) as int)
* Noel
July 26, 2005 at 1:08 pm
darn but you stole my idea noel - have been saving parsename for just such an occasion as this...btw - sometime in the past (many moons ago) - i did test parsename with 2 and 3 dots & it worked equally fine with < 4.............
**ASCII stupid question, get a stupid ANSI !!!**
July 26, 2005 at 1:18 pm
... worked equally fine with <= 4.............
* Noel
July 26, 2005 at 1:22 pm
Noel is correct.
Mine orders this like an Index to a document and that is not what you want. (I did not notice that in your desired order).
I wasn't born stupid - I had to study.
July 26, 2005 at 1:23 pm
have to have the last word don't you ?! okay - worked equally fine with <= 4!
**ASCII stupid question, get a stupid ANSI !!!**
July 26, 2005 at 1:31 pm
thanks but this givesa slightly different result than expected.i need same as in my initial post. i guess i need to tweak the query to do that.
July 26, 2005 at 1:51 pm
Noel, this result
1
2
3
4
2.1
2.2
2.3
2.99
2.100
2.101
2.102
2.1.1
2.1.2
2.1.3
2.1.4
2.1.5
2.1.6
2.1.7
2.99.1
2.100.1
2.1.1.1
2.99.1.1
doesnt match up with my initial listing is there a way to get the result as posted earlier??thanks
July 26, 2005 at 1:58 pm
Sorry try this!
Select number
from MyTable
order by
ISnull(cast(parsename(Number + replicate('.0',3 - len(Number) + len(replace(number,'.',''))),4) as int),0)
,ISnull(cast(parsename(Number + replicate('.0',3 - len(Number) + len(replace(number,'.',''))),3) as int),0)
,ISnull(cast(parsename(Number + replicate('.0',3 - len(Number) + len(replace(number,'.',''))),2) as int),0)
,ISnull(cast(parsename(Number + replicate('.0',3 - len(Number) + len(replace(number,'.',''))),1) as int),0)
* Noel
July 26, 2005 at 2:09 pm
Thanks you are awesome...
July 26, 2005 at 2:10 pm
Happy to Help
* Noel
July 27, 2005 at 2:41 am
Stealing ideas of everyone else, I suggest the following:
SELECT * from #Numbers
order by
convert(int, reverse(parsename(reverse(Number),1))),
convert(int, reverse(parsename(reverse(Number),2))),
convert(int, reverse(parsename(reverse(Number),3))),
convert(int, reverse(parsename(reverse(Number),4)))
Viewing 15 posts - 1 through 15 (of 29 total)
You must be logged in to reply to this topic. Login to reply