pyodbc returns extra decimal places for MS Access Double/Single fields - ms-access

When I'm fetching data from my access database with pyodbc, it returns false format for float, date, or integers.
For example:
These values
Are returned like this
I've been searching for a long time now, I think it comes from the ODBC Driver used, but realy, I have no clue.
Another example would be a date like "21/01/2021" of type DATE in my MS Access DB, will be returned as a datetime in chinese or korean format YYYY-mm-dd H:m:s.

Floating-point columns (Numeric(Double) and Numeric(Single)) will display all decimal places in the Access UI if the field definitions in the table use the default formatting options
Format: (empty)
Decimal Places: Auto
Changing those settings to
Format: Fixed
Decimal Places: 2
will change the display format in the Access UI but the number itself will still be stored with its full precision. The ODBC driver does not pay attention to those formatting properties, so it returns the values with all available decimal places.
As for Date/Time values, the default format in the Access UI is to display just the date if the time component is exactly midnight. However the Date/Time value is stored with that time component (00:00:00) and again, the ODBC driver returns the complete value. See this answer for a more detailed explanation.

Related

MS Access Calculating date differences if Dates are Short Text

Is is possible in a Table to Calculate differences between Dates if the Value in the field is considered "short text"?
I am working to convert an Excel macro database into Access one and I have imported the data from the Excel file into an Access Table.
however i realized 2 feilds that count up until closure are now just fixed numbers but need to add up as each day passes until closure
when i imported the Dates became Short Text.
is there an expression that would handle this situation?
Each record has a serialized non repeating ID number seperate from access as well.
Dates I have are
OfficialissuanceDate,
DatePlanSubmitted,
DatePlanCompletedSubmitted,
DateClosed,
I need 2 calculations that increments daily when DateplanSubmitted and DatePlanCompletedSubmitted are null
Both comparing to OfficialIssuanceDate. then stop counting when they are no longer null. (have a date in updated to the record) and retain the value.
I have tried to Google calculating Dates but i get DateDiff function which doesnt appear to work. I've used Access and taken a class on it but never really made a new DB from scratch
Dates in a text field are not actual dates, just strings of characters. A Date/Time field stores value as a double number then displays in a date structure - "dd/mm/yyyy" is Access default structure.
Sometimes Access will do implicit conversion of data but not in this case. Either change field type to Date/Time or do conversion with CDate() function. However, you will find that conversion functions error with Null input.
Arithmetic operation with Date/Time field type is possible. However, arithmetic when any term is null returns Null so have to deal with that. One way uses Nz() function: Nz([DateClosed], Date()) - [DateOpened]. Unfortunately, Nz() is not available in table Calculated field type, so do that calc in query or textbox. Most developers avoid table Calculated field type. If you really want to use, expression would have to be: IIf(IsNull([DateClosed), Date(), [DateClosed]) - [DateOpened].

What format does a Google Data Studio DATE field expect from the connector?

I am building a Google Data Studio connector with a field called date. The field's type is YEAR_MONTH_DAY. Now, when I return a string, for instance "2020-09-22", the field shows null as a result. What value should my connector provide to have Data Studio recognize it as a date?
"2020-09-22" is of type YEAR-MONTH-DAY, not YEAR_MONTH_DAY
Without knowing the specifics of the connector you are using:
Be careful theat your provided format matches the expected one
Read about supported Dates and times in Data Studio
If necessary, use date formatting methods like e.g. TODATE
If the DATE field you are talking about is the absolute date as mentioned here, the expected format is YYYYMMDD.
For some reason I have had to implement a workaround for this more often lately which is to create a new field in the data source and use the TODATE() function and use that new field in my dimensions on the report.
Try a formula field like:
TODATE(comp_pd_end, "%Y-%m-%d", "%Y%m%d")
The expected date format for the connector is YYYYMMDD, without any separator.
You can check the official documentation:
https://developers.google.com/datastudio/connector/reference?hl=en#semantictype

How do I convert from a long to an Access Date/Time value in Jackcess?

I have the following code:
// Model the table
Table tbl = new TableBuilder("Transactions")
.addColumn(new ColumnBuilder("TransactionID", DataType.LONG).setAutoNumber(true))
.addColumn(new ColumnBuilder("ControllerID", DataType.LONG).setAutoNumber(false))
.addColumn(new ColumnBuilder("ReaderID", DataType.LONG).setAutoNumber(false))
.addColumn(new ColumnBuilder("Event", DataType.LONG).setAutoNumber(false))
.addColumn(new ColumnBuilder("Timestamp", DataType.SHORT_DATE_TIME).setAutoNumber(false))
.addColumn(new ColumnBuilder("Number", DataType.LONG).setAutoNumber(false))
.addIndex(new IndexBuilder(IndexBuilder.PRIMARY_KEY_NAME).addColumns("TransactionID").setPrimaryKey())
.toTable(db);
// Add the row
Map<String, Object> values = new HashMap<>();
values.put("ControllerID", cid);
values.put("ReaderID", rid);
values.put("Event", evtNum);
values.put("Timestamp", ts); // Long; must be converted to DataType.SHORT_DATE_TIME
values.put("Number", accNum);
tbl.addRowFromMap(values);
I want to convert from a long, ts to a date format that MS Access understands. The type of the "Timestamp" column in MS Access is Date/Time, with values displayed in the form "YYYY-MM-dd HH:mm:ss". What do I need to do to convert ts?
This answer to a similar problem makes mention of conversion to a double (which is what Access uses to store Date/Time fields, according to the source), but I don't understand what I need to do to my code to conform to it:
This also points up the issue of the independence of display format and data storage with Jet/ACE date values. The storage is as a double, with the integer part indicating the day since 12/30/1899 and the decimal part the time portion within the day. Any date you enter is going to be stored as only one number.
— David W. Fenton; 4 July 2010
Note: I know how to convert a long to a java.util.Date, set the time for a java.util.Calendar, then get a human-readable/parsable formatted String from it. That's not what I'm trying to do, which is why marking as a duplicate of the linked question is incorrect. What I'm trying to do is get the double value that MS Access understands.
While it is true that Access stores Date/Time values as Double floating-point numbers, you don't need to do that conversion yourself; Jackcess will do it for you. Just pass a java.util.Date to Jackcess and it will take care of the rest.
If your long ts is the number of milliseconds since the Unix epoch (1970-01-01 00:00:00 UTC) then that could be as simple as
values.put("Timestamp", new java.util.Date(ts));
although Access doesn't really support fractional seconds, so it would be a bit safer to truncate the milliseconds by doing
values.put("Timestamp", new java.util.Date(ts / 1000 * 1000));
Note that such a conversion will use the current time zone of the Java Virtual Machine (JVM).

Store more than four decimal places in an MS Access Currency field

I'm trying to store price figures in my Access database and am using a Currency field (instead of Decimal) for reasons, including the fact I'm using an old version of Access that are detailed in this thread: Syntax error in PARAMETER clause when passing through parameters as Decimal types using MS Access querydefs.
For consistency between systems I need to store the prices to 6 decimal places. However regardless of the settings I choose in Access, the values I type in are automatically rounded to 4 decimal places.
Here is the field in the table setup to 8 decimal places (I need 6 but was trying more in case):
Here is the just entered, as yet unrounded figure:
And after I press return, the value saves to the table, but rounded to four dps:
Is there any way to get MS Access to store currency datatypes to more than 4 decimal places? Am I doing something wrong?
Many thanks,
Jamie
For anyone that has the same problem as me, I've solved it by using currency parameter types in the querydef and decimal fields in the table:
Decimal data type:
Currency param:
This gets over issues with the decimal param type (detailed here:Syntax error in PARAMETER clause when passing through parameters as Decimal types using MS Access querydefs) and issues with the currency field rounding to 4 dps http://msmvps.com/blogs/access/archive/2009/04/27/the-problems-with-currency-fields.aspx

Excel import to MySQL: Date inserted as 0000-00-00

I have used PHPExcel library to import an Excel file (.xlsx) and insert into MySQL. I'm facing two problems with the date field:
1- In the excel sheet I try to change the date format to the desired one. Upon saving, the cells show the format I need (yyyy/mm/dd) but when I double-click to edit them they again change back to how it was before (mm/dd/yyyy).
2- When the file is imported to MySQL through PHPExcel all the columns are inserted correctly except the date column which inserts 0000-00-00. The format in the Excelsheet is the same as in MySQL but it enters is all zeros.
Please help!
The value store in PHPExcel is an Excel serialized datetime value (a float, representing the number of days since 1/1/1900 or 1/1/9004 depending on whether Windows or Mac Calendar was used). Changing the format is simply changing the way this value is displayed.
You will either need to know in advance whether a cell contains a date, or test it using the PHPExcel_Shared_Date::isDateTime() method, and convert it appropriately. You can then either retrieve it using getFormattedValue() to return the value as a formatted string; or use the built-in conversion functions to convert it to a unix timestamp (PHPExcel_Shared_Date::ExcelToPHP()) or a PHP DateTime object (PHPExcel_Shared_Date::ExcelToPHPObject()) that you can then format using the standard PHP date formatting functions before using it in your MySQL INSERT/UPDATE statements.
Change the date format to yyyy-mm-dd, save the file as CSV and import that into your database.