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表示从后面取多少位。


  • 相关阅读:
    Object类中常见的方法,为什么wait notify会放在Object里边
    什么是Java序列化和反序列化,如何实现Java序列化
    hashCode和equals方法的区别与联系
    MVC设计思想
    dstat命令--检查linux系统性能
    linux mint19.1解决网易云音乐安装后打不开的问题
    linux 修改用户密码
    MD5加密
    xmpp
    cocoapods安装
  • 原文地址:https://www.cnblogs.com/yeye518/p/2231665.html
Copyright © 2011-2022 走看看