在SQL或C#中将vchar转换为int [英] convert from vchar to int in sql or c#

查看:362
本文介绍了在SQL或C#中将vchar转换为int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我需要在SQL查询中将vchar转换为int类型

代码

string qury = " SELECT STUDENT_NO,";
            qury += "MAX(CASE WHEN COURSE_DEPARTMENT = ''BIO'' AND COURSE_NO = 792 THEN GRADE   ELSE 0 END) AS BIO792, MAX(CASE WHEN COURSE_DEPARTMENT = ''COMM''THEN GRADE  ELSE 0 END) AS comm FROM roll GROUP BY STUDENT_NO,GRADE";
            SqlCommand cmd = new SqlCommand(qury, con);
            int x = cmd.ExecuteNonQuery();



但是当成绩= F时我有错误,它无法转换为int ??

解决方案

首先测试vchar是否为IsNumeric,如果是,则解析为int.

此处 [




然后返回F

:)


hi
i need to convert from vchar to int type in sql query

the code

string qury = " SELECT STUDENT_NO,";
            qury += "MAX(CASE WHEN COURSE_DEPARTMENT = ''BIO'' AND COURSE_NO = 792 THEN GRADE   ELSE 0 END) AS BIO792, MAX(CASE WHEN COURSE_DEPARTMENT = ''COMM''THEN GRADE  ELSE 0 END) AS comm FROM roll GROUP BY STUDENT_NO,GRADE";
            SqlCommand cmd = new SqlCommand(qury, con);
            int x = cmd.ExecuteNonQuery();



but i have eror when grade=F it cannot convert to int??

is there any method or way i can use it??

First test if the vchar IsNumeric, and if so, then Parse to int.

here[^] is a link.


hi,

i don''t think there is direct solution... because we can''t convert f to integer..
but we can either change the type of grade to character.. if that is not possible then may u can check one more condition if grade is f then return values as 1.


thank for replay

we solved the problem by put '' '' in 0 after else like

<pre lang="midl">string qury = " SELECT STUDENT_NO,";
            qury += "MAX(CASE WHEN COURSE_DEPARTMENT = ''BIO'' AND COURSE_NO = 792 THEN GRADE   ELSE 0 END) AS BIO792, MAX(CASE WHEN COURSE_DEPARTMENT = ''COMM''THEN GRADE  ELSE ''0'' END) AS comm FROM roll GROUP BY STUDENT_NO,GRADE";




then return F

:)


这篇关于在SQL或C#中将vchar转换为int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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