Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
neoragav
Starting Member
8 Posts |
Posted - 2011-12-06 : 04:35:24
|
Hi all,I really need help for fixing one of the issue in my project.We have a passenger table which is used for Airlines operation. We need to sort the passengers by their seat numbers. Seat numbers are in format <1/2digits><1Alphabet> eg: 1A, 1B, 1C, 22A, 11B, 24D, 9A, 33A, 12B etc...I have managed to sort the passenger table by seat number with the following query:Select * from passenger_tbl where (dyn_flt_alcd = 'FR') and (dyn_flt_no = '000') and (dyn_flt_dt = '11/20/2011')and dyn_bcty='BLL' order by LEN(fname_seat1),fname_seat1where fname_seat1 is varchar(3) which will hold the seat number.The problem now is that I have to sort the passengers according to the seat numbers and display only passengers from 6th row. I can get the seat number of 6th row from code, I am unable to form a query which sorts the passengers according to seat numbers from 6th row.I have formed the below query which is not working as expected.Select * from passenger_tbl where (dyn_flt_alcd = 'FR') and (dyn_flt_no = '000') and (dyn_flt_dt = '11/20/2011')and dyn_bcty='BLL' and len(fname_seat1)>=2 and fname_seat1 >= '9A' order by LEN(fname_seat1),fname_seat1where 9A is the seat number of the sixth row. The result is not as expectedNote: the expected result is that "The passengers who have seat number greater than '9A' should be listed".Please help!!! |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-12-06 : 04:48:59
|
[code]Select * from passenger_tbl where (dyn_flt_alcd = 'FR') and (dyn_flt_no = '000') and (dyn_flt_dt = '11/20/2011')and dyn_bcty='BLL' and len(fname_seat1)>=2 and fname_seat1 >= '9A' order by left(fname_seat1,patindex('%[A-Za-z]%',fname_seat1)-1)*1 ASC,stuff(fname_seat1,1,patindex('%[A-Za-z]%',fname_seat1)-1,'') ASC[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
neoragav
Starting Member
8 Posts |
Posted - 2011-12-06 : 04:55:15
|
Sorry sir, I am not getting the seat numbers after '12A'. The result is as below :( 1A 4D6C8C 9C 12A14B17D20D31A31E32A33Aquote: Originally posted by visakh16
Select * from passenger_tbl where (dyn_flt_alcd = 'FR') and (dyn_flt_no = '000') and (dyn_flt_dt = '11/20/2011')and dyn_bcty='BLL' and len(fname_seat1)>=2 and fname_seat1 >= '9A' order by left(fname_seat1,patindex('%[A-Za-z]%',fname_seat1)-1)*1 ASC,stuff(fname_seat1,1,patindex('%[A-Za-z]%',fname_seat1)-1,'') ASC ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
|
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-12-06 : 05:05:10
|
that may be because of your other filter conditions. i've not specifically put anything to filter the ones after 12A. i've added only order by part------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
neoragav
Starting Member
8 Posts |
Posted - 2011-12-06 : 05:07:58
|
Sorry for not posting my full query :(This is my query and I am using fname_seat1 >= '12A'select * from passenger_tbl WHERE fname_alcd='FR' AND fname_flt_no='000' AND fname_flt_dt='12/06/2011' and len(fname_seat1)>=2 and fname_seat1 >= '12A' order by left(fname_seat1,patindex('%[A-Za-z]%',fname_seat1)-1)*1 ASC,stuff(fname_seat1,1,patindex('%[A-Za-z]%',fname_seat1)-1,'') ASCquote: Originally posted by visakh16 that may be because of your other filter conditions. i've not specifically put anything to filter the ones after 12A. i've added only order by part------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
|
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-12-06 : 05:14:27
|
[code]select * from passenger_tbl WHERE fname_alcd='FR' AND fname_flt_no='000' AND fname_flt_dt='12/06/2011' and len(fname_seat1)>=2 and left(fname_seat1,patindex('%[A-Za-z]%',fname_seat1)-1)*1 >= 12 and stuff(fname_seat1,1,patindex('%[A-Za-z]%',fname_seat1)-1,'') >'A' order by left(fname_seat1,patindex('%[A-Za-z]%',fname_seat1)-1)*1 ASC,stuff(fname_seat1,1,patindex('%[A-Za-z]%',fname_seat1)-1,'') ASC[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
neoragav
Starting Member
8 Posts |
Posted - 2011-12-06 : 05:22:39
|
Perfect sir :) But only one thing is missing. The list now is not including '12A' the list which is generated is14B17D20D31EIt would be better if the above list also includes '12A'Thanks in advance.quote: Originally posted by visakh16
select * from passenger_tbl WHERE fname_alcd='FR' AND fname_flt_no='000' AND fname_flt_dt='12/06/2011' and len(fname_seat1)>=2 and left(fname_seat1,patindex('%[A-Za-z]%',fname_seat1)-1)*1 >= 12 and stuff(fname_seat1,1,patindex('%[A-Za-z]%',fname_seat1)-1,'') >'A' order by left(fname_seat1,patindex('%[A-Za-z]%',fname_seat1)-1)*1 ASC,stuff(fname_seat1,1,patindex('%[A-Za-z]%',fname_seat1)-1,'') ASC ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
|
 |
|
neoragav
Starting Member
8 Posts |
Posted - 2011-12-06 : 05:54:01
|
I have following seat numbers1A 4D6C8C 9C 10A11A12A14B17D20D31A31E32A33Aif i use the query given"select * from passenger_tbl WHERE fname_alcd='FR' AND fname_flt_no='000' AND fname_flt_dt='12/06/2011' and len(fname_seat1)>=2 and left(fname_seat1,patindex('%[A-Za-z]%',fname_seat1)-1)*1 >= 12 and stuff(fname_seat1,1,patindex('%[A-Za-z]%',fname_seat1)-1,'') >'A' order by left(fname_seat1,patindex('%[A-Za-z]%',fname_seat1)-1)*1 ASC,stuff(fname_seat1,1,patindex('%[A-Za-z]%',fname_seat1)-1,'') ASC"The result is 14B17D20D31Ebut the expected result is 14B17D20D31A31E32A33APlease sir!!!quote: Originally posted by neoragav Perfect sir :) But only one thing is missing. The list now is not including '12A' the list which is generated is14B17D20D31EIt would be better if the above list also includes '12A'Thanks in advance.quote: Originally posted by visakh16
select * from passenger_tbl WHERE fname_alcd='FR' AND fname_flt_no='000' AND fname_flt_dt='12/06/2011' and len(fname_seat1)>=2 and left(fname_seat1,patindex('%[A-Za-z]%',fname_seat1)-1)*1 >= 12 and stuff(fname_seat1,1,patindex('%[A-Za-z]%',fname_seat1)-1,'') >'A' order by left(fname_seat1,patindex('%[A-Za-z]%',fname_seat1)-1)*1 ASC,stuff(fname_seat1,1,patindex('%[A-Za-z]%',fname_seat1)-1,'') ASC ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
|
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-12-06 : 05:56:11
|
[code]select * from passenger_tbl WHERE fname_alcd='FR' AND fname_flt_no='000' AND fname_flt_dt='12/06/2011' and len(fname_seat1)>=2 and left(fname_seat1,patindex('%[A-Za-z]%',fname_seat1)-1)*1 >= 12 and stuff(fname_seat1,1,patindex('%[A-Za-z]%',fname_seat1)-1,'') >='A' order by left(fname_seat1,patindex('%[A-Za-z]%',fname_seat1)-1)*1 ASC,stuff(fname_seat1,1,patindex('%[A-Za-z]%',fname_seat1)-1,'') ASC[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
neoragav
Starting Member
8 Posts |
Posted - 2011-12-06 : 06:00:29
|
Thank you very much sir!!! It's working like charm:)quote: Originally posted by visakh16
select * from passenger_tbl WHERE fname_alcd='FR' AND fname_flt_no='000' AND fname_flt_dt='12/06/2011' and len(fname_seat1)>=2 and left(fname_seat1,patindex('%[A-Za-z]%',fname_seat1)-1)*1 >= 12 and stuff(fname_seat1,1,patindex('%[A-Za-z]%',fname_seat1)-1,'') >='A' order by left(fname_seat1,patindex('%[A-Za-z]%',fname_seat1)-1)*1 ASC,stuff(fname_seat1,1,patindex('%[A-Za-z]%',fname_seat1)-1,'') ASC ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
|
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-12-06 : 06:15:05
|
wc------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
neoragav
Starting Member
8 Posts |
Posted - 2011-12-08 : 00:38:53
|
Hi Sir,I have problem now for a scenarioif the seat numbers are 12A12B12C14B17D20D31A31E32A33AI need to sort from 12B: from the given query if I execute it again gives 12A12B12C14B17D20D31A31E32A33ABut what I need is from 12B :( Please help quote: Originally posted by visakh16 wc------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
|
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-12-08 : 01:23:51
|
where will 12A go? you dont want it in resultset at all?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
neoragav
Starting Member
8 Posts |
Posted - 2011-12-08 : 04:16:14
|
No Sir...As I want seat numbers after 12B. I wouldn't need 12A to be in result set. I need only seats which would come after 12Bquote: Originally posted by visakh16 where will 12A go? you dont want it in resultset at all?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
|
 |
|
|
|
|
|
|