关于cast函数

liu23xing 2005-12-06 11:34:07
我刚刚学delphi,请教各位高手,case函数是不是不能直接转换数据库里的表达式字段阿?像select cast(字段名 as char) from 表,出来的好像仍然是表达式,而不是表达式的值.怎样才能直接转换过来呢?
...全文
303 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
liu23xing 2005-12-08
  • 打赏
  • 举报
回复
这种方法我会的,唉,算了,就这样把
qizhanfeng 2005-12-07
  • 打赏
  • 举报
回复
那不如建个这样的存储过程
create proc p_t(@Expression varchar(20))
as
execute ('select '+@expression)
go

程序里一条一条循环取值
with adoquery1 do
begin
close;
sql.text := 'select e from table1';
open;
first;
while not eof do
begin
with adoStoredProc1 do
begin
close;
procedureName ;= 'p_t;1';
parameters.refresh;
parameters[1].value := adoquery1.FieldByName('e').asString;
Open;
showmessage(Adoquery1.Fields[0].asstring);
end;
next;
end;
end
liu23xing 2005-12-07
  • 打赏
  • 举报
回复
varchar最多只能8KB阿,我数据一多,好像就不能执行了
hmzgz81 2005-12-06
  • 打赏
  • 举报
回复
这你就得编个四则运算了,把你的运算表达式传进去,返回结果。
zhangl_cn 2005-12-06
  • 打赏
  • 举报
回复
字段a原先是什么类型的?让它自动计算好像不行
liu23xing 2005-12-06
  • 打赏
  • 举报
回复
select cast(字段a as char) from 表,像字段a有个值是0.8*100/20,sql语句取出来是0.8*100/20,如何让它取出来是值4呢?
zhangl_cn 2005-12-06
  • 打赏
  • 举报
回复
像select cast(字段名 as char) from 表

//////////////

这只是改变了字段的类型,将"字段名"转换为char类型
WangZWang 2005-12-06
  • 打赏
  • 举报
回复
--可以,
select cast(字段名 as char) as 别名
from tblname
hmzgz81 2005-12-06
  • 打赏
  • 举报
回复
楼主举个例子,没看懂
qizhanfeng 2005-12-06
  • 打赏
  • 举报
回复
--创建表
create table t(id int identity(1,1),e varchar(20))
go
--插入数据
insert into t select '0.8*100/20'
union select '0.8*100/23'
union select '0.8*100/37'
go
--创建存储过程
create proc p_t
as
declare @s varchar(8000)
select @s=' select id,e,'
select @s=@s+e+'[value] from t where id = '+convert(varchar(20),id)+' union select id,e, ' from t
exec (@s+' null from t where id=-1')
go
--测试
exec p_t
go
drop table t
drop proc p_t
/*

id e value
----------- -------------------- ------------
1 0.8*100/20 4.000000
2 0.8*100/23 3.478260
3 0.8*100/37 2.162162
*/
liu23xing 2005-12-06
  • 打赏
  • 举报
回复
我想知道能不能直接,不用过渡,不用一条一条的动态执行就得出来呢?
qizhanfeng 2005-12-06
  • 打赏
  • 举报
回复
给你个例子,要动态执行sql才行
declare @Expression varchar(20)
set @Expression='0.8*100/20'
execute ('select '+@expression)
liu23xing 2005-12-06
  • 打赏
  • 举报
回复
如果不行,那要怎么改呢
liu23xing 2005-12-06
  • 打赏
  • 举报
回复
字段a原先是char类型的阿

2,498

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 数据库相关
社区管理员
  • 数据库相关社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧