这句sql应该怎么写?为什么查不出值来?

blurxx 2006-12-10 11:24:54
首先orders表的begintime字段为datetime类型,且有条记录的begintime是2006-12-10 20:38:05
用SQL语句:
select * from orders where begintime= Cast('2006-12-10 20:38:05' as datetime )
这句查值为0记录

select * from orders where begintime=convert('2006-12-10 20:38:05','YYYY-MM-DD HH:MI:SS')
这句提示错误:'2006-12-10 20:38:05' 附近有语法错误

谢谢解答!
...全文
272 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
mysummer2003 2006-12-13
  • 打赏
  • 举报
回复
select * from orders where convert(varchar(19),begintime,120)='2006-10-09 20:00:00'
dxtaxlyh 2006-12-11
  • 打赏
  • 举报
回复
select * from orders where begintime='2006-12-10 20:38:05'
studenthj 2006-12-11
  • 打赏
  • 举报
回复
看看你在建表时是不是建立了什么约束,当你插入时应该用with noncheck
jackiecheng001 2006-12-11
  • 打赏
  • 举报
回复
select * from orders where begintime=convert( datetime,'2006-12-10 20:38:05','YYYY-MM-DD HH:MI:SS')
convert(类型,变量,格式)
格式用 120代表yyyy-mm-dd
还有好几个 103 ..

类型可以有程度的限制,这样可以转化为日期时间或则是日期
varchar(9)
caixia615 2006-12-11
  • 打赏
  • 举报
回复
已经是datetime 类型了那就没必要再转换了..
dawugui 2006-12-11
  • 打赏
  • 举报
回复
select * from orders where begintime = '2006-12-10 20:38:05'

or

select * from orders where convert(varchar(19),begintime,120) = '2006-12-10 20:38:05'
blurxx 2006-12-11
  • 打赏
  • 举报
回复
谢谢刚才回答问题的几位朋友,问题没有解决,但我有一些发现:因为我数据库里面的数据是程序插入的,插入后的数据,用select * from orders显示的数据均是这样的时间2006-12-09 20:51:19.517,就是说后面都带有毫秒吧,当我手动插入数据的时候select显示其记录,其记录时间的毫秒为000,用
select * from orders where begintime=convert(datetime,'2006-12-10 20:38:05',120)
可找到插入的数据,而毫秒不为整000的数据用上面这样的SQL语句查询均无法显示。

楼上的朋友说120代表yyyy-mm-dd,如果像是我这样带毫秒的数据应该怎么查?还有,如果用
select * from orders where begintime=convert( datetime,'2006-12-10 20:38:05','YYYY-MM-DD HH:MI:SS')这样的SQL语句,sql server提示错误:函数 convert 的参数 3 的数据类型 varchar 无效。
sgucxc0 2006-12-11
  • 打赏
  • 举报
回复

create table orders (id smallint identity(1,1),begintime datetime)

insert into orders select convert(datetime,'2006-12-10 20:38:05',120)

select * from orders
/*结果
2006-12-10 20:38:05.000
*/

select * from orders where begintime=convert(datetime,'2006-12-10 20:38:05',120)
/*结果
1 2006-12-10 20:38:05.000
*/

34,870

社区成员

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

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