简体   繁体   English

我如何在sql server中按特定日期检索行?

[英]how i can retrieve rows by specific date in sql server?

I'm working in project for by PHP and SQL Server. 我在PHP和SQL Server的项目中工作。

The owner want from me design page show only users who register in same day 店主希望我设计页面只显示当天注册的用户

ie, if today 11-3-2011 i want show only all users who register in 11-3-2011 即,如果今天11-3-2011我只想显示所有在11-3-2011注册的用户

The table is: 该表是:

id  username  date

1   john      11\3\2011
2   sara      11\3\2011
3   john      5\1\2011
4   kreem     1\2\2011

i make it by mysql 我是通过mysql制作的

where DATE_ADD( items.created_date, INTERVAL 1 DAY ) > NOW() 

this cable of code show data which only insert in same day thats mean if today 10-4-2011 it will show only data which insert in 10-4-2011 if i today 15-4-2011 and im dose not insert any thing it will not show any thing, how i can build code like this in sql server? 这条代码的电缆显示的数据只在同一天插入,这意味着如果今天10-4-2011它将仅显示插入10-4-2011的数据,如果我今天15-4-2011并且im剂量不插入任何东西它不会显示任何东西,我如何在sql server中构建这样的代码? hope to be my question clear and understand 希望我的问题清楚明白

select id, userName
from YourTable
where CAST(date AS DATE) = CAST(GETDATE() AS DATE)

Function GETDATE() returns the current date and time. 函数GETDATE()返回当前日期和时间。

CAST(column as TYPE) will threat DateTime as just Date to omit differences in Time CAST(列为TYPE)将威胁DateTime作为日期以省略时间差异

获取今天的日期记录:

select * from tablename where date column between '2011-10-25 00:00:00' And '2011-10-25 23:59:59'

select * from yourtable where date = CONVERT(VARCHAR(20),GETDATE(),101) //希望这对获取当前日期值有所帮助..

This query compares only the date piece of today, ignoring the time. 此查询仅比较今天的日期部分,忽略时间。 It works in MS SQL Server, I'm not sure about other implimentations: 它适用于MS SQL Server,我不确定其他的含义:

select *
from YourTable
where convert(datetime, floor(convert(float, GETDATE()))) = [date]
select *
from YourTable
where
  [date] >= '20110311' and
  [date] < '20110312'

If you want it for today (always) you can use this 如果您今天想要它(总是),您可以使用它

select *
from YourTable
where
  [date] >= dateadd(d, datediff(d, 0, getdate()), 0) and
  [date] < dateadd(d, datediff(d, 0, getdate())+1, 0)

Last 10 days. 过去10天

select *
from YourTable
where
  [date] >= dateadd(d, datediff(d, 0, getdate())-9, 0)

Edit 1 Query with sample data and result 使用示例数据和结果编辑1查询

Table definition 表定义

create table users
  (name varchar(20),
   user_register_date datetime)

Data 数据

insert into users values ('user today', getdate())
insert into users values ('user yesterday', getdate()-1)
insert into users values ('user 9 days ago', getdate()-9)
insert into users values ('user 10 days ago', getdate()-10)

Query 询问

select *
from users
where user_register_date >= dateadd(d, datediff(d, 0, getdate())-9, 0)

Result 结果

name                 user_register_date
-------------------- -----------------------
user today           2011-04-14 08:29:28.407
user yesterday       2011-04-13 08:29:28.410
user 9 days ago      2011-04-05 08:29:28.410
SELECT * from YourTable
WHERE cast(convert(varchar(10),yourdate,101) as datetime) = 
cast(convert(varchar(10),[any day you want],101) as datetime)

Hope this helps. 希望这可以帮助。 Just replace [any day you want] with a date or datetime value 只需用日期或日期时间值替换[您想要的任何一天]

The CAST function is equivalent to the CONVERT function, except convert allows for some formatting options. CAST函数等同于CONVERT函数,但convert允许一些格式化选项。 In my example, I simply trimmed off the time from the date. 在我的例子中,我只是缩短了约会时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM