简体   繁体   English

在没有时间的情况下检索日期字段的正确 SQL 查询是什么? C# 微软访问

[英]What's the correct SQL query to retrieve date field without time? C# MS Access

I have a Microsoft Access Database with a Date column listing dates in the format MM/DD/YYYY.我有一个 Microsoft Access 数据库,其中的日期列以 MM/DD/YYYY 格式列出日期。 I'm using the query :我正在使用查询:

SELECT Date FROM Table

This returns the date in C# in the format MM/DD/YYYY HH:MM:SS.这将以 MM/DD/YYYY HH:MM:SS 格式返回 C# 中的日期。

What would be the correct query to retrieve just the date and not the time?只检索日期而不是时间的正确查询是什么?

您可以通过执行在 C# 中更改它

yourDateTimeVariable.ToShortDateString();

To format date in MS Access you can use FORMAT function.要在 MS Access 中格式化日期,您可以使用FORMAT功能。 In your case it will look like this:在您的情况下,它将如下所示:

SELECT FORMAT(Date, 'Short Date') FROM Table

Edit: Note that above example returns date in short date format as it is set up in system settings.编辑:请注意,上面的示例以短日期格式返回日期,因为它是在系统设置中设置的。 To be more specific you can also use custom format like FORMAT(Date, 'yyyy/mm/dd' ).更具体地说,您还可以使用自定义格式,如FORMAT(Date, 'yyyy/mm/dd' )。

I'm not quite sure what you mean here with C#.我不太确定你在这里用 C# 是什么意思。 But if you want the query to return it in that format, you could do something like但是,如果您希望查询以该格式返回它,您可以执行以下操作

SELECT CONVERT(VARCHAR(10), t.Date, 101) FROM Table t

read https://msdn.microsoft.com/en-us/library/ms187928.aspx about convert and it's formats.阅读https://msdn.microsoft.com/en-us/library/ms187928.aspx关于转换及其格式。

If you want to have c# do it, you can use the DateTime.Format() see https://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx如果你想让 c# 做到这一点,你可以使用 DateTime.Format() 参见https://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx

In C# (since this question is tagged C# )C# (因为这个问题被标记为C#

You have Date property of DateTime which returns Date .您有DateTime Date属性,它返回Date

var newdate = dateobject.Date;

or you can create new Date by passing date, month, year values to DateTime constructor.或者您可以通过将日期、月份、年份值传递给DateTime构造函数来创建新日期。

var newdate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 0, 0, 0);

Please note, in both cases what you get is DateTime object with time set to 12:00 AM .请注意,在这两种情况下,您得到的是DateTime对象,时间设置为12:00 AM in C# there is nothing like Date only, it is always associated with time.C#中没有像Date那样的东西,它总是与时间相关联。

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

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