SQL Server 日期时间 LIKE 选择? [英] SQL Server datetime LIKE select?

查看:31
本文介绍了SQL Server 日期时间 LIKE 选择?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 MySQL 中

select * from record where register_date like '2009-10-10%'

SQL Server 中的语法是什么?

What is the syntax in SQL Server?

推荐答案

你可以使用 DATEPART() 函数

You could use the DATEPART() function

SELECT * FROM record 
WHERE  (DATEPART(yy, register_date) = 2009
AND    DATEPART(mm, register_date) = 10
AND    DATEPART(dd, register_date) = 10)

我发现这种方式易于阅读,因为它忽略了时间部分,而且您不必使用第二天的日期来限制您的选择.您可以通过添加额外的子句,使用适当的 DatePart 代码来提高或降低粒度,例如

I find this way easy to read, as it ignores the time component, and you don't have to use the next day's date to restrict your selection. You can go to greater or lesser granularity by adding extra clauses, using the appropriate DatePart code, e.g.

AND    DATEPART(hh, register_date) = 12)

获取 12 到 1 之间的记录.

to get records made between 12 and 1.

有关有效参数的完整列表,请参阅 MSDN DATEPART 文档.

Consult the MSDN DATEPART docs for the full list of valid arguments.

这篇关于SQL Server 日期时间 LIKE 选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆