返回不同的日期
早上,
我试图通过唯一的标识符返回结果的不同日期。
例如:
ID|Date
1 | 2011-10-01 23:57:59
1 | 2011-10-01 23:58:59
1 | 2010-10-01 23:59:59
2 | 2010-09-01 23:59:59
2 | 2010-09-01 23:58:29
3 | 2010-09-01 23:58:39
3 | 2010-10-01 23:59:14
3 | 2010-10-01 23:59:36
时间并不重要,日期才重要。例如,在 ID 1 上,我无法对 ID 执行不同操作,因为这只会返回我的日期之一。所以我想返回:
1|2011-10-01
1|2010-10-01
我已经尝试了以下查询:
Drop Table #Temp
select Distinct DateAdd(dd, DateDiff(DD,0, Date),0) as DateOnly
,ID
Into #Temp
From Table
Select Distinct (Date)
,ID
From #Temp
但是我得到了以下结果:
ID|Date
1 | 2011-10-01 00:00:00
1 | 2011-10-01 00:00:00
1 | 2010-10-01 00:00:00
我是 SQL 新手,所以很抱歉我可能犯了一个明显的错误。到目前为止,我已经通过搜索之前提出的问题得到了答案。
一如既往,我们非常感谢任何帮助和指示。
Morning
I am trying to return the distinct dates of an outcome by a unique identifer.
For example:
ID|Date
1 | 2011-10-01 23:57:59
1 | 2011-10-01 23:58:59
1 | 2010-10-01 23:59:59
2 | 2010-09-01 23:59:59
2 | 2010-09-01 23:58:29
3 | 2010-09-01 23:58:39
3 | 2010-10-01 23:59:14
3 | 2010-10-01 23:59:36
The times are not important just the dates. So for example on ID 1 I can't do a distinct on the ID as that would return only one of my dates. So I would want to return:
1|2011-10-01
1|2010-10-01
I Have tried the following query:
Drop Table #Temp
select Distinct DateAdd(dd, DateDiff(DD,0, Date),0) as DateOnly
,ID
Into #Temp
From Table
Select Distinct (Date)
,ID
From #Temp
I am getting the following results however:
ID|Date
1 | 2011-10-01 00:00:00
1 | 2011-10-01 00:00:00
1 | 2010-10-01 00:00:00
I'm new to SQL so apologies I may have made a glaring mistake. I have got so far by searching through the previously asked questions.
As always any help and pointers is greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 T-SQL
convert
函数来提取日期。尝试
这样做,根据您的情况,做
更多信息: 获取 SQL Server 的日期部分日期时间字段
希望这有帮助!
You can use the T-SQL
convert
function to extract the Date.Try
so, in your case, do
further informations: Getting the Date Portion of a SQL Server Datetime field
hope this helps!
如果您使用的是 Sql Server 2008 - 您可以将 DateTime 列转换为内置
Date
类型,否则要删除时间,您应该仅转换为VARCHAR()
day/月/年部分,然后转换回日期时间,因此时间部分将被归零:If you are using Sql Server 2008 - you can cast DateTime column to a built in
Date
type , otherwise to get rid of time you should cast toVARCHAR()
only day/month/year parts and then convert back todatetime
so time part would be zeroed: