Author |
Topic |
keyursoni85
Posting Yak Master
233 Posts |
Posted - 2010-08-04 : 08:30:24
|
Hi, I have sorting problem in sql for questions.I have questions list in table and when i sort that column it returns like below:ABQ1. What is your current job title?Q10. What is your current job title?Q10. Overall Experience With Alliant Insurance ServicesQ2. What is your current job title test test test?WOUT PUT. ABQ1. What is your current job title?Q2. What is your current job title test test test?Q10. What is your current job title?Q10. Overall Experience With Alliant Insurance ServicesWPlease help : ) |
|
Sachin.Nand
2937 Posts |
Posted - 2010-08-04 : 08:42:57
|
[code]declare @tbl as table(val varchar(40))insert into @tblselect 'A' union allselect 'B' union allselect 'Q1' union allselect 'Q10' union allselect 'Q10'union all select 'Q2'union all select 'W'select * from @tblorder by ASCII(val),LEN(val)[/code]Limitations live only in our minds. But if we use our imaginations, our possibilities become limitless. PBUH |
 |
|
keyursoni85
Posting Yak Master
233 Posts |
Posted - 2010-08-04 : 08:59:56
|
If i add Q3declare @tbl as table(val varchar(40))insert into @tblselect 'A' union allselect 'B' union allselect 'Q1' union allselect 'Q10' union allselect 'Q10'union all select 'Q3'union all select 'Q2'union all select 'W'select * from @tblorder by ASCII(val),LEN(val)It will not work :( |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2010-08-04 : 09:18:05
|
select * from @tblorder by LEN(val), val N 56°04'39.26"E 12°55'05.63" |
 |
|
keyursoni85
Posting Yak Master
233 Posts |
Posted - 2010-08-04 : 09:43:44
|
Peso... thank you for your answer but i want Alpha and then numeric in your queryyour result come as:ABWQ1Q2Q3Q9Q10Q10I want it like:OUTPUT---------ABQ1Q2Q3Q9Q10Q10WSo any number of string characters with alpha and then numeric can be handledPlease help.. |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2010-08-04 : 09:50:05
|
order by LEFT(VAL, 1), LEN(val), val N 56°04'39.26"E 12°55'05.63" |
 |
|
keyursoni85
Posting Yak Master
233 Posts |
Posted - 2010-08-04 : 09:54:20
|
Now working with below one.. Plzdeclare @tbl as table(val varchar(40))insert into @tblselect 'G' union allselect 'AM' union allselect 'CS' union allselect 'VG' union allselect 'WS' union allselect 'CEO' union allselect 'ban1' union allselect 'ban2' union allselect 'Fair' union allselect 'Poor' union allselect 'RAMM' union allselect 'SREP' union allselect 'RSSEP' union allselect 'Region' union allselect 'CEIOOOO' union allselect 'Banner 1' union allselect 'Job Title' union allselect 'Sales Office' union allselect 'Sequential serial number' union allselect 'Q2. Relationship With Aon' union allselect 'Q2. Relationship With Hewitt' union allselect 'Q2. Relationship With Mercer' union allselect 'Q2. Relationship With Willis' union allselect 'Q2. Relationship With Lockton' union allselect 'Q9. Future Opportunity With Aon' union allselect 'Q10. Overall Experience With Aon' union allselect 'Q2. Relationship With Gallagher' select *, len(val) from @tblorder by LEFT(VAL, 1), LEN(val), valNot working |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2010-08-04 : 10:02:46
|
Are there more sample data of which we know nothing? N 56°04'39.26"E 12°55'05.63" |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2010-08-04 : 10:08:22
|
[code]declare @tbl as table(val varchar(40))insert into @tblselect 'G' union allselect 'AM' union allselect 'CS' union allselect 'VG' union allselect 'WS' union allselect 'CEO' union allselect 'ban1' union allselect 'ban2' union allselect 'Fair' union allselect 'Poor' union allselect 'RAMM' union allselect 'SREP' union allselect 'RSSEP' union allselect 'Region' union allselect 'CEIOOOO' union allselect 'Banner 1' union allselect 'Job Title' union allselect 'Sales Office' union allselect 'Sequential serial number' union allselect 'Q2. Relationship With Aon' union allselect 'Q2. Relationship With Hewitt' union allselect 'Q2. Relationship With Mercer' union allselect 'Q2. Relationship With Willis' union allselect 'Q2. Relationship With Lockton' union allselect 'Q9. Future Opportunity With Aon' union allselect 'Q10. Overall Experience With Aon' union allselect 'Q2. Relationship With Gallagher' SELECT *FROM @tblORDER BY LEFT(val, 1), CASE PATINDEX('Q[0-9]%', val) WHEN 0 THEN LEN(SUBSTRING(val, 2, charindex(val, '.'))) ELSE LEN(val) END, CASE PATINDEX('Q[0-9]%', val) WHEN 0 THEN SUBSTRING(val, 2, charindex(val, '.')) ELSE val END[/code] N 56°04'39.26"E 12°55'05.63" |
 |
|
keyursoni85
Posting Yak Master
233 Posts |
Posted - 2010-08-04 : 10:08:38
|
No.. its final sample data Just instead of Q1 it may be A1 like that..declare @tbl as table(val varchar(40))insert into @tblselect 'G' union allselect 'AM' union allselect 'CS' union allselect 'VG' union allselect 'WS' union allselect 'CEO' union allselect 'ban1' union allselect 'ban2' union allselect 'Fair' union allselect 'Poor' union allselect 'RAMM' union allselect 'SREP' union allselect 'RSSEP' union allselect 'Region' union allselect 'CEIOOOO' union allselect 'Banner 1' union allselect 'Job Title' union allselect 'Sales Office' union allselect 'Sequential serial number' union allselect 'Q2. Relationship With Aon' union allselect 'Q2. Relationship With Hewitt' union allselect 'Q2. Relationship With Mercer' union allselect 'Q2. Relationship With Willis' union allselect 'Q2. Relationship With Lockton' union allselect 'Q9. Future Opportunity With Aon' union allselect 'Q10. Overall Experience With Aon' union allselect 'Q2. Relationship With Gallagher'select *, len(val) from @tblorder by LEFT(VAL, 1), LEN(val), val |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2010-08-04 : 10:11:50
|
[code]SELECT *FROM @tblORDER BY LEFT(val, 1), CASE PATINDEX('[A-Z][0-9]%', val) WHEN 0 THEN LEN(SUBSTRING(val, 2, charindex(val, '.'))) ELSE LEN(val) END, CASE PATINDEX('[A-Z][0-9]%', val) WHEN 0 THEN SUBSTRING(val, 2, charindex(val, '.')) ELSE val END[/code] N 56°04'39.26"E 12°55'05.63" |
 |
|
keyursoni85
Posting Yak Master
233 Posts |
Posted - 2010-08-04 : 10:21:56
|
Thanks but its not working with Q2 data..And one more thing is can we have generic solution like Question dont have dot (.) in line then...also it will work with data..for eg, Q2. test data -- it may be likeQ2 test data -- it may be likeseedeclare @tbl as table(val varchar(40))insert into @tblselect 'G' union allselect 'AM' union allselect 'CS' union allselect 'VG' union allselect 'WS' union allselect 'CEO' union allselect 'ban1' union allselect 'ban2' union allselect 'Fair' union allselect 'Poor' union allselect 'RAMM' union allselect 'SREP' union allselect 'RSSEP' union allselect 'Region' union allselect 'CEIOOOO' union allselect 'Banner 1' union allselect 'Job Title' union allselect 'Sales Office' union allselect 'Sequential serial number' union allselect 'Q2. Relationship With Aon' union allselect 'Q2. Relationship With Hewitt' union allselect 'Q2. Relationship With Mercer' union allselect 'Q2. Relationship With Willis' union allselect 'Q2. Relationship With Lockton' union allselect 'Q9. Future Opportunity With Aon' union allselect 'Q10. Overall Experience With Aon' union allselect 'Q2. Relationship With Gallagher' --select *, len(val) from @tbl--order by LEFT(VAL, 1), LEN(val), valSELECT *FROM @tblORDER BY LEFT(val, 1), CASE PATINDEX('[A-Z][0-9]%', val) WHEN 0 THEN LEN(SUBSTRING(val, 2, charindex(val, '.'))) ELSE LEN(val) END, CASE PATINDEX('[A-Z][0-9]%', val) WHEN 0 THEN SUBSTRING(val, 2, charindex(val, '.')) ELSE val END |
 |
|
keyursoni85
Posting Yak Master
233 Posts |
Posted - 2010-08-04 : 10:51:15
|
anyone can please help on this type of alpha numeric sorting |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2010-08-04 : 11:06:57
|
This is the result I get. What is wrong?valAMban1ban2Banner 1CEOCSCEIOOOOFairGJob TitlePoorQ2. Relationship With AonQ2. Relationship With HewittQ2. Relationship With MercerQ2. Relationship With WillisQ2. Relationship With LocktonQ2. Relationship With GallagherQ9. Future Opportunity With AonQ10. Overall Experience With AonRAMMRSSEPRegionSales OfficeSequential serial numberSREPVGWS N 56°04'39.26"E 12°55'05.63" |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2010-08-04 : 11:08:28
|
Replace "charindex(val, '.')" with "charindex(val, ' ')" if you not always have "." as separator.And again, why haven't you posted the full and complete sample set?Do you, or we, gain from revealing little by little? N 56°04'39.26"E 12°55'05.63" |
 |
|
keyursoni85
Posting Yak Master
233 Posts |
Posted - 2010-08-04 : 11:11:14
|
IF YOU ALTER STRING OF 'Q2. Relationship With Gallagher' TO 'Q2. Relationship With Gallagher kkkkkk' IT WILL NOT WORK..quote: Originally posted by Peso This is the result I get. What is wrong?valAMban1ban2Banner 1CEOCSCEIOOOOFairGJob TitlePoorQ2. Relationship With AonQ2. Relationship With HewittQ2. Relationship With MercerQ2. Relationship With WillisQ2. Relationship With LocktonQ2. Relationship With GallagherQ9. Future Opportunity With AonQ10. Overall Experience With AonRAMMRSSEPRegionSales OfficeSequential serial numberSREPVGWS N 56°04'39.26"E 12°55'05.63"
|
 |
|
keyursoni85
Posting Yak Master
233 Posts |
Posted - 2010-08-04 : 11:21:43
|
Why you checked with lenth of character it can be anything..see below onedeclare @tbl as table(val varchar(40))insert into @tblselect 'G' union allselect 'AM' union allselect 'CS' union allselect 'VG' union allselect 'WS' union allselect 'CEO' union allselect 'ban1' union allselect 'ban2' union allselect 'Fair' union allselect 'Poor' union allselect 'RAMM' union allselect 'SREP' union allselect 'RSSEP' union allselect 'Region' union allselect 'CEIOOOO' union allselect 'Banner 1' union allselect 'Job Title' union allselect 'Sales Office' union allselect 'Sequential serial number' union allselect 'Q10. Overall Experience With Aon' union allselect 'Q2. Relationship With Aon' union allselect 'Q2. Relationship With Hewitt' union allselect 'Q2. Relationship With Mercer' union allselect 'Q2. Relationship With Willis' union allselect 'Q2. Relationship With Lockton' union allselect 'Q9. Future Opportunity With Aon' union allselect 'Q2. Relationship With Gallagher kleyoie' SELECT *, LEN(SUBSTRING(val, 2, charindex(val, '.'))), SUBSTRING(val, 2, charindex(val, '.')),LEFT(val, 1), len(val)FROM @tblORDER BY LEFT(val, 1),--CASE PATINDEX('[A-Z][0-9]%', val)--WHEN 0 THEN LEN(SUBSTRING(val, 2, charindex(val, '.')))--ELSE LEN(val),--END,--CASE PATINDEX('[A-Z][0-9]%', val)--WHEN 0 THEN SUBSTRING(val, 2, charindex(val, '.'))--ELSE val--END |
 |
|
keyursoni85
Posting Yak Master
233 Posts |
Posted - 2010-08-04 : 11:24:07
|
This is final complete set..of result string...just problem of lenth of string coming..quote: Originally posted by Peso Replace "charindex(val, '.')" with "charindex(val, ' ')" if you not always have "." as separator.And again, why haven't you posted the full and complete sample set?Do you, or we, gain from revealing little by little? N 56°04'39.26"E 12°55'05.63"
|
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2010-08-04 : 11:29:47
|
Now, this should give you an idea to continue if the output doesn't match your preference (of which I still don't know).declare @tbl as table(val varchar(40))insert into @tblselect 'G' union allselect 'AM' union allselect 'CS' union allselect 'VG' union allselect 'WS' union allselect 'CEO' union allselect 'ban1' union allselect 'ban2' union allselect 'Fair' union allselect 'Poor' union allselect 'RAMM' union allselect 'SREP' union allselect 'RSSEP' union allselect 'Region' union allselect 'CEIOOOO' union allselect 'Banner 1' union allselect 'Job Title' union allselect 'Sales Office' union allselect 'Sequential serial number' union allselect 'Q2. Relationship With Aon' union allselect 'Q2. Relationship With Hewitt' union allselect 'Q2. Relationship With Mercer' union allselect 'Q2. Relationship With Willis' union allselect 'Q2. Relationship With Lockton' union allselect 'Q9. Future Opportunity With Aon' union allselect 'Q10. Overall Experience With Aon' union allselect 'Q2. Relationship With Gallagher kkkkkk' --select *, len(val) from @tbl--order by LEFT(VAL, 1), LEN(val), valSELECT *, PATINDEX('[A-Z][0-9]%', val), charindex(' ', val)FROM @tblORDER BY LEFT(val, 1), CASE PATINDEX('[A-Z][0-9]%', val) WHEN 0 THEN LEN(val) ELSE charindex(' ', val) END, CASE PATINDEX('[A-Z][0-9]%', val) WHEN 0 THEN val ELSE SUBSTRING(val, 1, charindex(' ', val) - 1) END, CASE PATINDEX('[A-Z][0-9]%', val) WHEN 0 THEN val ELSE SUBSTRING(val, charindex(' ', val) + 1, LEN(val)) END N 56°04'39.26"E 12°55'05.63" |
 |
|
keyursoni85
Posting Yak Master
233 Posts |
Posted - 2010-08-04 : 13:02:41
|
great thanks peso |
 |
|
keyursoni85
Posting Yak Master
233 Posts |
Posted - 2010-08-05 : 03:17:39
|
Hi Peso,I got the query solution and then found some other data which cause the problem in query..can you please help me on that data to be sorted properly i tried with changes but didnt get solution..Sample data..F.20b Base: Have At Least One Notebook BrandF.21a Base: Organizations Owning At Least One ServerF.21b Base: Have More Than One Server BrandF.21b Base: Have One Server BrandF.21b Base: Have At Least One Server BrandQ.1 Scenarios Relevant to Purchase DecisionsQ.10 Level of IT SupportQ.11 Desktop Purchase Experience - Value Customization vs. SimplicityQ.12 Summary Share Table - Percentage of Desktop and Notebook PCs in OrganizationQ.12a % desktop personal computersQ.12b % notebook personal computersQ.13 Company Annual SalesQ.14 When Business EstablishedQ.15 Organization TypeQ.16 Number of Desktops in OrganizationQ.17 Number of Notebooks in OrganizationQ.S5a Involvement in Technology Product Purchase Decisions - NotebooksQ.S5b Involvement in Technology Product Purchase Decisions - DesktopsQ.S5c Involvement in Technology Product Purchase Decisions - ServersQ.S5d Involvement in Technology Product Purchase Decisions - StorageQ.12a % desktop personal computersQ.12b % notebook personal computersQ11. RCM&D (Baltimore, MD) - RENEWALS: Offers ability to discuss Q2c. Name Of Auto Service Provider - Jiffy LubeQ2c. Name Of Auto Service Provider - Pep BoysQ12d. Service Performed - MeinekeQ14b. Brand In Commercial - MeinekeQ.S5c Involvement in Technology Product Purchase Decisions - ServersQB7e_18 Brand Imagery - Bacitraycin - Is good for everyone in my QB7e_19 Brand Imagery - Bacitraycin - Costs more, but is worth itQ.2d Value of newsletters - Rated on a 5 point scale from - Extremely Q.2e Company website visit for instructional resourcesQ.10X7 - Self Description (Great Escape) - Taking A Little Break Now Q.10X - Self Description - Summary Of Bottom Box (Does Not Descibe Me Q8. How would you rate your relationship with Frenkel Benefits (New c(126,127)c(134,135)c(136,137)Screener:Q.4b How often do you use these products?Q.21b Primary Server BrandIs there any generic thing that we can do sorting for some starting characters.. |
 |
|
Next Page
|