大家帮我看个SQL2008字符串转换时间问题。执行提示从字符串转换日期和/或时间时,转换失败。在线等啊!

a312991915 2011-03-16 01:49:46
ALTER proc [dbo].[Sp_tj_Sjtj]
(
@Communityid varchar(10),
@type varchar(5) ,
@Stime datetime, --开始时间
@Etime datetime --截止时间
)
as

begin
declare @strsql varchar(8000),
@name varchar(20),
@where varchar(50)
if @Communityid='0' or @Communityid='' or @Communityid =null or @Communityid is null
set @Communityid ='null'
IF @Stime = ''
SET @Stime = NULL
IF @Etime = ''
SET @Etime = NULL
if(@type='1')
begin
set @name='TypeName'
set @where='t.Typeid';
end
else if(@type='2')
begin
set @name='FlowCategoryName'
set @where='fc.FlowCategoryid';
end
else
begin
set @name='FlowName'
set @where='f.Flowid';
end


set @strsql ='

select isnull( '+@name+',''未知'') name ,COUNT(cid) as ''合计'','+@where+' id from tblCase c
left join tblFlow f on c.Flowid=f.Flowid
left join tblFlowCategory fc on f.FlowCategoryid =fc.FlowCategoryid
left join tblType t on fc.typeid=t.Typeid where
(CommunityID='+@Communityid+' or '+@Communityid+' is null)
and (CONVERT(varchar(100),c.HappenTime,23)>='+@Stime+' or '+@Stime+' is null)
and (CONVERT(varchar(100),c.HappenTime,23)<='+@Etime+' or '+@Etime+' is null)
group by '+@where+','+@name+'

order by ''合计'' desc'
exec(@strsql)

end


我感觉就是拼字符串那有问题啊。大家帮帮忙啊。
...全文
1817 24 打赏 收藏 转发到动态 举报
写回复
用AI写文章
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
网瘾大叔 2011-03-16
  • 打赏
  • 举报
回复
哇!高人很多啊,有点乱。
a312991915 2011-03-16
  • 打赏
  • 举报
回复
[Quote=引用 22 楼 acherat 的回复:]
看19楼的!或者:


set @strsql ='

select isnull( '+@name+',''未知'') name ,COUNT(cid) as ''合计'','+@where+' id from tblCase c
left join tblFlow f on c.Flowid=f.Flowid
left join tblFlowCategor……
[/Quote]
谢谢了。
AcHerat 元老 2011-03-16
  • 打赏
  • 举报
回复
看19楼的!或者:


set @strsql ='

select isnull( '+@name+',''未知'') name ,COUNT(cid) as ''合计'','+@where+' id from tblCase c
left join tblFlow f on c.Flowid=f.Flowid
left join tblFlowCategory fc on f.FlowCategoryid =fc.FlowCategoryid
left join tblType t on fc.typeid=t.Typeid where
(CommunityID='+@Communityid+' or '+@Communityid+' is null)
and c.HappenTime>='''+convert(varchar(10),@stime,120)+''' or '''+convert(varchar(10),@stime,120)+''' is null)
and c.HappenTime<='''+convert(varchar(10),@Etime,120)+''' or '''+convert(varchar(10),@Etime,120)+''' is null)
group by '+@where+','+@name+'

order by ''合计'' desc'
a312991915 2011-03-16
  • 打赏
  • 举报
回复
[Quote=引用 18 楼 acherat 的回复:]
set @strsql ='

select isnull( '+@name+',''未知'') name ,COUNT(cid) as ''合计'','+@where+' id from tblCase c
left join tblFlow f on c.Flowid=f.Flowid
left join tblFlowCategory fc on f.FlowC……
[/Quote]
语句不报错了,但是查不出来结果。。
这样就可以查出来。
select * from tblCase c
left join tblFlow f on c.Flowid=f.Flowid
left join tblFlowCategory fc on f.FlowCategoryid =fc.FlowCategoryid
left join tblType t on fc.typeid=t.Typeid where
(CommunityID=2 or Communityid is null)
and (CONVERT(varchar(100),c.HappenTime,23)>='2011-03-01' or '+@Stime+' is null)
and (CONVERT(varchar(100),c.HappenTime,23)<='2011-03-05' or '+@Etime+' is null)
cutebear2008 2011-03-16
  • 打赏
  • 举报
回复
强烈的建议你print @sql出来看看!
大牛们都在等呢!
AcHerat 元老 2011-03-16
  • 打赏
  • 举报
回复
最简单的,改这里!


ALTER proc [dbo].[Sp_tj_Sjtj]
(
@Communityid varchar(10),
@type varchar(5) ,
@Stime varchar(19), --开始时间
@Etime varchar(19) --截止时间
)
as
AcHerat 元老 2011-03-16
  • 打赏
  • 举报
回复

set @strsql ='

select isnull( '+@name+',''未知'') name ,COUNT(cid) as ''合计'','+@where+' id from tblCase c
left join tblFlow f on c.Flowid=f.Flowid
left join tblFlowCategory fc on f.FlowCategoryid =fc.FlowCategoryid
left join tblType t on fc.typeid=t.Typeid where
(CommunityID='+@Communityid+' or '+@Communityid+' is null)
and (CONVERT(varchar(100),c.HappenTime,23)>='''+convert(varchar(100),@stime,23)+''' or '''+convert(varchar(100),@stime,23)+''' is null)
and (CONVERT(varchar(100),c.HappenTime,23)<='''+convert(varchar(100),@Etime,23)+''' or '''+convert(varchar(100),@Etime,23)+''' is null)
group by '+@where+','+@name+'

order by ''合计'' desc'
a312991915 2011-03-16
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 acherat 的回复:]
ALTER proc [dbo].[Sp_tj_Sjtj]
(
@Communityid varchar(10),
@type varchar(5) ,
@Stime datetime, --开始时间
@Etime datetime --截止时间
)
as


and (CONVERT(varchar(100),c.HappenTime,23)>='+@St……
[/Quote]
谢谢你。。但还是不行。。我有的不是拼的SQL语句,这么写都没问题。就是拼语句的问题。
快溜 2011-03-16
  • 打赏
  • 举报
回复
#1,你拼接字符串,需要把datetime类型转为字符串
#2,就你写的这个set @Etime=null 就算转换为了字符串再拼接起来,你的@strsql也是null

你可以print出来看看
AcHerat 元老 2011-03-16
  • 打赏
  • 举报
回复
and (CONVERT(varchar(100),c.HappenTime,23)>='''+@Stime+''' or '''+@Stime+''' is null)
and (CONVERT(varchar(100),c.HappenTime,23)<='''+@Etime+''' or '''+@Etime+''' is null)

??????
王向飞 2011-03-16
  • 打赏
  • 举报
回复
convert(,,120)



100 或者 0 mon dd yyyy hh:miAM (或者 PM)
101 mm/dd/yy
102 yy.mm.dd
103 dd/mm/yy
104 dd.mm.yy
105 dd-mm-yy
106 dd mon yy
107 Mon dd, yy
108 hh:mm:ss
109 或者 9 mon dd yyyy hh:mi:ss:mmmAM(或者 PM)
110 mm-dd-yy
111 yy/mm/dd
112 yymmdd
113 或者 13 dd mon yyyy hh:mm:ss:mmm(24h)
114 hh:mi:ss:mmm(24h)
120 或者 20 yyyy-mm-dd hh:mi:ss(24h)
121 或者 21 yyyy-mm-dd hh:mi:ss.mmm(24h)
126 yyyy-mm-ddThh:mm:ss.mmm(没有空格)
130 dd mon yyyy hh:mi:ss:mmmAM
131
a312991915 2011-03-16
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 wxf163 的回复:]
哪有你这么用的


试过了,不行。

SQL code
select isnull( @name,'未知') name ,COUNT(cid) as '合计',@where id from tblCase c
left join tblFlow f on c.Flowid=f.Flowid
left join tblFlowCategory fc on f.FlowCategoryid……
[/Quote]
试过了,不行。去了也是这样
AcHerat 元老 2011-03-16
  • 打赏
  • 举报
回复
ALTER proc [dbo].[Sp_tj_Sjtj]
(
@Communityid varchar(10),
@type varchar(5) ,
@Stime datetime, --开始时间
@Etime datetime --截止时间
)
as


and (CONVERT(varchar(100),c.HappenTime,23)>='+@Stime+' or '+@Stime+' is null)
and (CONVERT(varchar(100),c.HappenTime,23)<='+@Etime+' or '+@Etime+' is null)


1# 既然定义了两个变量是datetime型,就不要用 convert 转换了
2# 存储过程里的判断可以去掉!
3#

and c.HappenTime>='+@Stime+' or '+@Stime+' is null)
and c.HappenTime<='+@Etime+' or '+@Etime+' is null)
王向飞 2011-03-16
  • 打赏
  • 举报
回复
(CommunityID=@Communityid or @Communityid is null) --这也改掉
--小F-- 2011-03-16
  • 打赏
  • 举报
回复
print出来看看

把PRINT的语句贴出来我们看看
王向飞 2011-03-16
  • 打赏
  • 举报
回复
哪有你这么用的



  select isnull( @name,'未知') name ,COUNT(cid) as '合计',@where id from tblCase c
left join tblFlow f on c.Flowid=f.Flowid
left join tblFlowCategory fc on f.FlowCategoryid =fc.FlowCategoryid
left join tblType t on fc.typeid=t.Typeid where
(CommunityID='+@Communityid+' or '+@Communityid+' is null)
and (CONVERT(varchar(100),c.HappenTime,23)>=@Stime or @Stime is null)
and (CONVERT(varchar(100),c.HappenTime,23)<=@Etime or @Etime is null)
group by @where,@name
order by '合计' desc

不用exec
a312991915 2011-03-16
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 xiao_ai_mei 的回复:]
什么错误提示
[/Quote]
从字符串转换日期和/或时间时,转换失败。
a312991915 2011-03-16
  • 打赏
  • 举报
回复
大家看看怎么改啊。。。别告诉我哪里有问题。。求教了。
a312991915 2011-03-16
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 haiwer 的回复:]
IF @Stime = ''
-->
IF @Stime is nul

@Stime是datetime类型,不能 = ''
[/Quote]
试过了,不是这里问题。
昵称被占用了 2011-03-16
  • 打赏
  • 举报
回复
and (CONVERT(varchar(100),c.HappenTime,23)>='+@Stime+' or '+@Stime+' is null)
and (CONVERT(varchar(100),c.HappenTime,23)<='+@Etime+' or '+@Etime+' is null)


这里有问题
加载更多回复(4)

34,576

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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