简体   繁体   English

在sql中选择按日加一的日期时间数据

[英]Select a datetime data set by day plus one in sql

I am working on a C# program where I have to select some specific datetime records from 2012 SQL server. 我正在开发一个C#程序,我必须从2012 SQL服务器中选择一些特定的日期时间记录。 The data record has two DateTime columns, start and ending. 数据记录有两个DateTime列,即开始和结束。 I just need the data if begin and ending is not the same day. 如果开始和结束不是同一天,我只需要数据。

SELECT * FROM table WHERE CAST(start AS DATE) != CAST(end AS DATE)

but this is not the problem yet. 但这还不是问题。 I need it a little bit more specific, I only want the data if the end date is the starting date plus one day, no matter what time booth of them have. 我需要它更具体一点,如果结束日期是开始日期加一天,我只想要数据,无论他们的展位是什么时候。

I made some pseudocode, unfortunately my SQL skills are quite limited. 我做了一些伪代码,遗憾的是我的SQL技能非常有限。

 SELECT Convert(date, getdate()) start
 FROM table
 WHERE EXISTS 
 (SELECT Convert(date, getdate())Ende FROM table  WHERE end = dateadd(day,1,start) )

The result should be all rows where start + 1 day = end (ignoring the time in the DateTime), so I can work with the data in my c# Programm. 结果应该是start + 1 day = end的所有行(忽略DateTime中的时间),因此我可以使用c#Programm中的数据。

I hope you can help me out, thank you. 我希望你能帮助我,谢谢你。

You can use DATEDIFF : 你可以使用DATEDIFF

SELECT * 
FROM table 
WHERE DATEDIFF(d, [start], [end]) = 1;

LiveDemo

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

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