zoukankan      html  css  js  c++  java
  • sql中,把varchar类型转换成int型,在进行排序

    为了实现varchar型的字段序号递增,我每次插入数据要先找出数据库中序号最大的那个再加1,可是对  
    varchar进行order   by   时,得不到正确结果,如   varchar里,‘100’<'99'  
    怎么实现转化  
    另外,我只比较varchar的后几位,那个怎么截取后几位再进行int转化再排序呢?   

    回答1:
    select   *   from   yourtable   order   by   cast(youcol   as   int)

    回答2:
    order   by   convert(int,   字段)

    回答3:
    但是你的列里面只能是数字组成的字符,要不然会出错

    回答4:
    create   table   #t(  
    b   int   identity(1,1),  
    a   varchar(3)  
    )  
    insert   into   #t   select   '003'  
    insert   into   #t   select   '002'  
    insert   into   #t   select   '001'  
    insert   into   #t   select   '010'  
    insert   into   #t   select   '023'  
    --insert   into   #t   select   'a03'  
       
       
    select   *   from   #t   order   by   cast(a   as   int)  
       
    drop   table   #t

    回答5:
    declare   @maxid   numeric(8)  
       
    select   @maxid=max(isnull(cast(right(字段,x)   as   numeric),0)+1)   
    x表示从后面取多少位。 x表示从后面取多少位。 x表示从后面取多少位。


  • 相关阅读:
    Lombok 安装、入门
    Centos 6.5中使用yum安装jdk
    Mysql规范和使用注意点(转)
    Java编码规范
    windows2016重新配置sid
    Postman POST接口请求无法获取数据
    微信修改密码
    SQL事务
    .net时间格式化
    SQL查询列是否有重复值
  • 原文地址:https://www.cnblogs.com/yeye518/p/2231665.html
Copyright © 2011-2022 走看看