Excel changes time-format to datetime with MSQuery - mysql

I make with Excel a MSQuery to obtain some fields from a table in MySql what is working fine but there is one column which has in MySQL a time-format and Excel delievers me this in datetime-format.
I can format the cell in Excel so that it shows me the time in format hh:mm that is not the problem but I had to use these fields from the database to do some calculations in Excel which leads to my problem: I can't make a sum over these fields when the sum is greater then 24 hours, because then I get only the value of the hours and not from the days multiplied with 24 (e.g. instead of 25:15 I get only 1:15).
Normally I use for this format [hh]:mm so there will be displayed all values (even for more then 24 hours) but this doesn't work here, because I get a very high value (for example: 1017144:15). The reason for this is, that Excel adds for every field to the time the acrtual date of today (e.g. 12:00 will be 12.01.2016 12:00) and with this for every time in the sum the value for the date will be add additionally.
I tried it with the following statement in the MSQuery:
SELECT
DATE_FORMAT(entry.timeBegin, '%h:%i') AS 'Beginn',
entry.timeEnd AS 'Ende',
TIME(entry.pause) AS 'Pause'
FROM timetable.entry entry<br>
All 3 columns have in the database in MySQL the format hh:mm. Ende and Pause behaves like described above and Beginn is formatted in the right way (just without formatting the field by Excel) but the content of the field has type string and with this, Excel always use 0 (zero) for these fields when calculating with them, so I get allways 0 as sum.
I know that I can build the sum in MySQL, but this is no solution for me because the user could set some filters in Excel and this is to complicated to build in the query, espacially this is only one part of a more difficult construct.

Ok, here are a few examples. 'Beginn' allways delivers 0:00 (as described above) and 'Ende' and also 'Pause' are allways equal together. So we have to consider Ende or Pause with hh:mm and [hh]:mm format:
0:00 leads to 0:00 / 1017144:00
1:30 leads to 1:30 / 1017145:30
20:00 + 3:00 leads to 23:00 / 2034311:00
21:30 + 3:00 leads to 0:30 / 2034312:30
I didn't try PowerQuery because I don't know it till now and I don't know if it is inside of Excel or MS. I don't want to install an additional tool on the PC for every user. If I had only to take it into my Excel-sheet so it could be a solution if it is functions right.
Supplement: I looked after PowerQuery and see that this will bring me problems, because the most users uses MS Office 2010 and the requirement with Excel 2010 is Microsoft Office 2010 Professional Plus mit Software Assurance and the feature Software Assurance is not present in our company.
Added: For more clearness I add a screenshot from Excel:screenshot of Excel
I get column A from SQL displayed in format h:mm.
Column B is the same just in number-format.
D2 and E2 are the Sum from A1:A10 in format h:mm respectively [h]:mm:ss.
D3 and E3 are the Sum from A1:A23 in format h:mm respectively [h]:mm:ss.
As you can see SQL delievers the Time 5:30:00 via MSQuery and Excel shows me 14.01.2016 05:30:00 which I can format in the right way (column A). Building the sum is possible (look D2) as long as it is smaller than 24 hours else I get only the hours (< 24) which are greater than a entire day. The reason for this you can see in D3 and E3.
What can I do to get the right values? I suppose the easiest way would be at the point of getting the data from MSQuery.

Related

Adding two date stamps into a single SSRS expression/cell

This is all done in Microsoft SQL Server Report Builder
So I have a large data set that contains Work orders and then their 'type/craft'. Each craft is then broken down into each row so that you can see how many work orders are still open in the certain craft. At the top of the page, It list, WO's 1-2 Past Due, WO's 3-5 Past Due, WO's 6-10 Past Due... etc, till you reach 30 days+
I then have an expression inserted that will tell you what date the 1-2 days is... However, I am having trouble making the expression value be 2 dates or an in between date.... For example, I have =DateAdd("d", -1, Now) inserted which will give me the date from 1 day back, however, I would ALSO like it to show 2 days back.... So instead of ONLY saying 6/13/2018... it would say 6/12/2018 to 6/13/2018
I guess I could go back and edit my SQL code to automatically do the dates, however I thought it would be easier to use the report system.
You would want your expression to be something like this:
=DateAdd("d", -2, Today()) & " to " & DateAdd("d", -1, Today())
If you need the time as well you would want to use Now instead of Today, but based on your question it seems you are only interested in the dates, so this should return exactly what you are looking for.

MS Access, Conditional Formatting for Short Time Value highlight above 06:30

I don't see any related questions to conditional formatting regarding a time value. I have a report that needs to highlight any times greater than 06:30. I had the field set to Medium Time (06:30 AM) but the AM/PM may be an issue so now I am trying simply short time.
I have tried every combination or Left, Right, Hour, Minute - the real issue is simply the 30 minutes between 06:00 and 06:30 that is the issue. I can do a simple "Expression is Left([Time],2)>5 to get anything above 06:00, but I don't want anything from 06:00 to 06:30 highlighted.
Is the solution to this converting the time to a number first, and then applying the conditional format?
Never use strings for handling date and time. No exceptions.
You have dedicated functions for this, like:
DateDiff("n", #06:30:00#, [TimeField]) > 0
or even:
DateDiff("n", TimeSerial(6, 30, 0), [TimeField]) > 0
If a date value is included, strip that with TimeValue:
DateDiff("n", #06:30:00#, TimeValue([TimeField])) > 0

Timezone conversion in a Google spreadsheet

I know this looks simple.
In a Google spreadsheet, I have a column where I enter time in one timezone (GMT)
And another column should automatically get time in another time zone(Pacific Time)
GMT | PT
----------|------------
5:00 AM | 9:00 PM
As of now I am using
=$C$3-time(8,0,0)
The problem here is, I want to change the time formula for Daylight savings.
Is there any function or script available which can take the daylight saving into calculation automatically.
Short answer
There is no built-in function but you could build a custom function.
Example
/**
* Converts a datetime string to a datetime string in a targe timezone.
*
*#param {"October 29, 2016 1:00 PM CDT"} datetimeString Date, time and timezone.
*#param {"Timezone"} timeZone Target timezone
*#param {"YYYY-MM-dd hh:mm a z"} Datetime format
*#customfunction
*/
function formatDate(datetimeString,timeZone,format) {
var moment = new Date(datetimeString);
if(moment instanceof Date && !isNaN(moment)){
return Utilities.formatDate(moment, timeZone, format)
} else {
throw 'datetimeString can not be parsed as a JavaScript Date object'
}
}
NOTE:
new Date(string) / Date.parse(string) implementation in Google Apps Script doesn't support some timezones abbreviations.
From https://tc39.es/ecma262/#sec-date-time-string-format
There exists no international standard that specifies abbreviations for civil time zones like CET, EST, etc. and sometimes the same abbreviation is even used for two very different time zones.
Related
Get UTC offset from timezone abbreviations
Explanation
In order to consider daylight saving time zones the input argument for of the value to be converted should include the date, no only the time of the day. You could set a default date and time zone to build the datetimeString by concatenating it before calling the formula.
=formatDate("October 29, 2016 "&A2&" GMT","PDT","hh:mm a")
For the target timezone besides the three letter abbreviation we could use TZ database names like America/Los_Angeles, example:
=formatDate("October 29, 2016 "&A2&" GMT","America/Los_Angeles","HH:mm")
If timezone abbreviation and TZ name fails for the datetimeString use time offsets (i.e. UTC-4).
See also
Calculating year, month, days between dates in google apps script
References
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Date
This tutorial was amazingly helpful: https://davidkeen.com/blog/2017/01/time-zone-conversion-in-google-sheets/
Google Sheets does not have a built in way of converting time zone data but by using the power of Moment.js and Google’s script editor we can add time zone functionality to any sheet.
These are the files I copied into my script project:
https://momentjs.com/downloads/moment-with-locales.js saved as moment.js
https://momentjs.com/downloads/moment-timezone-with-data.js saved as moment-timezone.js
Make sure you add the moment.js script first and have it above the moment-timezone.js script because moment-timezone.js depends on it.
Then in your other script project, your Code.gs file can look like this:
var DT_FORMAT = 'YYYY-MM-DD HH:mm:ss';
/**
https://stackoverflow.com/questions/34946815/timezone-conversion-in-a-google-spreadsheet/40324587
*/
function toUtc(dateTime, timeZone) {
var from = m.moment.tz(dateTime, DT_FORMAT, timeZone);//https://momentjs.com/timezone/docs/#/using-timezones/parsing-in-zone/
return from.utc().format(DT_FORMAT);
}
/**
https://stackoverflow.com/questions/34946815/timezone-conversion-in-a-google-spreadsheet/40324587
*/
function fromUtc(dateTime, timeZone) {
var from = m.moment.utc(dateTime, DT_FORMAT);//https://momentjs.com/timezone/docs/#/using-timezones/parsing-in-zone/
return from.tz(timeZone).format(DT_FORMAT);
}
The easiest method is using a simple calculation.
Use =NOW() command in sheets and subtract it with the time difference divided by 24.
Example:
IST to Colombia
=NOW()-(10.5/24)
The time difference from India to Colombia is 10hours and 50min, we need to subtract it from the "Now" time and divide it by 24.
If the time zone is ahead of your place, then you need to add it.
Example:
IST to JAPAN:
=NOW()+(3.5/24)
=Now is set to US time by default, you can change it under general in settings.
enter image description here
enter image description here
I had the same problem (convert Manila Time to Sydney Time and automatically adjust for daylight saving time) when I found this page.
I didn't want to have a custom function but I found that, in Sydney, AEST (Australian Eastern Standard Time) starts on the first Sunday of April and AEDT (Australian Eastern Daylight Time) starts on the first Sunday of October.
So I thought, if I could find a formula that detects whether a date falls between the first Sunday of April and first Sunday of October (Standard Time) then I can automatically add 1 hour to the usual 2 hours to Manila time during Daylight Saving Time (dates falling outside the two dates) to have Sydney Time.
These two Excel solutions worked fine in Google Sheets:
How You Can Determine the First Sunday in a Month in Excel
How to determine if a date falls between two dates or on weekend in Excel
First Sunday of April this year (A1):
=CONCATENATE("4/1/",Year(today()))+CHOOSE(WEEKDAY(CONCATENATE("4/1/",Year(today())),1),7,6,5,4,3,2,1)
First Sunday of October this year (A2):
=CONCATENATE("10/1/",year(today()))+CHOOSE(WEEKDAY(CONCATENATE("10/1/",year(today())),1),7,6,5,4,3,2,1)
DST detector (A3) — if a date falls outside these two dates, it's DST in Sydney:
=IF(AND(today()>A1,today()<A2),"AEST","AEDT")
Time in Sydney (A4):
=NOW()+TIME(IF(A3="AEDT",3,2),0,0)
NOW() can be changed to any time format for tabulation:
I'm a new contributor and a novice, but I stumbled upon a function that had not been mentioned despite many hours of searching on the Sheets/Time Zone issue. Hoping this relatively simple solution will help.
For my sheet, I just want to add a row and automatically populate the local sheet date and time in the first two cells.
The .getTimezoneOffset() returns the difference in minutes between the client TZ and GMT, which in NY during Daylight Savings Time is 240. The function returns a positive number for the zones with "GMT-x", and vice versa for zones with "GMT+x". Hence the need to divide by -60 to get the correct hour and sign.
function addRow() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
sheet.insertRows(2, 1);
rightNow = new Date();
var tzOffset = "GMT" + rightNow.getTimezoneOffset() / -60;
var fDate = Utilities.formatDate(rightNow, tzOffset, "MM-dd-yyyy");
var fTime = Utilities.formatDate(rightNow, tzOffset, "HH:mm");
sheet.getRange('A2').setValue(fDate);
sheet.getRange('B2').setValue(fTime);
sheet.getRange('C2').setValue(tzOffset);
}
I've since found that I'm not the first person to respond to the GMT correction connundrum mentioning .getTimezoneOffset(). However, this thread has the most views on this topic, so I figured this option deserves a mention.
DST ends here on November 7th, 2021, so I'll report back if it doesn't adjust as expected to "GMT-5"
.getTimezoneOffset()
That can also be done without macros. Just using functions and data manipulation will suffice. Explaning the whole process here would be a bit cumbersome. Just do your research on how the various time functions work and use your creativity.
Hint: Use =NOW() if you want both current date and time. You'll actually need that if you need to find out the precise diff in time between to different dates.
Use =NOW()-INT(NOW()) when you only want the time (with date truncated if both times fall on the same date). Then format the corresponding cell or cells for time (i.e. 4:30 PM), not for date-time (3/25/2019 17:00:00). The latter is the format you'd use when you want to show both date and time... like when you use NOW().
Also search online for the Daylight Saving Time offset for the various standard time zones (PT, MT, CT, ET, AT) with respect to the Coordinated Universal Time (UTC). For example, in 2019 the offset for Pacific Time is UTC-7 when DST is observed starting on March 10 at 2 AM (Pacific) until November 3 at 2 AM. That means that the difference in time from UTC to Pacific is 7 hours. During the rest of the year is 8 hours (UTC-8). During DST observance starting sometime in March (the 10th this yr) it goes from PST to PDT by moving clocks forward 1 hr, or what we know as UTC-7 (that's summer time). After DST observance it goes from PDT to PST by moving clocks back 1 hr again, or what we know as UTC-8 (or winter time). Remember that the clock is advanced one hour in March to make better use of time. That's what we call DST, or Daylight Saving Time. So after March 8 at 2 AM (this year in 2019) we are in UTC-7. In November, we do the opposite. In Nov 3 at 2 AM the clock is taken back one hour as the winter kicks in. At that point we are back in Standard Time. Seems a bit confusing but it's really not.
So, basically, for folks in PT they go from PST to PDT in March and from PDT to PST in November. The exact same process goes on with Mountain Time, Central Time and Eastern Time. But they have different UTC time offsets. MT is either UTC-6 or UTC-7. CT is either UTC-5 or UTC-6. And ET is either UTC-4 or UTC-5. All depending on whether we are in summer time when Daylight Saving is observed to make better use of daylight and working hours, or in winter time (AKA, Standard Time).
Study these thoroughly and understand how they work, and play around with the various time functions in Excel or Google Sheets like the TIME(#,#,#) and NOW() functions and such, and believe me, soon you'll be able to do about anything like a pro with plain functions without having to use VBA Google Apps Script. You can also use the TEXT() function, though, with tricks like =TEXT(L4,"DDD, MMM D")&" | "&TEXT(L4,"h:mm AM/PM"), where L4 contains you date-timestamp, to display time and date formats. The VALUE() function also comes in handy every now and then. You can even design a numerical countdown timer without the use of macros. You'd need to create a circular reference and set iterations to 1, and time display to say every 1 min, in your spreadsheet settings for that.
The official timeanddate dot com website is a good source of info for all to know about time zones and how daylight time is handled. They have all UTC offsets there too.
Create your own Timezone Converter in Google Sheets:
Step 1: Create your table for the timezone converter.
Step 2: Enter the times for your time zones in a column.
Note: Ensure that you select date/time format(Select Cell(s) -> Format -> Number -> Time/Date)
Step 3: Write a formula to convert timezone using the following functions
Google Sheet Functions
=HOUR(A8)+(B3*C3) converts the hours.
=MINUTE(A8)+(B3*C3) converts the minutes.
Step 4: Convert back to time format using TIME(h,m,s) function
=TIME(HOUR(A8)+(B3*C3), MINUTE(A8)+(B3*C3), SECOND(A8))
This is a simple way to convert timezones.
However, if you want to implement an accurate timezone converter that takes care of the previous day, next day, and beyond 24 hours, beyond 60 minutes, please use MOD operations and handle all the cases.
Visit(or Use) this google sheet for reference:
https://docs.google.com/spreadsheets/d/1tfz5AtU3pddb46PG93HFlzpE8zqy421N0MKxHBCSqpo/edit?usp=sharing
just use the TZData format to "pull" a sync from UTC and display your choice.
Example in order to "change" the display of your cell to Berlin local time
=fromUTC(N82, "Europe/Berlin")
or for Tokyo
=fromUTC(N82, "Asia/Tokyo")
or San Francisco
=fromUTC(N82, "America/Inuvik")
point of reference for Time Zones is here >>
https://en.wikipedia.org/wiki/List_of_tz_database_time_zones

Add Datetime rows to a datatable in the DataTableBuilder for Google Scripts

I've built an intraday macroeconomic forecast system in R. It uses intraday market data to update an forecast. Basically, I have a forecast series that looks like financial market data.
I want to show these data using a Google Chart in the Google App Script platform. To keep the datatable small (and thus load time down), I only want to plot intraday values for the five most recent days. For older days, I want to save only the last forecast of the day. That is, I want to have a time series plot with two frequencies of data which the user can choose from.
Here's and example of how I write the data from R (columns separated with "|"):
OBSlatest.forecast|freq
2014-03-10 14:45:00 EDT|4.09662|D
2014-03-11 15:00:00 EDT|4.10075|D
2014-03-12 14:57:00 EDT|4.08862|D
2014-03-13 14:57:00 EDT|3.998|D
2014-03-14 15:00:00 EDT|3.9843|D
2014-03-10 09:30:00 EDT|4.13823|I
2014-03-10 09:33:00 EDT|4.13468|I
2014-03-10 09:36:00 EDT|4.14078|I
I'm currently translating this table into Google App Script with
var data = Charts.newDataTable()
// Set data table columns
.addColumn(Charts.ColumnType.DATE, "Time")
.addColumn(Charts.ColumnType.NUMBER, "Forecast")
.addColumn(Charts.ColumnType.STRING, "Frequency")
// Fill in rows
.addRow([new Date(2014,3,13,14,57, 0),3.998 ,'D'])
.addRow([new Date(2014,3,10,9,30, 0),4.13823 ,'I'])
// More rows, but they all look like this, either a 'D' or 'I' at end
I've got a rough script up now that uses a category filter so that the user can choose to show either the daily frequency 'D' or intraday frequency data. However, I seem to be using the Date() function wrong. My data are all out of order and the dates don't match what I've loaded. My understanding the of Date() function's arguments is: Date(year, month,day,hour,min,sec), yet I only seem to be able to get the year to show up right in my chart. For example, the first row I'm adding in my code above yields "13 apr 2014 11:57:00" when my expectation is that it'd give "13 mar 2014 13:30:00"!
Nearest I can tell is that it is subtracting three hours from my time and one month from my month. Any ideas what's going on? This approach works fine for an HTML implementation of Google Charts, which I have up at: EfficientForecast.com
Thanks for any help!
The month parameter is zero-based, so subtracting one is the correct/expected thing to do. Check the MDN docs.
About your hour issue, I can not reproduce it. So I imagine it is something specific to your system. Please check what your Google Drive timezone setting is, then this Apps Script's setting and lastly your OS timezone setting. I imagine this is happening because some of these configurations are not matching.

Access data conversion issue

I'm using Access 2003. Have a table with some date values in a text data column like this;
May-97
Jun-99
Jun-00
Sep-02
Jan-04
I need to convert them to proper date format and into another Date/time column, So create a new Date/Time columns and just updated the values from the Text column into this new column. At first it looked fine, except for years after the year 2000. The new columns converted the dates as follows;
May-97 > 01/05/1997
Jun-99 > 01/06/1999
Jun-00 > 01/06/2000
Sep-02 > 01/09/2010
Jan-04 > 01/01/2010
As you can see any data with year after 2000 get converted to 2010. The same thing happens if I query the data using FORMAT(dateString, "dd/mm/yyyy").
Any ideas why this is so? Do I have to split the month and year and combine them again?
Thanks
Access/Jet/ACE (and many other Windows components) use a window for interpreting 2-digit years. For 00 to 29, it's assumed to be 2000-2029, and for 30-99, 1930-1999. This was put in place to address Y2K compatibility issues sometime in the 1997-98 time frame.
I do not allow 2-digit year input anywhere in any of my apps. Because of that, I don't have to have any code to interpret what is intended by the user (which could conceivably make mistakes).
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.
If you input an incomplete date (i.e., with no century explicitly indicated for the year), your application has to make an assumption as to what the user intends. The 2029 window is one solution to the 2-digit year problem, but in my opinion, it's entirely inappropriate to depend on it because the user can change it in their Control Panel Regional Settings. I don't write any complicated code to verify dates, I just require 4-digit year entry and avoid the problem entirely. I have been doing this since c. 1998 as a matter of course, and everybody is completely accustomed to it. A few users squawked back then, and I had the "it's because of Y2K" as the excuse that shut them down. Once they got used it, it became a non-issue.
The date is ambiguous, so it is seeing 02 as the day number. Depending on your locale, something like this may suit:
cdate("01-" & Field)
However, it may be best to convert to four digit year, month, day format, which is always unambiguous.
Access seems to be get conduced between MM-YYYY format and MM-DD format. Don't know why it is doing it for dates after the year 2000, but solved it by converting the original string date to full date (01-May-01). Now Access converts the year into 2001 instead of 2010.
If you don't supply a year and the two sets of digits entered into a date field could be a day and month then Access assumes the current year. So your first three dates definitely have a year in them. But the last two don't.
Note that this isn't Access but actually the operating system doing the work. You get the same results in Excel. I had an interesting conversattion with some Microsoft employees on this issue and it's actually OLEAUT32.DLL.