导航
  • 主页
  • 基础类
  • 应用实例
  • 新技术前沿

关于时间数据处理的问题

dgz988 2007-12-28 03:08:40
各位老大请帮忙:
在数据库内有一列数据: abc070805
前面三位是随机,后面是日期数据,这样我可以用SUBSTRING取出日期数据070805,我想在它的基础上推算两年后的日期,可否用SQL来实现啊?

即用语句实现出来的结果是:090805

谢谢了。
...全文
51 点赞 收藏 11
写回复
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
dgz988 2007-12-28
明白。谢谢老大。嘿嘿。。
回复
popeye627 2007-12-28
Select substring(CONVERT(varchar(30),dateadd(year,2,cast(right('abc070805',6)as datetime)),112),3,6) result

result
------------
090805

(1 個資料列受到影響)

---dateadd的第二个参数2,表2年
回复
popeye627 2007-12-28
Select substring(CONVERT(varchar(30),dateadd(year,2,cast(right('abc070805',6)as datetime)),112),3,6) result

result
------------
090805

(1 個資料列受到影響)

---dateadd的第二个参数2,表2年
回复
popeye627 2007-12-28
不好意思,写成Oracle的了,呵呵 LZ还是参照乌龟的吧
回复
dgz988 2007-12-28
感动中。。一个贴子这么多兄弟观注。
非常感谢!!!我试一下。
回复
popeye627 2007-12-28
select to_char(ADD_MONTHS(to_date(substr('abc070805',4,6),'yymmdd'),24),'yymmdd') from dual
---24:2年24个月

---------查询结果-------------
1 090805
回复
popeye627 2007-12-28
select to_char(ADD_MONTHS(to_date(substr('abc070805',4,6),'yymmdd'),24),'yymmdd') from dual
回复
dawugui 2007-12-28
declare @s as varchar(20)
set @s = 'abc070805'

select result =
right(datename(year,(dateadd(year,2,cast(right(@s,6) as datetime)))),2)
+ right('00'+datename(mm,(dateadd(year,2,cast(right(@s,6) as datetime)))),2)
+ right('00'+datename(day,(dateadd(year,2,cast(right(@s,6) as datetime)))),2)

/*
result
--------------------------------------------------------------------
090805

(1 行受影响)
*/

回复
dawugui 2007-12-28
declare @s as varchar(20)
set @s = 'abc070805'

select result = left(@s,3) +
right(datename(year,(dateadd(year,2,cast(right(@s,6) as datetime)))),2)
+ right('00'+datename(mm,(dateadd(year,2,cast(right(@s,6) as datetime)))),2)
+ right('00'+datename(day,(dateadd(year,2,cast(right(@s,6) as datetime)))),2)

/*
result
--------------------------------------------------------------------
abc090805

(1 行受影响)
*/

回复
dawugui 2007-12-28
declare @s as varchar(20)
set @s = 'abc070805'

select result = left(@s,3) +
right(datename(year,(dateadd(year,2,cast(right(@s,6) as datetime)))),2)
+ right('00'+datename(mm,(dateadd(year,2,cast(right(@s,6) as datetime)))),2)
+ right('00'+datename(day,(dateadd(year,2,cast(right(@s,6) as datetime)))),2)

/*
result
--------------------------------------------------------------------
abc09085

(1 行受影响)
*/
回复
dawugui 2007-12-28
declare @s as varchar(20)
set @s = 'abc070805'

select result = left(@s,3) +
right(datename(year,(dateadd(year,2,cast(right(@s,6) as datetime)))),2)
+ datename(mm,(dateadd(year,2,cast(right(@s,6) as datetime))))
+ datename(day,(dateadd(year,2,cast(right(@s,6) as datetime))))

/*
result
--------------------------------------------------------------------
abc09085

(1 行受影响)
*/

回复
发动态
发帖子
MS-SQL Server
创建于2007-09-28

3.2w+

社区成员

MS-SQL Server相关内容讨论专区
申请成为版主
社区公告
暂无公告