The database (SQLite) has a field of type REAL with the values of the form (42153.659595).
How to translate this value in the form "dd.MM.yy HH:mm:ss" if 42153.659595 = 29.05.2015 15:49:49 ?
You can be explicit about what calendar system you require: http://www.sqlite.org/lang_datefunc.html
SELECT julianday('now') - julianday('1776-07-04');
In principle just don't "parse" (you mean: interpret raw representation). Use Sqlite API/builtin SQL functions to do it for you
In the interest of information:
The date and time functions use a subset of IS0-8601 date and time formats.
The datetime() function returns "YYYY-MM-DD HH:MM:SS". The julianday() function returns the Julian day - the number of days since noon in Greenwich on November 24, 4714 B.C. (Proleptic Gregorian calendar)
The date() function returns the date in this format: YYYY-MM-DD. The time() function returns the time as HH:MM:SS.
The correct value is achieved using the API/builtin SQL functions (42153.659595 - value from database):
SELECT datetime(julianday(42153.659595, 'localtime') + 2415018.29167) AS DT;
Output:
DT |
2015-05-29 15:49:49|
Constant 2415018.29167 was selected manually and query:
SELECT datetime (2415018.29167);
returns the current Greenwich Mean Time.
I work with a third-party application and documentation on the database is missing. Perhaps this strange decision, but it works. Thank you all for answers.
Related
I'm currently exporting data from Dynamics. There are a lot of date fields I need to export. By default they are all UTC timezone. I want to report on New Zealand timezone. I tried to approaches in Power Query:
1) use DateTimeZone.ToLocal: =Table.TransformColumns(#"dataset", {{"**UTC Date**", **DateTimeZone.ToLocal**, type datetimezone}})
2) use a specific timezone ("en-NZ"): =Table.TransformColumnTypes(#"dataset", {{"UTC Date", type datetime}}, **"en-NZ"**)
Both approaches work OK in Power BI desktop report, However once I published to Power BI service and after several refreshes (initially it was NZ time), the time turn back to UTC time.
I don't want to create extra columns in DAX and really want to try use Power Query. Is there any way to work it out?
I have faced the same issue a while back and came up with the following solution.
You can simply use a combination of RemoveZone and Addzone power query function to achieve this. Example below shows how to get Indian Standard Time(IST = +5:30)
= DateTime.AddZone(DateTimeZone.RemoveZone(DateTimeZone.UtcNow()),5,30)
Note that I used DateTimeZone.UtcNow() to always get the Universal standard DateTime and then convert this to the Indian time. You can use your own timezone values
Add the above code in place of DateTimeZone.ToLocal in your code.
Documentation to DateTime.AddZone
Power BI:
Use this Power Query function to convert times from UTC to Local Time, here its mountain time, but you can set to Whatever New Zealand Time is
let
ConvertDateColumnstoMountainTime = (sourcetable as table) as table =>
let
TargetColumnList = Table.ColumnsOfType(sourcetable, {type datetime, type nullable datetime, type datetimezone, type nullable datetimezone}),
AdjustTimeZones = Table.TransformColumns(sourcetable, List.Transform(TargetColumnList,
(name) => {name, (date) => if date <> null
then DateTime.From(DateTimeZone.RemoveZone(date) + #duration(0,CalculateUTCOffset(date,null),0,0))
else null})),
//Above returns type as text. Need it explicitly as Date.
AdjustColumnTypes = Table.TransformColumnTypes(AdjustTimeZones,List.Transform(TargetColumnList,
(name) => {name, type datetime}))
in AdjustColumnTypes
in ConvertDateColumnstoMountainTime
I want to convert the string 20160101000000 into datetime format using expression. I have used below date function
TO_DATE(PERIOD_END_DATE),'MM/DD/YYYY HH24:MI:SS')
But my table file is not loading. My session and workflow gets succeed. My target and source is also flatfile.
I want to change the string 20160101000000 into MM/DD/YYYY HH24:MI:SS for loading data into my target table.
You need to give exact format that looks so that to_date function can understand that format and converts it into date.
TO_DATE(PERIOD_END_DATE,'YYYYMMDDHH24MISS')
So here your date looks like YYYYMMDDHH24MISS (20160101000000).
There is often confusion with the TO_DATE function... it is in fact for converting a string into a date and the function itself is to describe the pattern of the incoming date. Now if you want to convert a date field to a specified date format you must use TO_CHAR
I am exporting data from Dynamics CRM 2011 into an Excel File. The entries have timestamps. In CRM they look like normal dates, but when I export them into Excel the timestamps look similar to this:
41855.4043865741
41831.6309259259
In Excel I can right-click on the cell with the timestamp and do Cell Formatting > Numbers > Date and convert this to a human readable string.
e.g.
04.08.2014 09:42:19
11.07.2014 15:08:32
The problem is, after I save the Excel with the human readable Datetime format and read the Excel with the xlrd Module (Python 2.7) I still get the strange format and not the translated one.
So I tried using datetime Module to dranslate the date but when doing so I get the wrong date.
import datetime
str_dt = float(41831.6309259259)
print datetime.datetime.fromtimestamp(str_dt).strftime('%Y-%m-%d %H:%M:%S')
My result is: 1970-01-01 12:37:11
Of course it is not Unixtimestamp but I have no idea what timestamp it actually is and how I can convert it with python.
The values you see are the actual number of days since the 1st of January 1900. This is the format used by CRM and Excel.
As suggested in this answer, you can use xlrd.xldate.xldate_as_datetime to:
Convert an Excel date/time number into a datetime.datetime object.
#param xldate The Excel number
#param datemode 0: 1900-based, 1:
1904-based.
#return a datetime.datetime() object.
In your case the datemode would be 0.
What is the difference between dateformat() and createODBCDate() in ColdFusion? Are these two functions the same or not? When do I need to use DateFormat() and when do I need to use createODBCDate()?
dateFormat() accepts a date and a format 'mask' and returns a string of the date, in the format passed.
For example, consider the following code:
mydate = dateFormat( now(), 'yyyy-mm-dd' );
Assuming the date is July 15, 2014 (which it was when I wrote this) the value of the variable named 'mydate' would be '2014-07-15' (without the quotes). So, you need to pass a date to the function.
createODBCDate() creates an actual date from the values passed - it does not format the date, it merely creates a date 'object'
dateFormat() is typically used to display a date in a user friendly manner. Try running this writeDump( now() ) to see what the default display looks like.
createODBCDate() is typically used when you need to pass a date to a SQL query. However, if you use cfqueryparam with a cf_sql_type that accepts a date, ColdFusion will handle converting the value (assuming it is a valid date) to a date that the database accepts and you do not need to use createODBCdate()
In 10+ years of doing ColdFusion, I have never used createODBCDate()
We have a staging table that's used to load raw data from our suppliers.
One column is used to capture a time-stamp but its data-type is varchar(265). Data's dirty: about 40% of the time, there is garbage data, otherwise time-stamp data like this
2011/11/15 20:58:48.041
I have to create a report that filters some dates/timestamps out that column but where I try to cast it, I get an error:
db2 => select cast(loadedon as timestamp) from automation
1
--------------------------
SQL0180N The syntax of the string representation of a datetime value is incorrect. SQLSTATE=22007
What do I need to do in order to parse/cast the timestamp string?
The string format for a DB2 timestamp is either:
'2002-10-20-12.00.00.000000'
or
'2002-10-20 12:00:00'
You have to get your date string in either of these formats.
Also DB2 runs on a 24 hour clock even though the output sometimes uses a 12 hour clock (AM / PM)
So '2002-10-20 14:49:50' For 2:49:50 PM
Or '2002-10-20 00:00:00' For midnight. Output would be 12:00:00 AM
It seems you have a lot of garbage data, so firt of all you should check if the data is a valid timestamp in the format you expect ('2011/11/15 20:58:48.041'). We could use a simple solution - just replace all digits with '0' and check the result format:
TRANSLATE(timestamp_column,'0','0123456789','0') = '0000/00/00 00:00:00.000'
If the format is the expected one, you should convert to DB2 timestamp. In DB2 for iSeries there is a build-in function since V6R1 TIMESTAMP_FORMAT. In your case it will look like that:
TIMESTAMP_FORMAT('2011/11/15 20:58:48.041','YYYY/MM/DD HH24:MI:SS.NNNNNN')
So the solution query combined should look something like that:
SELECT
CASE
WHEN TRANSLATE(timestamp_column,'0','0123456789','0') = '0000/00/00 00:00:00.000'
THEN TIMESTAMP_FORMAT(timestamp_column,'YYYY/MM/DD HH24:MI:SS.NNNNNN')
ELSE NULL
END
FROM
your_table_with_bad_data
EDIT
I just saw your comment that provider agreed to clean the data. You could use the solution provided to speed up the process and clean the data by yourself:
ALTER your_table_with_bad_data ADD COLUMN clean_timestamp TIMESTAMP DEFAULT NULL;
UPDATE your_table_with_bad_data
SET clean_timestamp =
CASE
WHEN TRANSLATE(timestamp_column,'0','0123456789','0') = '0000/00/00 00:00:00.000'
THEN TIMESTAMP_FORMAT(timestamp_column,'YYYY/MM/DD HH24:MI:SS.NNNNNN')
ELSE NULL
END;