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

kfgdxczn  于 2021-07-26  发布在  Java
关注(0)|答案(1)|浏览(492)

我有一个函数,用于检索日期范围之间的一些数据。我传递两个日期范围作为开始日期和结束日期。
起始日期:{18/06/2020 00:00:00}->起始日期
结束日期:{18/06/2020 23:59:59}->结束日期
在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} 它执行了 {18/06/2020 23:59:59} sql省略了时间,并将其设为00:00:00。请帮忙

eeq64g8w

eeq64g8w1#

你可以。

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}

相关问题