按整数的大小对包含字符串和整数的列进行排序,将字符串留在末尾

Order a column with strings and integers by the size of integers, leaving strings at the end

我有一列包含数字和字符串,但该列的数据类型是 varchar。我想 select 并按数字顺序排列 table,字符串应该在最后。可能吗 ?它的查询是什么? 这是我的 ColdFusion 查询:

<cfquery name="qManpowerData" datasource="#REQUEST.DSN#" result="queryGrade">
                select tdep.position_name_#Application.stLang.DEFLANGFIELD# as dept_name,tdep.position_id as dept_id
                    ,tdiv.position_name_#Application.stLang.DEFLANGFIELD# as div_name,tdiv.position_id as div_id
                    <cfif request.dbdriver eq "ORACLE">
                    <!--- ,GETGRADE(level_code) ---> 
                    ,thrmempcompany.level_code as grade,
                    <cfelse>
                    <!--- ,dbo.GETGRADE(level_code) ---> 
                    ,thrmempcompany.level_code as grade,
                    </cfif>


                    count(thrmempcompany.emp_id) as cnt
                from   (select emp_id, newposition_id position_id,NEW_STRATA_NAME as level_code ,effective_date startdate from thrmemploymenthistory
                        where 
                        (end_date >= #CreateODBCDate(dateadd("d",-1,nextMonth))# or end_date is null) 
                        and effective_date < #CreateODBCDate(nextMonth)#
                        and CAREERTRANSITION_CODE <> 'TERMINATION'
                    ) thrmempcompany,thrmposition,thrmposition tdiv,thrmposition tdep
                where thrmempcompany.position_id=thrmposition.position_id
                      and thrmposition.division_id=tdiv.position_id
                      and ( (   tdiv.organization_level = 'SEC'
                                and tdep.organization_level = 'DEP'

                            <cfif request.dbdriver eq "ORACLE">
                            and ','||tdiv.position_parentpath||',' like '%,'||tdep.position_id||',%'    )   
                                <cfelse>
                                    and ','+tdiv.position_parentpath+',' like '%,'+Ltrim(Rtrim(str(tdep.position_id)))+',%' )
                            </cfif>

                                OR
                            (   tdiv.organization_level = 'BOD' and tdiv.position_id=tdep.position_id   )
                                OR
                            (   tdiv.organization_level = tdep.organization_level and tdiv.position_id=tdep.position_id )
                        )
                      and thrmposition.company_id=#cookie.company_id#
                      <cfif rdoView eq "Sect" and len(listSect)>
                        AND TDiv.position_id IN (#replace(listSect,"~",",","ALL")#)
                      <cfelseif len(listDept)>
                        AND TDep.position_id IN (#replace(listDept,"~",",","ALL")#)
                      <cfelse>
                        AND 1=0
                      </cfif>
                      and level_code  <> ''
                group by tdep.position_name_#Application.stLang.DEFLANGFIELD#,tdep.position_id
                    ,tdiv.position_name_#Application.stLang.DEFLANGFIELD#,tdiv.position_id
                    ,tdiv.position_parentpath,
                    <cfif request.dbdriver eq "ORACLE">
                    <!--- GETGRADE(level_code) --->
                    thrmempcompany.level_code
                    <cfelse>
                    <!--- dbo.GETGRADE(level_code) --->
                    thrmempcompany.level_code
                    </cfif>
                order by
                    CAST(thrmempcompany.level_code AS INTEGER)
                    ,dept_name,tdiv.position_parentpath,div_name
            </cfquery>
            <cfif request.dbdriver eq "ORACLE">
                <cfquery name="qGetStrata" datasource="#attributes.DSN#">
                    SELECT  DISTINCT <!--- GETGRADE(LevelCode) AS---> LevelCode 
                    FROM    THRMPayLevel
                    Where Company_id = '#Cookie.Company_id#'
                </cfquery>
            <cfelse>
                <cfquery name="qGetStrata" datasource="#attributes.DSN#">
                    SELECT  DISTINCT <!--- dbo.GETGRADE(LevelCode) AS ---> LevelCode 
                    FROM    THRMPayLevel
                    Where Company_id = '#Cookie.Company_id#'
                </cfquery>
            </cfif>

结果是: 1 10 11 12 13 .... 2 3 ... GRDIRECTOR

我希望成绩排序像

GRDIRECTOR
1
2
3
...

在您的 order by 条款中使用 isnumeric and case
像这样:

order by isnumeric(thrmempcompany.level_code) desc, 
         case when isnumeric(thrmempcompany.level_code) then
              cast(thrmempcompany.level_code AS int)
         else
              thrmempcompany.level_code
         end,
         dept_name, 
         tdiv.position_parentpath, 
         div_name

如果值为数字,isnumeric 函数将 return 1,否则为 0,这就是为什么您需要第一部分 desc