按整数大小排序包含字符串和整数的列,最后留下字符串

时间:2015-08-11 02:57:12

标签: sql sql-server sql-server-2008 coldfusion

我有一个包含数字和字符串的列,但该列的数据类型是varchar。我想以数字方式选择和排序表,字符串应该在最后。可能吗 ?它的查询是什么? 这是我的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
...

1 个答案:

答案 0 :(得分:2)

在您的订购条款中使用isnumericcase 像这样:

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函数将返回1,如果不是,则返回0,这就是您需要desc的第一部分的原因。