SQL Server : converting varchar to INT

后端 未结 4 2040
长情又很酷
长情又很酷 2021-02-06 22:14

I am stuck on converting a varchar column UserID to INT. I know, please don\'t ask why this UserID column was not created as

4条回答
  •  甜味超标
    2021-02-06 22:36

    I would try triming the number to see what you get:

    select len(rtrim(ltrim(userid))) from audit
    

    if that return the correct value then just do:

    select convert(int, rtrim(ltrim(userid))) from audit
    

    if that doesn't return the correct value then I would do a replace to remove the empty space:

     select convert(int, replace(userid, char(0), '')) from audit
    

提交回复
热议问题