hive - Get date in mm/dd/yyyy format - hive

Using Hive, I have date dates in yyyyMMdd format and I need it in 'MM/dd/yyyy' format.
SELECT dt,
CAST(SUBSTRING(FROM_UNIXTIME(UNIX_TIMESTAMP(dt, 'MMddyyyy')), 1, 10) AS date)
FROM timetable

No need for cast and substring.Specify the dateformat for dt in unix_timestamp() and the desired dateformat for from_unixtime()
select
dt,
from_unixtime(unix_timestamp(dt,'yyyyMMdd'),'MM/dd/yyyy')
from timetable;

Related

How to convert string in 'DD/MM/YYYY' or 'YYYY-MM-DD' into date in SQL Server?

I've got a string here which needs to be converted into date but the problem is that it could either be in 'DD/MM/YYYY' or 'YYYY-MM-DD' format.
I've already tried convert which only works for one of the two formats but not both:
declare #string nvarchar(255) = '2019-05-21'
declare #table table (date date)
insert into #table
select convert(date, #string, 111) as date
select * from #table
declare #string nvarchar(255) = '21/05/2019'
declare #table table (date date)
insert into #table
select convert(date, #string, 103) as date
select * from #table
Both of the above solutions result in an error is I use the other format.
Is there a way to get a string converted to date regardless of what format it is in?
Use try_convert():
insert into #table
select coalesce(try_convert(date, #string, 111),
try_convert(date, #string, 103)
) as date
try_convert() returns NULL if the conversion fails. In that case, the conversion will move on to the next pattern. With coalesce(), you can have as many different formats as you like.
You can use TRY_PARSE or PARSE to parse the date literal using a specific culture.
The second format YYYY-MM-DD is an unambiguous date format for the "new" date types like date and datetime2. It's not affected by the DATEFORMAT setting like datetime.
This means you only need to find one culture that can handle the first format. All of the following queries will return the same value :
select parse('21/05/2019' as date using 'en-GB')
-----
2019-05-21
select parse('2019-05-21' as date using 'en-GB')
-----
2019-05-21
select try_parse('21/05/2019' as date using 'en-GB')
-----
2019-05-21
select try_parse('2019-05-21' as date using 'en-GB')
-----
2019-05-21
If you are on SQL 2012 and above, you can use the FORMAT function.
The signature of this function is - FORMAT (value,format[,culture])
Example: SELECT FORMAT (getdate(), 'dd-MM-yyyy') as date and in your case SELECT FORMAT(CAST(<str_value> as DATE), 'yyyy-mm-dd')

Convert ISO-8601 varchar (0000-00-00T00:00:00+00:00) to datetime in SQL

How can I convert 2019-07-01T00:00:00+05:30 to DateTime in SQL?
2019-07-01T00:00:00+05:30 is a varchar field. I need to convert this into DateTime to compare this to a date field.
suggest me a query to Convert (2019-07-01T00:00:00+05:30) into DateTime
Convert To date :
select cast('2019-07-01T00:00:00+05:30' as Date)
Convert To time:
select cast('2019-07-01T00:00:00+05:30' as Time)
Convert To datetime :
select convert(datetime2, '2019-07-01T10:00:30+05:30',0)
Try any of these..
select cast(convert(datetime2, '2019-07-01T10:00:30+05:30',0) as datetime)
select convert(datetime2, '2019-07-01T10:00:30+05:30',0)
One option would be to use a combination of CONVERT on the timestamp without the timezone component, then use TODATETIMEOFFSET with the timezone portion to get the final result:
WITH yourTable AS (
SELECT '2019-07-01T00:00:00+05:30' AS dt
)
SELECT
TODATETIMEOFFSET(CONVERT(datetime, LEFT(dt, 19), 126), RIGHT(dt, 6)) AS output
FROM yourTable;
This outputs:
01/07/2019 00:00:00 +05:30
Demo
Unfortunately, SQL Server truncates the time zone information when converting from datetimeoffset to dateordatetime`. But, you can calculate the offset and add it back in:
select dateadd(minute,
datediff(minute, convert(datetimeoffset, dt), convert(datetime, convert(datetimeoffset, dt))),
convert(datetime, convert(datetimeoffset, dt))
)
from (values ('2019-07-01T00:00:00+05:30')) v(dt);
For your particular timezone, the date at midnight matches the UTC date, so you are safe. I'm on the other side of the world, so this would be a more important consideration in the "western" world ("west" being west of UTC).
The following query will convert the given VARCHAR to DATETIME value:
DECLARE #DateVal AS VARCHAR (30) = '2019-07-01T00:00:00+05:30';
SELECT CAST(REPLACE(SUBSTRING(#DateVal, 0, CHARINDEX('+', #DateVal)), 'T', ' ') AS DATETIME);

string to date - hive SQL

I am running queries in a hive environment.
I have a column which has a timestamp but is set up a string in the tables. I tried the following : all of them return Null
SELECT
,To_date(activitydate)
Cast:
,cast(activitydate as timestamp)
This is the how the data is set up in the table:
Appreciate any inputs on how I can convert this :
05/12/2017 00:00:00
SELECT
cust_id
,to_date(activitydate) activity_date
,type type_of_contact
FROM repl_task
WHERE to_date(activitydate) BETWEEN '2014-01-01' AND '2017-01-01' ;
I am running out of memory if I run this :
SELECT
cust_id
,activitydate
,SUBSTR(activitydate,4,2) AS DT
,SUBSTR(activitydate,0,2) AS MONTH
,SUBSTR(activitydate,7,4) AS YEAR
,type
FROM task
WHERE activitydate >='01/01/2016 00:00:00'
unix_timestamp function converts given format to unix timestamp and from_unixtime function converts from unix timestamp to given format:
hive> select from_unixtime(unix_timestamp('01/01/2016 00:00:00','MM/dd/yyyy HH:m:ss'),'yyyy-MM-dd');
OK
2016-01-01
Time taken: 0.118 seconds, Fetched: 1 row(s)
Can you try using to_date() with the date portion of your timestamp in ISO format:
SELECT
cust_id,
TO_DATE(CONCAT(SUBSTR(activitydate, 7, 4), '-',
SUBSTR(activitydate, 0, 2), '-',
SUBSTR(activitydate, 4, 2))) activity_date
type type_of_contact
FROM repl_task
WHERE
TO_DATE(CONCAT(SUBSTR(activitydate, 7, 4), '-',
SUBSTR(activitydate, 0, 2), '-',
SUBSTR(activitydate, 4, 2)))
BETWEEN '2014-01-01' AND '2017-01-01';
If this runs, but is not very performant, then you should consider storing your timestamps in ISO format. Storing dates as text, or as text in a non standard format, carries a penalty with most databases.

big query conversion from string to date format

I have string dates that I need converted to the date format. How do I take dates like "Jan 1, 2012 12:40:17 AM" and convert it to 1/1/2012? It doesn't work when I try to cast it as a date or when I use the date function.
Try (play with) below
#standardSQL
WITH yourTable AS (
SELECT 'Jan 1, 2012 12:40:17 AM' AS dt
)
SELECT
dt,
PARSE_TIMESTAMP('%b %d, %Y %r', dt) AS dt_as_timestamp,
DATE(PARSE_TIMESTAMP('%b %d, %Y %r', dt)) AS dt_as_date,
FORMAT_DATE('%m/%d/%Y', DATE(PARSE_TIMESTAMP('%b %d, %Y %r', dt))) AS dt_as_string
FROM yourTable
see more

Convert date, time stamp to date

I have the data in the following format in varchar form. There are 48 million rows in this format
'2015-09-18 00:00:00.000'
and want to convert it to the following format
'2015-09-18'
Can anyone help me with the code in Oracle
If your column has a timestamp type, you simply need to use to_char to format it properly:
with yourTable(yourDateColumn) as
(
select to_timestamp('2015-09-18 00:00:00.000', 'YYYY-MM-DD HH24:MI:SS.FF') from dual
)
select to_char(yourDateColumn, 'yyyy-mm-dd')
from yourTable
If your column is a string ( and storing dates in string fields is generally a very bad idea) with a fixed format, you simply need a substr:
with yourTable(yourStringColumn) as
(
select '2015-09-18 00:00:00.000' from dual
)
select substr(yourStringColumn, 1, 10)
from yourTable