char转换成datetime问题

mythqxh 2008-10-21 02:36:32
现在我的字段date是char类型。里面的数据如下
2008-2-3
2008-5-10

我想用SQL语句把它转换成2008-02-03这种类型的格式。然后再去做比较!
想要的SQL语句是 select date from table where date='2008-02-03' 想让这句话能相等!
...全文
186 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
ChinaJiaBing 2008-10-21
  • 打赏
  • 举报
回复

select date from table where covert(nvarchar(10),date,120)='2008-02-03'
super_iven 2008-10-21
  • 打赏
  • 举报
回复
select datename(MM,'2008-02-03')
mythqxh 2008-10-21
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 fcuandy 的回复:]
SQL codeselect date from table where date=convert(varchar(10),cast('2008-2-3' as datetime),120)
[/Quote]


解决,谢谢。
顺便说下 convert(char(10),date,120) 获取的值 不会在前面加0的!
dlpseeyou 2008-10-21
  • 打赏
  • 举报
回复
where convert(char(10),date,120)='2008-02-03'
fcuandy 2008-10-21
  • 打赏
  • 举报
回复
select date from table where date=convert(varchar(10),cast('2008-2-3' as datetime),120)
muzhenxing013 2008-10-21
  • 打赏
  • 举报
回复
select DATENAME(YEAR,'2008-9-2')+'-'+DATENAME(MONTH,'2008-9-2')+'-0'+DATENAME(DAY,'2008-9-2')
mengxiqu 2008-10-21
  • 打赏
  • 举报
回复
select date from table where convert(char(10),date,120)='2008-02-03'
fcuandy 2008-10-21
  • 打赏
  • 举报
回复
select date from table where date=convert(varchar(10),'2008-2-3',120)

如果你的字段中值存的有时分秒,只比较日,那么
select date from table where datediff(dd,date,'2008-2-3')=0
mythqxh 2008-10-21
  • 打赏
  • 举报
回复
数据库是SQL2000

CONVERT(varchar(10) ,'2008-9-2', 120)
这句获得的值还是 2008-9-2 不会添加0

34,587

社区成员

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

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