关于时间字段的转换

hy_lihuan 2008-01-06 10:57:45
cast(edittime as datetime) >='2007-05-01'这个是我一条sql语句里面的一个检索条件,
因为是维护已有的系统,内部存在大概20w条数据,edittime为varchar(50)的类型,
edittime字段里面的数据有几种形式表现:2007 8 12;2007-08-12;8 12 2007;最讨厌的是还有一批数据是 '2007-08-12 至 2007-09-23'这样的;
现在检索的时候会出现“从字符串转换为 datetime 时发生语法错误。”的情况;现在这样的检索语句要怎么样写比较好啊?一下是整个sql语句

select sum(num) as num,sum(totalpage) as totalpage,
editor from (
select d.num,d.totalpage,c.editor
from t_volumerecord c
inner join (
select a.volumeid,count(b.id) as num,max(b.pagenum2) as totalpage
from t_volumerecord a inner join t_volumeitem b
on a.volumeid = b.volumeid
group by a.volumeid
) d on c.volumeid = d.volumeid where 1=1 and c.editor='***'
and cast(f.edittime as datetime) >='2007-05-01'
and cast(f.edittime as datetime) <'2008-03-01'
) e group by editor
...全文
92 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
wzy_love_sly 2008-01-07
  • 打赏
  • 举报
回复
呵呵,没关系,一点虚拟分!
hy_lihuan 2008-01-07
  • 打赏
  • 举报
回复
ls的,不好意思已经结贴了,谢谢你
wzy_love_sly 2008-01-07
  • 打赏
  • 举报
回复
declare @tb table(dt varchar(50))
insert into @tb select '2008-01-03'
insert into @tb select '2008-01-04'
insert into @tb select '2008-01-05'
insert into @tb select '2008-01-04 至 2008-01-06'
insert into @tb select '2008-01-06'
insert into @tb select '2008-01-07'

select * from @tb
where (charindex('至',dt)=0 and dt between '2008-01-04' and '2008-01-06') or
(charindex('至',dt)>0
and (cast(left(dt,10) as datetime) between '2008-01-04' and '2008-01-06')
and (cast(right(dt,10) as datetime) between '2008-01-04' and '2008-01-06'))


2008-01-04
2008-01-05
2008-01-04 至 2008-01-06
2008-01-06

楼主双包胎?厉害啊
hy_lihuan 2008-01-07
  • 打赏
  • 举报
回复
实际的数据库内还发现了至少5种其它可是的数据,所以只能通过和业主协商处理了。。。
hy_lihuan 2008-01-07
  • 打赏
  • 举报
回复
能不能从字符串中出现'至'的地方进行处理,可是这样要进行判断才能够得到
pt1314917 2008-01-06
  • 打赏
  • 举报
回复

这样看行不?
isdate(f.edittime)>0 and cast(cast(f.edittime as varchar(10)) as datetime)

另外楼主确实强人,双胞胎,比较厉害。呵呵


dawugui 2008-01-06
  • 打赏
  • 举报
回复
只能是逐一分解串生成合理的时间格式.
dobear_0922 2008-01-06
  • 打赏
  • 举报
回复
数错了,是四种,
你可以查询一下,看看到底有多少种,实在不行,只能建个新表,把数据复制一份再处理了(有些数据可能要手动修改)

select edittime from tb
where edittime not like '% % %'
and edittime not like '%-%-%'
and edittime not like '%至%'
hy_lihuan 2008-01-06
  • 打赏
  • 举报
回复
'2007-08-12 至 2007-09-23'这样的形式也有可能'2007-8-2 至 2007-9-3'的
hy_lihuan 2008-01-06
  • 打赏
  • 举报
回复
因为数据量很大,我现阶段找到了就这三种,而且以前别人写的程序里面好像也是使用cast(f.edittime as varchar(10)) 的,听说也会出错;我倒是没有遇见,所以想看看有没有其他的方式
dobear_0922 2008-01-06
  • 打赏
  • 举报
回复
edittime字段里面的数据有几种形式表现:2007 8 12;2007-08-12;8 12 2007;最讨厌的是还有一批数据是 '2007-08-12 至 2007-09-23'这样的;

--------------
如果只有这三种形式,可以试试:
cast(cast(f.edittime as varchar(10)) as datetime)
hy_lihuan 2008-01-06
  • 打赏
  • 举报
回复
呵呵,谢谢ls的
dobear_0922 2008-01-06
  • 打赏
  • 举报
回复
老婆怀孕4个月了,去做B超,发现是双胞胎;高兴阿!!!
希望生出来是两个一摸一样的或者龙凤胎。。。
虽然还不知道出生以后怎么样养活这两个小子,看来要努力赚钱养家糊口了;
两份奶粉、两张婴儿床、每件东西都是两份的,这个分量阿!


-------------
呵呵,恭喜恭喜,,,

34,590

社区成员

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

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