如何在SQL Server 2008中将String转换为Int? [英] How to convert String to Int in SQL Server 2008?

查看:493
本文介绍了如何在SQL Server 2008中将String转换为Int?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在SQL Server 2008中将字符串值转换为整数值。

我正在尝试这样的



I want to convert string value into integer value in SQL Server 2008.
I'm trying like this

set @Position = CAST(select MAX(position) from uniquesponsors where sponsorid=@SponsorId as int)
if @Position is null
    begin
        insert into uniquesponsors (sponsorid,position)
        values(@SponsorId,1)
    end
else if @Position is not null
    begin
        insert into uniquesponsors (sponsorid,position)
        values(@SponsorId,@Position+1)
    end
end





但是我得到错误怎么能解决这个错误?





but it's getting error how can I fix this error please?

Msg 156, Level 15, State 1, Procedure AddUniqueSponsors, Line 10
Incorrect syntax near the keyword 'select'.
Msg 156, Level 15, State 1, Procedure AddUniqueSponsors, Line 10
Incorrect syntax near the keyword 'as'.



推荐答案

这将解决问题:

This will do the trick :
SELECT CAST(YourVarcharCol AS INT) FROM Table


Select @Position = CAST(MAX(position) as int) from uniquesponsors where sponsorid=@SponsorId

if @Position is null
    Insert into uniquesponsors (sponsorid,position)
    values(@SponsorId,1)
Else
    insert into uniquesponsors (sponsorid,position)
    values(@SponsorId,@Position+1)


不确定为什么要尝试这样转换...但是下面给出了更正的查询 -



设置@Position = CAST((选择MAX(位置)来自uniquesponsors,其中sponsorid = @ SponsorId)AS INT)
Not sure why you are trying to convert like this... but the corrected query is given below-

set @Position =CAST((Select MAX(position) from uniquesponsors where sponsorid=@SponsorId) AS INT)


这篇关于如何在SQL Server 2008中将String转换为Int?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆