SQL将日期时间从23:59:50转换为00:00:00

时间:2020-06-19 06:27:21

标签: sql sql-server

我有一个函数可以检索日期范围之间的一些数据。我通过两个日期范围作为开始日期和结束日期。

  • 开始日期:{18/06/2020 00:00:00}-> fromDate
  • EndDate:{18/06/2020 23:59:59}-> toDate

在我的SQL查询中,我正在执行以下操作:

public List<User> GetUsers(DateTime fromDate, DateTime toDate) 
{
     const string query = @"SELECT 
                                Username, age, address,
                                @fromDate AS ExecutedFromDate,
                                @toDate AS ExecutedToDate
                            FROM 
                                [Customer].[dbo].[People]
                            WHERE 
                                CreatedDate > @fromDate AND TransactionDate < @toDate";
}

var result = await Connection.QueryAsync<Users>(query, new { fromDate = from, toDate = to });

我的问题是我希望ExecutedFromDate的值为{18/06/2020 00:00:00},而ExecutedToDate的值为{18/06/2020 23:59:59}

SQL省略了时间,并将其设置为00:00:00。请帮忙

1 个答案:

答案 0 :(得分:0)

可以。

const string query = @"SELECT 
                                    Username, age, address,
                                    @fromDate AS ExecutedFromDate,
                                    @toDate AS ExecutedToDate
                                FROM 
                                    [Customer].[dbo].[People]
                                WHERE 
                                    CreatedDate > @fromDate AND TransactionDate < DATEADD(day, DATEDIFF(day, 0, @toDate, '23:59:59');

但这不好。最好的选择就是放置日期= {18/06/2020 23:59:59}