0

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?

Felix Pamittan
  • 30,546
  • 7
  • 32
  • 56
Hermes
  • 7
  • 1
  • 3

4 Answers4

2

You can just change it in C# by doing

yourDateTimeVariable.ToShortDateString();
JC Borlagdan
  • 2,662
  • 3
  • 24
  • 44
1

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').

Dmitry Rotay
  • 3,230
  • 1
  • 13
  • 11
  • Thank you so much! After hours of searches online, I finally found an answer. – Hermes Apr 15 '16 at 03:03
  • I see that in comment to another answer you mention WPF. If you display data through bindings, you can just change it's display format in WPF and not bother formatting it in SQL query. Check this question: http://stackoverflow.com/questions/7978249/date-formatting-in-wpf-datagrid – Dmitry Rotay Apr 15 '16 at 03:07
0

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

Jared Drake
  • 124
  • 7
  • I have an Access Database and a C# program. I'm trying to grab data from the Date Column from the database and display it in C# WPF. – Hermes Apr 15 '16 at 02:49
  • I think I understand. If you are simply excutingSql commands (no linq or anything) using c#, does the query I provided solve your problem? – Jared Drake Apr 15 '16 at 03:03
  • The function Convert wasn't found it's giving me. – Hermes Apr 15 '16 at 03:05
  • My apologies, I was somehow misread you were using Microsoft Access and thought of TSQL. It's Format instead of convert for Access – Jared Drake Apr 15 '16 at 03:09
0

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.

Hari Prasad
  • 15,659
  • 4
  • 18
  • 33