My datetime data is stored as a local datetime and local timezone, given two columns. First column contains local datetime and a second one local timezone (see the picture below):
How can i create a single column with datetime in UTC?
So far i've tried:
SELECT
localdatetime, localtimezone,
PARSE_TIMESTAMP('%b %d, %Y, %r', LocalDateTime, "UTC") AS parsed_datetime
FROM `xx`.yy.zz
WHERE Localtimezone = "Europe/Ljubljana"
But the result i get is obviously wrong, since BQ doesn't know from which timezone it is converting.
BigQuery will automatically convert to UTC, so what you need to specify is really the timezone you are converting from.
Try the following instead:
select
localdatetime
, localtimezone
, PARSE_TIMESTAMP('%b %d, %Y, %r', LocalDateTime, localtimezone ) AS parsed_datetime
from sample_data
For more information refer to the documentation here
https://cloud.google.com/bigquery/docs/reference/standard-sql/timestamp_functions#parse_timestamp
Related
using BigQuery query API to retrieve data from BigQuery. for timestamp column , am getting values in different format.
query="select * from table"
QueryJobConfiguration queryConfig = QueryJobConfiguration
.newBuilder(query)
.setUseLegacySql(false)
.build();
Value in Table : "2022-02-25 08:47:48.801665"
Value in Output : 1.645778868801665E9
If I am casting to string the getting proper value. why is this happening ?
can someone explain ?
You need to convert to TIMESTAMP, because it takes milliseconds since 1970-01-01T00:00:00 UTC. That's why you are getting this result.
You can convert this result to TIMESTAMP using this formula timestamp.toInstant().toEpochMilli() * 1000.
You can see this example:
QueryParameterValue.timestamp(
// Timestamp takes microseconds since 1970-01-01T00:00:00 UTC
timestamp.toInstant().toEpochMilli() * 1000))
Here is more documentation about it.
If you want to cast from BigQuery. You have some options.
CAST the TIMESTAMP columns.
SELECT CAST(DATE("2022-02-25 08:47:48.801665") AS TIMESTAMP )
CAST TIMESTAMP to STRING.
SELECT STRING(TIMESTAMP "2022-02-25 08:47:48.801665", "UTC") AS string;
Give some format.
SELECT FORMAT_TIMESTAMP("%c", TIMESTAMP "2022-02-25 08:47:48.801665", "UTC") AS formatted;
You can see more documentation about CAST.
I am converting the data into json (using FOR JSON PATH) in SQL Server, one of the column in that data has a datetime datatype which I need to convert to ISO8601
Correct format: 2018-11-13T20:20:39+00:00
When I tried CONVERT(NVARCHAR(33), datetimecolumn, 127), the result is: 2019-10-24T12:35:12.870
When I tried TODATETIMEOFFSET(datetimecolumn, '+00:00'), the result is: 2015-03-31T00:00:00Z
In both cases I am not getting +00:00.
It appears that SQL Server represents a zero offset or UTC time with Z.
If you were to run SELECT TODATETIMEOFFSET(GETDATE(),60), which grabs the current time and applies a 1 hour offset, you would get the format you are expecting. However, I believe that Z denoting UTC is perfectly acceptable in ISO8601, but could be mistaken.
This command seems to output the JSON as expected.
SELECT TODATETIMEOFFSET(GETDATE(),60) as T FOR JSON PATH
If it is vital that the zero offset be represented as 00:00, I believe you will be force to work with strings.
select CAST(CONVERT(NVARCHAR(33), getdate(), 127) as NVARCHAR(35)) + '+00:00' as T
FOR JSON PATH
JSON (as a data-interchange format) doesn't specify the format for date and time values. FOR JSON implicitly converts date and time values to strings, but you can cast the datetime values in your statement with the appropriate type and style before using FOR JSON:
Statement:
SELECT CONVERT(varchar(30), CONVERT(datetimeoffset(0), GETDATE()), 126) AS T
FOR JSON PATH
Result:
[{"T":"2020-04-22T08:02:58+00:00"}]
If you want to convert to a specific time zone, you may try to use AT TIME ZONE:
Statement:
SELECT CONVERT(nvarchar, CONVERT(datetime2(0), GETDATE()) AT TIME ZONE 'Central European Standard Time', 126) AS T
FOR JSON PATH
Result:
[{"T":"2020-04-24T08:33:49+02:00"}]
How to convert yyyy-mm-dd hh:mm:ss (in 24 hours format) to dd-mm-yyyy hh:mm:ss (in 24 hours format? I am using Sql Server 2008.
Given Date Format: 2017-12-18 18:16:49 - Its in DateTime format
Required Date Format: 18-12-2017 18:16:49
This Would work in Older SQL Server Versions Also, Converted to datetime first if it's VARCHAR otherwise you can skip that conversion.
SELECT convert(varchar,convert(datetime,'2017-12-18 18:16:49'),105) + ' ' +
convert(varchar(8),convert(datetime,'2017-12-18 18:16:49'),14);
Use this Link as Reference for Date & Time conversion Formats
Try this
SELECT FORMAT(CAST('2017-12-18 18:16:49' AS datetime) , 'dd-MM-yyyy HH:mm:ss')
DECLARE #date DATETIME = '2017-12-18 18:16:49'
SELECT FORMAT(#date,'dd-MM-yyyy HH:mm:ss')
I would advice not change directly in SQL formate, instead you could change your php code where query fetch the date data
for example you could assign your $yourDate object to new dateTime, and use formate function to define the date format:
<?php
$yourDate = new DateTime();
$timestring = $yourDate->format('m-d-Y h:i:s');
echo $timestring;
the output will be: 05-18-2018 05:00:34
You could take reference in this older discussion
This will prevent some bug error might be occurred if your data table has some other date format relative to, and also it is more useful once you need to have change the date format again
Hope this will be help
My table in hive has a filed of date in the format of '2016/06/01'. but i find that it is not in harmory with the format of '2016-06-01'.
They can not compare for instance.
Both of them are string .
So I want to know how to make them in harmory and can compare them. Or on the other hand, how to change the '2016/06/01' to '2016-06-01' so that them can compare.
Many thanks.
To convert date string from one format to another you have to use two date function of hive
unix_timestamp(string date, string pattern) convert time string
with given pattern to unix time stamp (in seconds), return 0 if
fail.
from_unixtime(bigint unixtime[, string format]) converts the
number of seconds from unix epoch (1970-01-01 00:00:00 UTC) to a
string representing the timestamp of that moment in the current
system time zone.
Using above two function you can achieve your desired result.
The sample input and output can be seen from below image:
The final query is
select from_unixtime(unix_timestamp('2016/06/01','yyyy/MM/dd'),'yyyy-MM-dd') from table1;
where table1 is the table name present in my hive database.
I hope this help you!!!
Let's say you have a column 'birth_day' in your table which is in your format,
you should use the following query to convert birth_day into the required format.
date_Format(birth_day, 'yyyy-MM-dd')
You can use it in a query in the following way
select * from yourtable
where
date_Format(birth_day, 'yyyy-MM-dd') = '2019-04-16';
Use :
unix_timestamp(DATE_COLUMN, string pattern)
The above command would help convert the date to unix timestamp format which you may format as you want using the Simple Date Function.
Date Function
cast(to_date(from_unixtime(unix_timestamp(yourdate , 'MM-dd-yyyy'))) as date)
here is my solution (for string to real Date type):
select to_date(replace('2000/01/01', '/', '-')) as dt ;
ps:to_date() returns Date type, this feature needs Hive 2.1+; before 2.1, it returns String.
ps2: hive to_date() function or date_format() function , or even cast() function, cannot regonise the 'yyyy/MM/dd' or 'yyyymmdd' format, which I think is so sad, and make me a little crazy.
According to the documentation I've found from AR Systems on BMC Remedy, timestamps in an MSSQL Server environment (we're using SQL Server 2005 here) are stored as an int datatype in UNIX timestamp format.
I cannot for the life of me get custom SQL in Business Objects Web Intelligence to let me convert this timestamp into mm/dd/yyyy format, which I need to be able to group records by a date (without the timestamp, obviously). Anything I try to do involving math or datatype conversion throws an "invalid type" error. I can't convert the field to an int, varchar, datetime, or anything else. The only function that works is DATEADD, which still returns the full timestamp.
How can I get this converted? I'm going nuts here!
to convert GMT/UTC/Zulu to Local time Zone(EDT/New York):
DATEADD(hour,-5,DATEADD(s,Last_Modified_Date,'1/1/1970 00:00:00'))
Example of use to display Remedy work info entries (Transact-SQL):
SELECT Work_Log_ID, DATEADD(hour, +12, DATEADD(s, Last_Modified_Date, '1/1/1970 00:00:00')) as Last_Modified_Date , Description, Detailed_Description, Infrastructure_change_ID, Number_of_Attachments
FROM dbo.CHG_WorkLog
WHERE Infrastructure_Change_ID = 'CRQ001261'
ORDER BY Work_Log_ID desc
Why doesn't this work?
DECLARE #timestamp INT
SELECT #timestamp = DATEDIFF(s,'1/1/1970 00:00:00',GETDATE())
SELECT DATEADD(s,#timestamp,'1/1/1970 00:00:00')
Substitute the #Timestamp with the value from your table.
You may need to multiply the int timestamp by 1000. The AR System stores the date as the number of 'seconds' where as most other languages store the unix timestamp as milliseconds (and as a long data type)
Hope that helps!
Go to
user tool -> Tools -> Options -> Locale tab -> Date/Time Style -> Custom Format -> mm/dd/yyyy