I'm trying to figure out how to convert a timestamp into a datetime object in SQL (I'm using Google Big Query).
Here's what the timestamp column looks like — each row contains a 10 digit integer.
Any help would be appreciated!
You want timestamp_seconds():
select timestamp_seconds(time_stamp) as utc_timestamp
Your column looks like a Unix timestamp, which is the number of seconds since 1970-01-01.
Related
I am new to postgresql bot not to sql in general. I have a table that I need to read values from, on of the columns is a unix timestamp that I want to convert in to a more human readable format thus I found this:
SELECT lt,dw,up,to_char(uxts, 'YYYY-MM-DD HH24:MI:SS')
from products;
But that produces an error:
ERROR: multiple decimal points
I am lost here. I am sure someone can show me how to do it. The documentation isn't that clear to me. Postgresql 9.5 is the database.
to_char() converts a number, date or timestamp to a string, not the other way round.
You want to_timestamp()
Convert Unix epoch (seconds since 1970-01-01 00:00:00+00) to timestamp
So just apply that function on your column
SELECT lt,dw,up,to_timestamp(uxts) as uxts
from products;
This assumes that uxts is some kind of number data type (integer, bigint or double precision)
I have this table which stores millions of rows of data. This data has a date that indicates when was the data entered. I store the data in NUMERIC schemas with EPOCH UNIX as the format. However, I wanted to convert them to human date (yyyy-mm-dd hh:mm:ss) and later sort them by date not queried date.
However, it took me so long to find a suitable way. Here's my attempt.
I used SELECT CAST(DATE(timestamp) AS DATE) AS CURR_DT FROM dataset.table but it gave me this error:
No matching signature for function DATE for argument types: NUMERIC. Supported signatures: DATE(TIMESTAMP, [STRING]); DATE(DATETIME); DATE(INT64, INT64, INT64) at [1:13]
I used this method BigQuery: convert epoch to TIMESTAMP but still didn't fully understand
I'm a novice in coding so I hope you guys understand the situation. Thanks!
If I am understanding your question correctly you would like to take a numeric EPOCH time that is stored as an integer and convert it to a timestamp?
If so you can use the following in BigQuery Standard SQL:
select TIMESTAMP_SECONDS(1606048220)
It gives the output of:
2020-11-22 12:30:20 UTC
Documentation
If you only want the date component, then you would convert to a date after converting to a timestamp. Presumably you have seconds, so you would use TIMESTAMP_SECONDS() -- but there are similar functions for milliseconds and microseconds.
For just the date:
select date(timestamp_seconds(col))
Note that this removes the time component.
I have a date field that is pulling from a server as just an 8 digit number, and I would like to convert it to a standard date format.
For example, "20140501" would be converted to 05-01-2014
I tried using the DateTime code, but that didn't work for me. Any suggestions?
Many databases might recognize the string '20140501' as a date, because this is an ISO standard format. Hence, you might try this:
select cast(cast(datecol as varchar(255)) as date)
Whether or not this works depends on your database.
Have a scenario where I need to convert a NVARCHAR column containing time in hh:mm:ss format to a DateTime column for better filtering.
One of the dilemmas is that I don't really have a date, and as far as I know SQL does not have a Time datatype, but I can convert all to '1900-01-01' and append the time portion - I'm fine with this, I will just use the '1900-00-00' during my comparison.
The bigger issue is that the time data I'm converting has hour values going up to 27 hrs (according to them there's more then 24 hrs in a day). So there are values ie: 24:17, 26:25, etc..
To convert to datetime I'd have to do a little cleansing/string.replace first ie: 24:17 -> 00:17; 02:25, etc.
But before I dive in to that, any other more elegant way of dealing with this in SQL?
Using SQL Server 2014 Express .
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Oracle current_timestamp to seconds conversion
Convert Unixtime to Datetime SQL (Oracle)
I have been given a database wherein the datetime/timestamp column has been defined in NUMBER data type. What oracle function should i use to get the date time out of it?
Ex. In my table : Sample_Table, column time_touched is defined with oracle data type NUMBER. This column is supposed to be date time/timestamp. When I query the DB, I get numbers like 1355218434,1355218851 etc. These are representation of date time. But in query, what function should i use to get it display date time?
It looks like it is a UNIX timestamp (seconds since epoch 1970-01-01) where for example 1355218434 represents Tue, 11 Dec 2012 09:33:54 GMT (see converter here)
EDIT: Apparently the example below only works for MySQL, I thought that it also worked on Oracle because of the domain it is on but I was wrong. The solution is in the answer in this StackOverflow question: Convert Unixtime to Datetime SQL (Oracle)
You could use the FROM_UNIXTIME function to get what you want, see http://docs.oracle.com/cd/E17952_01/refman-5.0-en/date-and-time-functions.html#function_from-unixtime for reference.
Try something like this (untested):
SELECT FROM_UNIXTIME(time_touched) FROM Sample_Table
Your question is somewhat related to:
https://forums.oracle.com/forums/thread.jspa?threadID=2473501&tstart=75
Convert Unixtime to Datetime SQL (Oracle)