Convert UTC Seconds to String in SQL - sql

I have a column with UTC Seconds in SQL.
How can I convert UTCSeconds to varchar in order to get this output in a select statement:
DD-MMM-YYYY HH:mm:ss
Example: 1400249277 in table
16-May-2014 15:07:57 as output of Select statement

I don't have a SQLServer available right now but this looks like it might be what you're looking for : DATEADD. You are probably considering the number of seconds since January the 1st 1970, so it would be something alike :
SELECT DATEADD (second, <your number of seconds>, '1970-01-01')

Related

Select rows which date (epoch) field equals a specific year [duplicate]

I store date from Calendar.getTimeInMilliseconds() in SQLite DB.
I need to mark first rows by every month in SELECT statement, so I need convert time in milliseconds into any date format using SQLite function only. How can I avoid this?
One of SQLite's supported date/time formats is Unix timestamps, i.e., seconds since 1970.
To convert milliseconds to that, just divide by 1000.
Then use some date/time function to get the year and the month:
SELECT strftime('%Y-%m', MillisField / 1000, 'unixepoch') FROM MyTable
Datetime expects epochtime, which is in number of seconds while you are passing in milliseconds. Convert to seconds & apply.
SELECT datetime(1346142933585/1000, 'unixepoch');
Can verify this from this fiddle
http://sqlfiddle.com/#!5/d41d8/223
Do you need to avoid milliseconds to date conversion or function to convert milliseconds to date?
Since sqlite date functions work with seconds, then you can try to
convert milliseconds in your query, like this
select date(milliscolumn/1000,'unixepoch','localtime') from table1
convert millis to seconds before saving it to db, and then use date function in sql query

hive date cast issue

Hi in my Hive table I have a column with the date values like this .
cl1
31102019000000
30112019000000
31122019000000
I have tried to convert the column values to date format like this
Select from_unixtime(unix_timestamp(cl1,'yyyy/MM/dd'),'yyyy-MM-dd') from table1;
it prints NUll. Any help will be appreciated.
You said you have dates in dd-mm-yyyy but then posted data that doesn't have any hyphens in at all
Assuming 31102019000000 is 31-oct-2019 00:00:00
Select from_unixtime(unix_timestamp(cl1,'ddMMyyyyHHmmss'),'yyyy-MM-dd') from ...
Match the format string to the data..
Try using this
Select from_unixtime(unix_timestamp(REGEXP_REPLACE(cl1,'0+$',''),'ddMMyyyy'),'yyyy-MM-dd') from table1;
but this would fail for years 2020,2010.
So the below query would be a better alternative
Select from_unixtime(unix_timestamp(cast(cast(cl1/1000000 as bigint) as string),'ddMMyyyy'),'yyyy-MM-dd') from table1;
The UnixTimeStamp which you are trying to convert to date format is out of range.
Your Time stamp corresponds to :
GMT: Saturday, August 2, 2955 1:43:20 AM
Your time zone: Saturday, August 2, 2955 5:43:20 AM GMT+04:00
Relative: In 936 years
It is an open bug in MYSql community. Please check the below URL for reference.
https://forums.mysql.com/read.php?20,385047,385132#msg-385132
MySQL database's built-in functions like UNIX_TIMESTAMP() will return 0 after 03:14:07 UTC on 19 January 2038. (Source: https://en.wikipedia.org/wiki/Year_2038_problem)

UTC and offset date time compare in sql server

I am storing UTC datetime in Database
e.g.
2018-06-05 11:37:00.000 (UTC)
2018-06-05 17:07 (+5:30 India standart time)
I am having offset as :
offset as +02:00
How can I compare in sql query that now offset time matched ?
e.g.
2018-06-05 13:37:00.000
My issue is X (IST) date time converted to UTC and now I want to covert to different time zone (Y)
The following functions helped me to resolve the issue:
1. SWITCHOFFSET
2. TODATETIMEOFFSET
SELECT GETDATE()
SELECT SWITCHOFFSET(GETDATE(), '+05:30')
SELECT CONVERT(VARCHAR(19), SWITCHOFFSET(TODATETIMEOFFSET(GETDATE(), '+05:30'),'+00:00'), 120)
SELECT GETUTCDATE()
If I understand your question correctly, you can use the DATEADD function to calculate the new datetime based on the UTC datetime and the offset
For example:
2 hours = 120 minutes
DATEADD(minute, 120, '2018-06-05 11:37:00.000')
Or using hours
DATEADD(hour, 2, '2018-06-05 11:37:00.000')
You can also go the other way using negative numbers
You don't have to use a literal value, you can supply a column name to parameter 3 for use in a query and also use this expression as part of a where clause
Here's a cheat sheet for the DATEADD function:
https://learn.microsoft.com/en-us/sql/t-sql/functions/dateadd-transact-sql?view=sql-server-2017

Invalid date error when modifying Big Query Table schema from string (YYYY-MM-DD HH:MM:SS TIMEZONE) to date

I have a table with a string format column where value are like this :
YYYY-MM-DD HH:MM:SS TIMEZONE so for example 2015-08-27 19:42:53 UTC
UTC is the only timezone. I want to rewrite this string column as a date column. I have an Invalid date error when I'm trying to run the following query to export the result in a new table
SELECT
CAST(my_date AS DATE),stuff_here,stuff_here
FROM
`table`
What should I do in order to properly change the type of this column from string to date ?
You appear to be trying to write this logic:
select CAST(substr('2015-08-27 19:42:53 UTC', 1, 10) AS DATE)
Because I am in New York, I would instead write:
select date(timestamp('2015-08-27 19:42:53 UTC'), 'America/New_York')
This distinction has been very important in our using data -- the difference between days at the Greenwich Meridian versus in our local area.
Try:
SELECT CAST(SUBSTR(my_date,1,19) AS DATE,stuff_here,stuff_here
FROM `table`
This assumes that you are not interested in the timezone part.

EXTRACT the date and time - (Teradata)

I am trying to extract the date and time from a field in Teradata.
The field in question is:
VwNIMEventFct.EVENT_GMT_TIMESTAMP
Here is what the data look like:
01/02/2012 12:18:59.306000
I'd like the date and time only.
I have tried using EXTRACT(Date, EXTRACT(DAY_HOUR and a few others with no success.
DATE_FORMAT() does not appear to work since I'm on Teradata.
How would I select the date and time from VwNIMEventFct.EVENT_GMT_TIMESTAMP?
If the datatype of EVENT_GMT_TIMESTAMP is a TIMESTAMP, it's simple Standard SQL:
CAST(EVENT_GMT_TIMESTAMP AS DATE)
CAST(EVENT_GMT_TIMESTAMP AS TIME)
If it's a CHAR you need to apply a FORMAT, too:
CAST(CAST(EVENT_GMT_TIMESTAMP AS TIMESTAMP FORMAT 'dd/mm/yyyyBhh:mi:SS.s(6)') AS DATE)
CAST(CAST(EVENT_GMT_TIMESTAMP AS TIMESTAMP FORMAT 'dd/mm/yyyyBhh:mi:SS.s(6)') AS TIME)
Edit:
For simply changing the display format you need to add a FORMAT and a CAST to a string:
CAST(CAST(EVENT_GMT_TIMESTAMP AS FORMAT 'YYYYMMDDHHMI') AS CHAR(12))
or
CAST(CAST(EVENT_GMT_TIMESTAMP AS FORMAT 'YYYYMMDDHHMISS') AS CHAR(14))
If you don't care about display, just want to truncate the seconds:
EVENT_GMT_TIMESTAMP - (EXTRACT(SECOND FROM EVENT_GMT_TIMESTAMP) * INTERVAL '1.000000' SECOND)
Working with timestamps is a bit tricky :-)
I know this is an old topic, but I've struggled with this too. Try:
CAST(EVENT_GMT_TIMESTAMP AS TIMESTAMP(0))
The result will be
01/02/2012 12:18:59
The datatype will still be timestamp, but it will just be the date and time with no microseconds (looks just like a datetime object in Microsoft SQL).