| Author |
Topic |
|
masterx81
Starting Member
12 Posts |
Posted - 2003-09-05 : 10:30:39
|
| Hi, I need to order some records by a varchar field...The standerd order in case of varchar is:'1 blabla''11 blabla''2 blabla''22 bla bla'Etc...I need to order the records by the first number, without changing the structure of the database:'1 blabla''2 blabla''11 blabla''22 bla bla'Thanks!!! |
|
|
setbasedisthetruepath
Used SQL Salesman
992 Posts |
Posted - 2003-09-05 : 10:35:10
|
quote: I need to order the records by the first number
Come again? Your example doesn't do that. If it did it would look like:'1 blabla''11 blabla''2 blabla''22 bla bla'or'11 blabla''1 blabla''22 bla bla''2 blabla'or '11 blabla''1 blabla''2 blabla''22 bla bla'or'1 blabla''11 blabla''22 bla bla''2 blabla'Jonathan{0} |
 |
|
|
masterx81
Starting Member
12 Posts |
Posted - 2003-09-05 : 10:41:57
|
| Sorry :-PI'm italian and i have difficulty to write correct in italian, so, in english... :PThe standard ORDER BY (varchar column) statemet order the records:1 1 asti2 2 asti 3 26 asti 4 27 Asti5 3 astiI need the order in the follow mode:1 1 asti2 2 asti 5 3 asti3 26 asti 4 27 AstiI need to order not by the first number, sorry, but to order by the number that is fisrt of the 'asti'...Thenks! |
 |
|
|
rharmon
Starting Member
41 Posts |
Posted - 2003-09-05 : 10:52:48
|
| It's a little crude, but are you looking for something like this?create table #blablah(blahfield varchar(50))insert into #blablah (blahfield) values( '1 blabla')insert into #blablah (blahfield) values( '11 blabla')insert into #blablah (blahfield) values( '2 blabla')insert into #blablah (blahfield) values( '22 bla bla')select * from #blablahorder by -- get the integer before the spaceconvert(int,left(blahfield,charindex(' ',blahfield)))-- get the text after the space, right(blahfield,len(blahfield)-charindex(' ',blahfield)) |
 |
|
|
masterx81
Starting Member
12 Posts |
Posted - 2003-09-05 : 10:56:21
|
| the problem is that some records have the number and the text joined:12Astior other that have only textAstiIs possible???:'( |
 |
|
|
mr_mist
Grunnio
1870 Posts |
Posted - 2003-09-05 : 11:10:09
|
| You need to sort out your records. Split them into two fields. For the time being, try something like..select * from #blablahorder by case when charindex(' ',blahfield) > 0 then -- get the integer before the spaceconvert(int,left(blahfield,charindex(' ',blahfield)))-- get the text after the spaceelse1end, right(blahfield,len(blahfield)-charindex(' ',blahfield))But that wont really help you. Your problem is with the data, not with the SELECT. And if any of you text has spaces then even the above query will fall over. GIGO I'm afraid.-------Moo. :) |
 |
|
|
masterx81
Starting Member
12 Posts |
Posted - 2003-09-05 : 11:17:43
|
| Is possible to make a loop in sql???I loop until the end of the varchar, and test it with the isnumeric function...It's only in my dream? |
 |
|
|
Page47
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2003-09-05 : 11:40:02
|
| OK, Page47 is in a bad mood today.<rant>First, stop and go read this http://www.sqlteam.com/Forums/topic.asp?TOPIC_ID=28942Second, post the DDL of your table and DML to insert sample data that covers all your particular cases and finally a valid expected rowset, based on that sample data, with the ordering you are looking for. Please stop changing the requirements and the senerio every time someone makes a suggestion. It is frustrating for the people trying to help you (for free).Look at all these people taking shots in the dark to help you. Look at all the wasted time and effort simply because you are not giving all the relevent information. I can appreciate a language barrier as much as the next guy, but as far as I can tell we communicate in english here, so you either need to learn to (or put more effort into) communicate(ing) or go ask someone that understands you.</rant>I'm not trying to flame. I am tyring to help both the poster and the SQL Team community. My tone is harsh, but it reflects my feelings at the moment.Jay White{0} |
 |
|
|
rharmon
Starting Member
41 Posts |
Posted - 2003-09-05 : 11:50:58
|
| This is getting uglier all the time, I'm in concurrance with MR Mist, bad architecture leads to bad days.If you create a udf to handle the numeric placement stuff, you can force it to work.create table blablah(blahfield varchar(50))insert into blablah (blahfield) values( '1 blabla')insert into blablah (blahfield) values( '11 blabla')insert into blablah (blahfield) values( '2 blabla')insert into blablah (blahfield) values( '22 bla bla')insert into blablah (blahfield) values( '22sla bla')insert into blablah (blahfield) values( 'sla bla')insert into blablah (blahfield) values( 's1la bla')insert into blablah (blahfield) values( 'isla bla')goCREATE FUNCTION get_leading_numbers (@instring varchar(50))RETURNS intASBEGINdeclare @string varchar(50)declare @counter intdeclare @charholder char(1)set @charholder = '0'set @counter = 1set @string = @instringwhile isnumeric(@charholder) = 1beginset @charholder = substring(@string,@counter,1)set @counter = @counter + 1endRETURN @counter - 2ENDgoselect blahfield from blablahorder by left(blahfield, dbo.get_leading_numbers(blahfield)),ltrim(right(blahfield,len(blahfield) - dbo.get_leading_numbers(blahfield))) |
 |
|
|
masterx81
Starting Member
12 Posts |
Posted - 2003-09-05 : 11:58:43
|
| Thanks to all.I think to use the method of rharmon.Monday I return to work and I work on this example. THANKS!!! |
 |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2003-09-05 : 12:16:24
|
quote: Originally posted by Page47 OK, Page47 is in a bad mood today.<rant>First, stop and go read this http://www.sqlteam.com/Forums/topic.asp?TOPIC_ID=28942Second, post the DDL of your table and DML to insert sample data that covers all your particular cases and finally a valid expected rowset, based on that sample data, with the ordering you are looking for. Please stop changing the requirements and the senerio every time someone makes a suggestion. It is frustrating for the people trying to help you (for free).Look at all these people taking shots in the dark to help you. Look at all the wasted time and effort simply because you are not giving all the relevent information. I can appreciate a language barrier as much as the next guy, but as far as I can tell we communicate in english here, so you either need to learn to (or put more effort into) communicate(ing) or go ask someone that understands you.</rant>I'm not trying to flame. I am tyring to help both the poster and the SQL Team community. My tone is harsh, but it reflects my feelings at the moment.Jay White{0}
Wow Jay...stay away from the dark side...HEY...I just noticed..it's FRIDAY!!!da dada da da da da..TEQULIA!Brett8-)SELECT @@POST=NewId()That's correct! It's an AlphaNumeric! |
 |
|
|
masterx81
Starting Member
12 Posts |
Posted - 2003-09-09 : 07:14:47
|
| In the case that other people needs this triks... :The function:CREATE FUNCTION get_number (@instring varchar(50)) RETURNS int ASBEGINdeclare @counter intdeclare @charholder char(1)set @charholder = '0'set @counter = 1while isnumeric(@charholder) = 1beginset @charholder = substring(@instring,@counter,1)set @counter = @counter + 1EndIF ((@counter-1) > 0)BEGINset @counter = cast((substring(@instring,1,@counter -2)) as int)Endelse set @counter = 0RETURN @counterEndThe Call:select * from lala order by dbo.get_number(Field),field |
 |
|
|
|