一个简单的日期问题?

joling123 2008-04-10 01:57:15
栏位的日期是 2008-10-10 这样的格式,但我想以年月为条件的查询应怎么设?如 只想要 2008-2 这样不要日的。谢谢! where Tdate='2008-2' ????? 谢谢!
...全文
90 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
y_dong119 2008-04-10
  • 打赏
  • 举报
回复

SQL code

select replace(convert(char(7),getdate(),120),'-0','-') /* ----------- 2008-4 */

这个最牛b,确实简单..............

conan304 2008-04-10
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 happyflystone 的回复:]
这个简单不?



SQL codeselect replace(convert(char(7),getdate(),120),'-0','-')
/*
-----------
2008-4
*/
[/Quote]
Good.
-狙击手- 2008-04-10
  • 打赏
  • 举报
回复
这个简单不?



select replace(convert(char(7),getdate(),120),'-0','-')
/*
-----------
2008-4
*/
conan304 2008-04-10
  • 打赏
  • 举报
回复
--栏位的日期是 2008-10-10 这样的格式,但我想以年月为条件的查询应怎么设?
--如 只想要 2008-2 这样不要日的。谢谢! where Tdate='2008-2' ????? 谢谢!


select convert(char(7),getdate(),120)

select cast(year(getdate()) as char(4))+'-'+cast(month(getdate()) as varchar(2))

/*
-------
2008-04

(所影响的行数为 1 行)


-------
2008-4

(所影响的行数为 1 行)
*/
iou1220 2008-04-10
  • 打赏
  • 举报
回复
declare @t table(AA datetime,BB char(1))
insert @t select '2008-01-03','C'
union all select '2008-02-03 02:03:55','B'
union all select '2008-03-03 02:03:55','A'
union all select '2008-01-03 02:03:55','C'
union all select '2008-02-03 02:03:55','B'
union all select '2008-03-03 02:03:55','A'


select t1.* from (
select *,SUBSTRING(CONVERT(varchar(10),AA, 20), 1, 4)+
SUBSTRING(CONVERT(varchar(10),AA, 20), 6, 2) as CC
from @t
) t1 where CC='200803'


结果:
AA BB CC
--------------------------------------
2008-03-03 02:03:55.000 A 200803
2008-03-03 02:03:55.000 A 200803

ojuju10 2008-04-10
  • 打赏
  • 举报
回复

或者

select * from tab
where datediff(mm,Tdate,'2008-02')=0

ojuju10 2008-04-10
  • 打赏
  • 举报
回复

select * from tab
where convert(varchar(7),Tdate,120)='2008-02'
wzy_love_sly 2008-04-10
  • 打赏
  • 举报
回复
单月要2位 如'09','12'
wzy_love_sly 2008-04-10
  • 打赏
  • 举报
回复
convert (varchar(7),tdate,120)='2008-02'
bqb 2008-04-10
  • 打赏
  • 举报
回复
select convert(char(7),getdate(),120)

---------
2008-04

34,593

社区成员

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

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