当前位置:首页 » 服务存储 » 存储过程类型转换
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

存储过程类型转换

发布时间: 2022-08-20 11:14:42

Ⅰ 在存储过程中,如何把smallint类型转换成char类型

Qt下面,字符串都用QString,确实给开发者提供了方便,想想VC里面定义的各种变量类型,而且函数参数类型五花八门,经常需要今年新那个类型转换
Qt再使用第三方开源库时,由于库的类型基本上都是标准的类型,字符串遇的多的就是Char*类型
在Qt下怎样将QString转char*呢,需要用到QByteArray类,QByteArray类的说明详见Qt帮助文档。
因为char*最后都有一个‘/0’作为结束符,而采用QString::toLatin1()时会在字符串后面加上‘/0’
方法如下:
Qstring str;
char* ch;
QByteArray ba = str.toLatin1();
ch=ba.data();
这样就完成了QString向char*的转化。经测试程序运行时不会出现bug
注意第三行,一定要加上,不可以str.toLatin1().data()这样一部完成,可能会出错。

补充:以上方法当QString里不含中文时,没有问题,但是QString内含有中文时,转换为char*就是乱码,采用如下方法解决:
方法1:
添加GBK编码支持:
#include <QTextCodec>
QTextCodec::setCodecForTr(QTextCodec::codecForName("GBK"));
QTextCodec::setCodecForLocale(QTextCodec::codecForName("GBK"));
然后改变上面的第三行为:QByteArray ba = str.toLoacl8Bit(); toLoacl8Bit支持中文
方法2:
先将QString转为标准库中的string类型,然后将string转为char*,如下:
std::string str = filename.toStdString();
const char* ch = str.c_str();

sql server 存储过程varchar到int类型转换

/*
存储过程的return只能返回int类型的数据,并且只这样接收返回值的
declare@Aint
Exec@A=P_判断是否闰年
select@A
*/

--你可以改成这样
alterprocP_判断是否闰年
as
begin
declare@时间int
set@时间=datepart(year,'2000-02-01')
selectCASEWHEN(@时间%4=0AND@时间%100<>0)or(@时间%400=0)then'是闰年'
else'不是闰年'
end
end

--也可以这样
alterprocP_判断是否闰年(@RstVarchar(10)output)
as
begin
declare@时间int
set@时间=datepart(year,'2000-02-01')

Set@Rst=CASEWHEN(@时间%4=0AND@时间%100<>0)or(@时间%400=0)then'是闰年'
else'不是闰年'
end

end

/*
declare@Avarchar(10)
ExecP_判断是否闰年@Aoutput
select@A
*/

Ⅲ 如何对存储过程中的变量进行类型转换

/*负债表中未分配利润(管理费用)期初数*/if @yue=01beginselect @fzglqc1=sum(feiyong) from hqhesuan whereyear=@nian-1and (month<='12')set @fzglfyqc=@fzglqc1endelsebeginselect @fzglqc=sum(feiyong) from hqhesuan where year=@nianand (month<=@yue-1)set @fzglfyqc=@fzglqcend象上面的一段代码中@YUE为字符变量,要进行@yue-1的运算时需要转换成整数,如何做??

Ⅳ 如何在sql server存储过程中转化数据类型

不一定非要在存储过程中转换,用普通的sql语句就可以,一般用cast函数。

测试方法:

创建表及插入数据:

createtabletest
(idint,
starttimevarchar(20));

insertintotestvalues(1,'2015-07-11');

将starttime字段转化为datetime类型:

selectcast(starttimeasdatetime)fromtest;

结果如图,这时,starttime就转成了datetime类型:

Ⅳ SqlServer 中存储过程:money类型与char类型的转换问题

自己写的么?
1有些地方没必要4个单引号,3个就行,在sql中字符串的拼接语句中要表示1个单引号就得用两个单引号,前一个表示转义的意思,编程语言都有转义一说
2因为是字符串拼接的,而@FormerPrice这些是money类型的,如果不转换,就默认要把字符串转换成money类型了,就会出错。所以得把@FormerPrice之类的转换成字符类型的,存储过程修改如下
ALTER
PROCEDURE
[dbo].[SearchMerchandise]
@Category
varchar(50),
@Brand
varchar(50),
@Type
varchar(50),
@FormerPrice
money,
@LatterPrice
money
AS
BEGIN
declare
@QueryString
varchar(100)
set
@QueryString
=
'select
*
from
Merchandise
where
Category
=
'''
+
@Category
+
'''
and
Brand
=
'''
+
@Brand
+
'''
'
SET
NOCOUNT
ON;
if
@Type<>''
set
@QueryString
=
@QueryString+'
and
Type='''
+@Type+
''''
if
@FormerPrice<>''
and
@LatterPrice<>''
set
@QueryString
=
@QueryString+'
and
PriceNow
between
'''+
convert(varchar(20),@FormerPrice)
+
'''
and
'''
+
convert(varchar(20),@LatterPrice)
+
''''
if
@FormerPrice<>''
and
@LatterPrice=''
set
@QueryString
=
@QueryString+'
and
PriceNow
>='''
+
convert(varchar(20),@FormerPrice)
+
''''
if
@FormerPrice=''
and
@LatterPrice<>''
set
@QueryString
=
@QueryString+'
and
PriceNow
<='''
+
convert(varchar(20),@LatterPrice)
+
''''
exec(@QueryString)
END
因为没有表结果不能进行测试,所以可能会出错,如果出错就把表结果发一下,进行测试

Ⅵ oracle 存储过程转换类型问题。

你的to_date(''||2010-12-30||'' ,'yyyy-MM-dd'),函数写错了, 因为减号的优先级比||高,''||2010-12-30||''得到的是一个4位数, 写成下面这样就行了
to_date('2010-12-1' ,'yyyy-MM-dd'), 还有这样写虽然oracle写可以, 年月最好写的规范一点,写成to_date('2010-12-01' ,'yyyy-MM-dd')

Ⅶ 存储过程 string 类型 怎么转换成 int

在C#里?
string str="12345";
int number = int.Parse(str);

Ⅷ 存储过程类型转换

自己写的么?
1有些地方没必要4个单引号,3个就行,在sql中字符串的拼接语句中要表示1个单引号就得用两个单引号,前一个表示转义的意思,编程语言都有转义一说
2因为是字符串拼接的,而@FormerPrice这些是money类型的,如果不转换,就默认要把字符串转换成money类型了,就会出错。所以得把@FormerPrice之类的转换成字符类型的,存储过程修改如下
ALTER PROCEDURE [dbo].[SearchMerchandise]
@Category varchar(50),
@Brand varchar(50),
@Type varchar(50),
@FormerPrice money,
@LatterPrice money
AS
BEGIN
declare @QueryString varchar(100)
set @QueryString = 'select * from Merchandise where Category = ''' + @Category + ''' and Brand = ''' + @Brand + ''' '
SET NOCOUNT ON;

if @Type<>''
set @QueryString = @QueryString+' and Type=''' +@Type+ ''''
if @FormerPrice<>'' and @LatterPrice<>''
set @QueryString = @QueryString+' and PriceNow between '''+ convert(varchar(20),@FormerPrice) + ''' and ''' + convert(varchar(20),@LatterPrice) + ''''
if @FormerPrice<>'' and @LatterPrice=''
set @QueryString = @QueryString+' and PriceNow >=''' + convert(varchar(20),@FormerPrice) + ''''
if @FormerPrice='' and @LatterPrice<>''
set @QueryString = @QueryString+' and PriceNow <=''' + convert(varchar(20),@LatterPrice) + ''''

exec(@QueryString)
END
因为没有表结果不能进行测试,所以可能会出错,如果出错就把表结果发一下,进行测试