DoubleType to Timestamp in Impala - cloudera

How do I convert a DoubleType into a Timestamp in Impala ?
I have the following doubletype format:
month_date: 201710
I would have thought something like:
SELECT
to_timestamp(cast (t1.month_date as string), 'yyyy-MM')
FROM old t1;
I am getting all Null
in

Related

When casting a datetime as date, why does SQLite3 only return the year and not the full date?

I know that you can cast a datetime to date using the date() function:
sqlite> select date('2000-01-01 10:00:00');
2000-01-01
But why does SQLite3's cast expression such as in
sqlite> select cast('2000-01-01 10:00:00' as date);
2000
only return the year?
Even using an explicit datetime() setup solely returns the year:
sqlite> select cast(datetime('2000-01-01 10:00:00') as date);
2000
Or:
sqlite> select cast(datetime('now') as date);
2019
Looking at Postgresql, it resolves both properly:
postgresql> select date('2000-01-01 10:00:00');
2000-01-01
postgresql> select cast('2000-01-01 10:00:00' as date);
2000-01-01
What's the technical explanation for SQLite3's – to me unexpected – behavior?
For SQLite there is no Date datatype. As mentioned in their documentation here: https://www.sqlite.org/datatype3.html
What you use as Date is actually TEXT.
You can check that:
select typeof(datetime('now'));
returns:
text
And:
select typeof(cast(datetime('now') as date));
returns:
integer
So the result of cast('2000-01-01 10:00:00' as date) is an integer and it's the same
integer that you get by:
select '2000-01-01 10:00:00' + 0
when SQLite implicitly converts '2000-01-01 10:00:00' to 2000 in order to use it in a mathematical operation.
In the case of dates it happens to be the numeric value of the year, but in general SQLite returns the longest substring of the TEXT, starting from the 1st character, that can be represented as an integer.
So for '2000-01-01 10:00:00' it's the substring until the 1st -, which is the year.

How to use 'where clause' for Timestamp teradata where the data is in format 'YYYY-MM-DD HH:MI:SS.s'?

I'm willing to filter a table based on a column which has the data in format 'YYYY-MM-DD HH:MI:SS.s' as described in the question. Since this is not default timestamp format in teradata,so I tried to typecast.
So far I have tried following things :
where column_name > ${VAL1}(Timestamp(1),Format'YYYY-MM-DDbHH:MI:SS.s(1)') and column_name < ${VAL2}(Timestamp(1),Format'YYYY-MM-DDbHH:MI:SS.s(1)')
where column_name > ${VAL1}(Timestamp(0),Format'YYYY-MM-DDbHH:MI:SS.s(1)') and column_name < ${VAL2}(Timestamp(0),Format'YYYY-MM-DDbHH:MI:SS.s(1)')
where column_name > ${Val1}(Timestamp(1),Format'YYYY-MM-DDbHH:MI:SS.s(1)bt') and column_name < ${Val2}(Timestamp(1),Format'YYYY-MM-DDbHH:MI:SS.s(1)bt')
But everytime the error is same "Syntax error,expected something like
an 'OR' keyword or ')' between an integer and the integer '13'.
I believe the problem is in passing the format to teradata. Any help appreciated.
P.S: VAL1 and VAL2 are bash variables.
You pass the variables without quotes, e.g.
where column_name > 2019-03-04 12:34:56.7(Timestamp(1) ...
the parser treats 2019-03-04 as a calculation and stops at 13.
Try
where column_name > '${VAL1}'(Timestamp(1) ...
And because this format is Teradata's default instead of casting to a timestamp you better use a literal:
where column_name > TIMESTAMP '${VAL1}' and column_name < TIMESTAMP '${VAL2}'

How to convert timestamp to string in SQLite?

I have table with timestamps in ms stored in it. I want to convert those timestamps in a human readable form.
Here is a sample output of my table:
SELECT date raw, strftime('%d-%m-%Y', (date/1000)) as_string
FROM my_table
+-----------------+--------------+
| raw | as_string |
+-----------------+--------------+
| 1444687200000 | 06-47-3950 |
+-----------------+--------------+
... ... ...
+-----------------+--------------+
As you can see, the date as string is quite strange (06-47-3950).
How can I obtain 12-10-2015?
Try this:
SELECT date raw, strftime('%d-%m-%Y', datetime(date/1000, 'unixepoch')) as_string
FROM my_table
You need to convert timestamp to date before.
You need to convert timestamp to daytime first. There was an answer on one forum. I quote it here.
Here you are: try those queries to see why and how.
select julianday('1899-12-30 00:00:00');
-- that gives 2415018.5 (remember Julian dates start at noon)
select datetime('40660.9454658044', '+2415018 days', '+12 hours', 'localtime');
-- gets you 2011-04-28 00:41:28 (depending on your local
time)

How can i convert datetime to date format in sqlite selected vaues

I just try to convert datetime to date, but without success. Help me to do it:
select date('startTime') from usertable ;
select datetime(substr(startTime, 7, 4 )) from usertable;
Just try this.. it will convert datetime to date.
SELECT strftime('%d-%m-%Y', fieldname)
SELECT strftime('%d-%m-%Y', 'now') gives output 5-06-2015

Convert varchar(50) to Date Format 'yyyy-mm-dd' in teradata?

I have a column defined as Date format yyyy-mm-dd while creating.
I want to insert data from other table which has that column as varchar(50)
While selecting and inserting into the table I got this error
INSERT Failed. 2665: Invalid date.
Can someone help me in casting this?
INSERT INTO TEMP_TABLES.FACT
(
CUSTOMER_ACCOUNT_ID,
LOB_START_DATE,
)
SEL
CUSTOMER_ACCOUNT_ID,
I.start_date as LOB_START_DATE,
FROM #LOGIN I
left join JOURNEY_TABLE.DOTCOM_DIM d1
on I.PAGES = d1.PAGE_DESC
This is the example of date stored in varchar(50) field : 2014-04-03
Thanks in advance
In case it would be helpful, here's a query that should allow you to identify the rows with invalid dates:
select
*
from
#login t1
left outer join sys_calendar.calendar t2
on t1.start_date = cast (cast(t2.calendar_date as date format 'YYYY-MM-DD') as char(10))
where t2.calendar_date is null
Any rows that return from this query will have invalid dates.
I think all you need is the format statement
examples here
SEL
CUSTOMER_ACCOUNT_ID,
cast(((I.start_date (date, format 'yyyy-mm-dd'))(char(10))) as LOB_START_DATE,
In more recent versions of Teradata there is a TRYCAST() function. This function will attempt to cast the data and return NULL if the conversion fails, instead of failing the statement.
INSERT INTO TEMP_TABLES.FACT
(
CUSTOMER_ACCOUNT_ID,
LOB_START_DATE,
)
SELECT Customer_Account_ID
, TRYCAST(I.Start_Date AS DATE) AS LOB_START_DATE
FROM #LOGIN I
LEFT JOIN JOURNEY_TABLE.DOTCOM_DIM d1
ON I.Pages = d1.Page_Desc;

Resources