sql passing a date to a datetime - sql

I have a few user controls I made in wpf that are driven by two datepickers. One picker controls the begin of the date range and the other one controls the end of the date range.
I had issues with the datepicker.selecteddate property giving me the time along with the date and then my sql results were not all there because the passed in time value filtered out a lot of my results. I ended up finding that if I formatted that time to midnight then my results were all there, but every time I picked a new selection in the datepicker it would reset the formatted time.
After some testing I found that I can just pass the value of the datepicker text property as the parameter. This property's value is a string type and is set to the date value of the current selected date (ex: 3/14/2012 5:00:32 AM is selected date then '3/14/2012' is the text value).
So far, surprisingly, this seems to return all my results I wanted.
I was wondering about why this actually works (is it b/c the 'mdy' literal format is supported and the default?), and if there is any negative drawbacks to doing what i'm doing? I know a lot of times just because something works doesn't mean you should use it in production. I share the tables with others or I would just convert the datetime fields and parameters to date and be done.
I hope my question makes sense. Sometimes they don't. If there are questions leave comments and i'll chime in.

As far as negative drawbacks, I cannot see any - this is how we handle it throughout many apps/DBs. However, the way I typically handle it is if the record being added doesn't need the time (i.e. BusinessDate, LoadDate) then I add the record with the only the Date and no time - so the value is always in the format 3/14/2012 12:00:00 AM which I think makes querying significantly easier since you don't have to deal with the time
Then in my UI (winforms) when performing a search on the date with a datetimepicker I use the datetimepicker.Value.Date which gives the date in a similar format (3/14/2012 12:00:00 AM).
If you have a field in a table that needs a datetime, then use it and you can search the date by either formatting the date in the table or using date > yourdate AND date <= yourdate which would include the date you are searching.

Related

Data Type "Time" in mySQL turns into "Datetime" in Access

I have database with a MariaDB backend and a MS-Access frontend. I have a table with a column with the data type set to "time(6)". In my backend, the column only shows time-values between 00:00 and 23:59. There are no dates in that column.
However, when I open the (linked) table in my Access frontend, it shows date AND time in that column. It always shows the current date and the time value from the backend-table. So if the actual value in the field is "16:25", Access will show "07.02.2023 16:25" (current date in German date format).
I have a form in Access where one can edit the data for a specific recordset. In that form there is also a textbox for the time value. When the textbox is not selected, it will show the correct time (e.g. 16:25). However, as soon as I click into the textbox to edit the time, it will suddenly show date and time (e.g. 07.02.2023 16:25). The format of that textbox is set to "Time, 24h" though.
How can I make the textbox show only the time without date, even when the textbox is selected? Why does my Access frontend interpret the time value as a datetime value?
If I set the property "Show Date picker" of that textbox to "For dates", it will show the date picker after selecting the textbox, despite the fact that the format is set to "Time, 24h". This doesn't make sense to me. Any ideas? Thank you!
I updated the ODBC connection to the table several times, but in my frontend there are still datetime values in the column that should only have time values.
Why does my Access frontend interpret the time value as a datetime
value?
That's by design as Access doesn't have discrete data types for date and time.
You can easily read the time only:
=TimeValue([YourTimeField])
An edit can be done in an unbound textbox. Use the AfterUpdate event of this to write the edited time.
Also, see my article:
Entering 24-hour time with input mask and full validation in Microsoft Access.

Microsoft Access date comparison in query not working

I added the following field in a query:
IIf(Date()<[NextQDue],"Less","Greater")
The field NextQDue was created in a different query and formatted as Short Date. The value in NextQDue is 12/20/2018. Today's date is 1/1/2019. The query spits out "Less" when it should spit out "Greater." If I change the statement to Date()>[NextQDue] it spits out "Greater" when it should say "Less." I cannot figure this out. I believe I've formatted dates correctly, but nothing is working.
Always handle dates as Date, not text, not numbers, no exceptions.
So, adjust your query and change NextQDue to return a true date value.
If you need to display the value of NextQDue somewhere else, apply the format to the control displaying it.

Advanced DataGridView DateTime Filter

In my win form application I have used 'Advanced DataGridView'. For filter string columns it is working good but when i filter Datetime column the grid view becomes empty.
When i did debug it, i came to known that it is getting only dates and not dateTime from the column i.e
([Date_Time] IN ('11/04/2017', '12/04/2017', '13/04/2017'))
and this is the reason why my grid view become empty because there is no row with only date.
here is my code for 'Change filterString' event
Me.crossMatchBindingSource.Filter = dgvCrossMatchDetails.FilterString
So, my question is how can i include time in the filter.
I believe you are trying to bind a grid view to a data source, which uses SQL to extract data from the databases. I believe you can just amend your SQL as follows:
([Date_Time] between ('11/04/2017' and '14/04/2017'))
This should cater for times as well as dates.
You should just go to DataGridView properties and set "DateWithTime" to "True".
Then you are able to filter by Date and Time now.
Please correct me if i m wrong. whoever created AdvancedDataGrid, the code he/she wrote to filter can only filter the date and not the datetime. So i should split my datetime in two columns then i will be able to filter my dates. Actually i have done it and it is working perfectly but time column shows every record separately like text in the filter and not making grope of hours and minutes as excel does.

Same date format over several localizations

Currently im facing issue that troubles me a lot. I hope that somebody could help me out. I work for big company where are both Office 2007 (32bit) and Office 2010 (64 bit) used. Writing macros to be compatibile through whole company was hard task for me (I've never programmed in VBA before - actually this forum helped me a lot). My task is to maintain one big table in shared Excel sheet. There are several macros and several userforms. Now i will decsribe the problem briefly:
Sheet contains two columns with date format (start date and close date). Both values are imported to column form userform's textboxes (commandbutton lunches MsCal -exported to class- which fills those textboxes with date). What I simply need is to have date format as mm/dd/yyyy in both columns in order to perform filtering and other operations. When this values are updated by worker that uses different localization than English U.S. date is entered as dd.mm.yyyy. Thats make proper filtering based on date impossible. I tried to alter formating by:
UserForm1.TextBox10.Value = Format(Calendar1.Value, "mm/dd/yyyy")
but this piece of code misbehave somehow. On some machines it works, on some of them it is not working. And thats what is giving me headache. How should i proceed now? Is there a way to force excel to use same date format in sheet and ignore localization settings in Windows? Employees dont want to change localization to English U.S. because they are either used to their format, or need it for other applications. Is there a way to temporarily change localization only when this sheet opens?
Any advice will be apreciated.
Thanks in Advance
Peter
The best you can do is NEVER transform a date variable into Text.
Internally for excel a date is just a consecutive number (Left from the decimal separator are days and right from the decimal separator are hours). So, for example, the 10th of June 2012, for excel is 41188. This date value is independent of the date format set on your computer.
Now when it comes to represent dates (for humans to visualize) Excel will format this internal value into a String with the format set in your computer. So, for example if you have US date format in your computer, the date 41188 will be formated as 6/10/2012.
The big challenge with dates is to input the date in the correct format. When you input a Date as a string ("6/10/2012") then Excel will interpret it depending on the date format set on your computer. If you have US format, then it will thake the first cypher as month, the second as day and the last as the year. If you have a German format, it will read the first as day, the next as month and the last as year. So, the same input ("6/10/2012") for a US Format Excel will read 10th of june as for a German format Excel will read 6th of Oktober.
In your case, you should NOT format the date inside the Textbox10. For a US format Excel there is no problem, but if you have another date format, where the first cypher is the day instead of the month, you will get the wrong values:
Check this example. User inputs 10th of June in a German format Excel (dd.mm.yyyy)
Calendar1.Value retrieves a date value (41188)
Format(Calendar1.Value, "mm/dd/yyyy") transforms the date value into a string "06/10/2012"
When using the formated date (STRING), Excel will have to interpret what date it is. Because the computer date format is German, it will read Day:06, Month:10, Year:2012. You will be using day 41070 instead of 41188
If Calendar1.Value retrieves a Date variable and you give this date variable into a Date formated column, you will allways get the correct dale in your column and you will be able to filter and sort dates correctly regardless of the date format set inside the Column cells or the format set in the users computer.
Now, in your case, the best would be to assign directly the Calendar1.Value to the required cell. Something like:
ThisworkBook.WorkSheets("Sheet1").Range("C3").Value= Calendar1.Value
You can still asign Calendar1.Value into the TextBox10 for the user to see his selection, but disable the TextBox10 so that the only edit option is the calendar control. And when working with the date, istead of thaking it from the TextBox10, taking it directly from the Calendar1.Value .
If you still need to show the selected value from Calendar1 into a textBox then do NOT format the date in the Textbox. Instead, use:
UserForm1.TextBox10.Value = Cstr(Calendar1.Value)
This way, the user will see the date in the dateformat that he has set in his computer and to which he is used to.
One solution is to not use the date number format but rather only use the custom format for all your cell dates where you specify "mm/dd/yyyy" as the formatting string. However, in my experience, if your computer's regional settings are set to use "mm/dd/yyyy" then if you try make a custom cell formatting with this same string excel will keep as a dater linked to the computer setting so that doesn't help you. The way I worked around this was to change the date format on my computer, then format the cells as custom "mm/dd/yyyy" and save (and then turn your computer's settings back to how they were.) Now even though excel still claims they are date cells, you'll see that changing the settings on your computer doesn't change the value in the cell.
I guess another way is to always have a cell next to your date cell that calls the TEXT function. So if you have a date in A1 then in another cell =TEXT(A1, "mm/dd/yyyy") and only refer to this new cell. But that could make your spread sheet very messy.
I guess the best solution is to just get you IT dept to set every one in the company's date settings to use the same formats.

Problem reading Excel cells containing datetime in VBA

Some background: For a project I'm working on (building an XML DOM from a given excel spreadsheet of customer data), I need to be able to read the contents of a cell with a datetime in it. The cell in question contains "7/22/2011 0:00," and when I right-click->format cells, it tells me the category is "Custom" (not in the standard date category), and of type "m/d/yyyy h:mm." Yet when I select the cell, the formula pane displays it as "7/22/2011 12:00:00AM." So all three of these attempts to categorize the datatypes don't match up.
The problem: When I display the cell contents using ActiveWorkbook.ActiveSheet.Cells(x,y) in a MsgBox for debugging purposes, it only shows 7/22/2011 (cutting off the time). It can't be the space in between the date and time that throws it off, as I am successfully reading cells with spaces in them elsewhere in the spreadsheet.
Can anyone tell me why this is happening, or point me in the right direction for a VBA/Excel method that doesn't do this weird cropping thing that Sheet.Cells(x,y) is doing? Thanks. If only I had a penny for every time datetime datatypes caused problems for me..
Internally, Excel stores dates as numbers. It doesn't implement a date type. The number is the number of days since some point in the past (1900 or 1904, depending on the operation system, with some mistakes built-in regarding leap years). Time is represented as the fractional part of the number.
To make it look like a date to the user, you have to assign the cell a date format. Then the number is displayed as a date in the cell. (Excel assigns a date format automatically if you enter somethings that looks like a date to Excel.)
When you use ActiveWorkbook.ActiveSheet.Cells(x,y), you create a Range object and call its default property, which is Value. Value has a lot of magic built-in. In your case, it looks at the cell format and - seeing a date format - in converts the internally stored number into a Variant of subtype date. When you display it using a message box, the next trick happens. If the time is 0:00, the date is converted to a string without time. If the time were different from 0:00, it would be converted to a string with date and time. The date format is taken from your user's settings. Its independent of the date format you have assigned to the Excel cell.
If you use Value2 instead of Value (e.g. by using a messag box to display ActiveWorkbook.ActiveSheet.Cells(x,y).Value2), then you'll see the internal representation of the date, namely a number.
When you edit a cell, Excel uses yet another date format so you can see and edit all parts of the date: year, month, day and possibly hour, minutes and seconds. This is because your cell's date format could be restricted to just the month name.
Another complexity is added by internationalization. Certain date formats aren't applied directly but are a hint for using a date format from the current user's settings. So the format is first replaced with another date format and then applied. Furthermore, certain parts of the date and time formats are affected by the user's settings. And finally, the patterns of the date format are translated (in English, yyyy stands for the year, in German it's jjjj). When the Excel spreadsheet is saved, these formats are stored in the English form so that the sheet can be opened by user's and Excel installations with any language. In your case, internationalization probably affects the date format used when you edit the cell (putting month before day and using 12-hour display with AM/PM looks like Northern America).
I don't quite understand why Excel displays "7/22/2011 12:00:00AM" (instead of "7/22/2011 0:00:00AM"). I'm pretty sure your date/time has a time part of "0:00". But the internal number (as reveal by Value2) will tell you for sure.
It's like a formula, in the cell it will show the result, in the formula bar it will show the formula. The cell you can format, the formula bar you cannot. So you can change the cell's format to however you would like it to look like.
So, if you want to format the msgbox then you would need to do the following:
MsgBox (Format(ActiveWorkbook.ActiveSheet.Cells(x,y), "m/d/yyyy h:mm")