Hermes
Hermes

Reputation: 17

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. I'm using the query :

SELECT Date FROM Table

This returns the date in C# in the format MM/DD/YYYY HH:MM:SS.

What would be the correct query to retrieve just the date and not the time?

Upvotes: 1

Views: 6815

Answers (4)

Dmitry Rotay
Dmitry Rotay

Reputation: 3280

To format date in MS Access you can use FORMAT function. 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').

Upvotes: 2

Hari Prasad
Hari Prasad

Reputation: 16956

In C# (since this question is tagged C#)

You have Date property of DateTime which returns Date.

var newdate = dateobject.Date;

or you can create new Date by passing date, month, year values to DateTime constructor.

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. in C# there is nothing like Date only, it is always associated with time.

Upvotes: 0

Jared Drake
Jared Drake

Reputation: 124

I'm not quite sure what you mean here with 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.

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

Upvotes: 0

JC Borlagdan
JC Borlagdan

Reputation: 3618

You can just change it in C# by doing

yourDateTimeVariable.ToShortDateString();

Upvotes: 2

Related Questions