How to check the type of a column in impala? Query is
select TO_DATE(FROM_UNIXTIME(UNIX_TIMESTAMP(),'yyyy-MM-dd'))
Also I want to format the result of the query as a string yyyyMMdd. CAST returns null. Any tips?
Could you try below query in impala.
Case1:-
select cast(TO_DATE(FROM_UNIXTIME(UNIX_TIMESTAMP(),'yyyy-MM-dd')) as string);
Result
2018-03-16
Case2:-
select cast((FROM_UNIXTIME(UNIX_TIMESTAMP(),'yyyyMMdd')) as string);
Result
20180316
Case3:-
select cast(regexp_replace(TO_DATE(FROM_UNIXTIME(UNIX_TIMESTAMP(),'yyyy-MM-dd HH:mm:ss')),'-','')as string);
Result
20180316
You can use regexp_replace to replace all the - with empty string so that we can have yyyyMMdd format.
I don't get exactly your question on "check the type of a column". TO_DATE function is returning the result as string.
For your 2nd question, you can use current_date to get the server date then use a function called "DATE_FORMAT". By the way, Unix timestamp is useful if you want up to hours, mins, seconds and micro seconds and current_date function is sufficient if there is no time required.
SELECT DATE_FORMAT(CURRENT_DATE, 'yyyyMMdd');
Result: 20180316
Hi use the following script
select from_timestamp('2020-01-01','yyyy-MM-dd')
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'm looking to get a result in Amazon Athena were I can count the quantity of users created by day (or maybe by month)
But previous that I have to convert the unix timestamp to another date format. And this is where i fail.
My last goal is to convert this type of timestamp:
1531888605109
In something like:
2018-07-18
According to Epoch Converter
But when I try to apply the solution i saw in this quiestion: Casting unix time to date in Presto
I got the error:
[Simba]AthenaJDBC An error has been thrown from the AWS Athena client. SYNTAX_ERROR: line 1:13: Unexpected parameters (varchar) for function from_unixtime. Expected: from_unixtime(double) , from_unixtime(double, bigint, bigint) , from_unixtime(double, varchar(x)) [SQL State=HY000, DB Errorcode=100071]
This is my query:
select cast(from_unixtime(created)as date) as date_creation,
count(created)
from datalake.test
group by date_creation
Maybe I've to cast over the sctring because the data type of the field is not a date.
My table description: Link to the table description
line 1:13: Unexpected parameters (varchar) for function from_unixtime. Expected: from_unixtime(double)
This means that your timestamps -- even though they appear numeric -- are varchars.
You need to add a CAST to cast(from_unixtime(created)as date), like:
CAST(from_unixtime(CAST(created AS bigint)) AS date)
Note: When dealing with time-related data, please have in mind that https://github.com/prestosql/presto/issues/37 is not resolved yet in Presto.
I've a requirement to truncate time from datetime in Presto.
I noticed that I cannot use Trunc() function which is used in Redshift to achieve the same.
date_trunc() returns a timestamp again resetting to the start.
Cast the datetime to date
CAST(x AS DATE)
date(x) -- This is an alias for CAST(x AS DATE).
IF the date/time values to be converted are held as strings then such a conversion might fail; in which using TRY_CAST() is recommended as this returns NULL if the conversion fails.
I have the following function:
CREATE function SEMANA_ISO (fecha date) returns text as $$
select to_char(fecha, 'mm-dd-yyyy');
$$ LANGUAGE sql;
It works with:
Select SEMANA_ISO ('28/12/2014');
Select SEMANA_ISO ('01/01/2015');
Select SEMANA_ISO ('01/07/2015');
As you may see below
But not with:
Select SEMANA_ISO ('12/31/2014');
It shows:
********** Error **********
ERROR: The value of time / date is out of range "12/31/2014"
SQL state: 22008
Hint: You may need a different configuration of "dateStyle".
Character: 20
Do you have any suggestion without having to change the datestyle so I can enter
Select SEMANA_ISO ('12/31/2014');
And get an output of:
12-31-2014
using just one function to "parse" all dates?
Your function is declared to get a parameter of type date so you also need to pass such a value. '12/31/2014' is a character value, not a date.
When you pass a character literal (aka string) to the function Postgres is forced to do an implicit data type conversion based on the current datestyle - not something you should rely on.
If you want to call the function independently of the datestyle you need to pass a proper date literal, e.g. DATE '2014-12-31'
For more details on specifying date values, please see the manual: http://www.postgresql.org/docs/current/static/datatype-datetime.html#DATATYPE-DATETIME-INPUT
If you want 12/31/2014 accepted as a date set datestyle to iso, dmy;
if you want 31/12/2014 accepted as a date set datestyle to iso, mdy;
you can't have both at the same time, else 4/1/2015 is ambiguos, and must be rejected.
There is to_date() to input date literals with an arbitrary (given) format:
SELECT to_date('12/31/2014', 'MM/DD/YYYY') AS date1
, to_date('31/12/2014', 'DD/MM/YYYY') AS date2;
That's "without changing any datestyle". Obviously, you need to provide a matching format pattern, though.
To output the same in any desired format, use to_char():
SELECT to_char(to_date('12/31/2014', 'MM/DD/YYYY'), 'DD/MM/YYYY') AS date_as_text
I am working with a SQL Server 2005 database table that is currently storing dates as varchars. This is outside of my control. For ease of reporting, I would like to create a view that converts these varchar dates to datetime fields.
The varchar dates are formatted for the most part, except for the occasional typing error.
DateString
----------
2001/01/02 -- most of the fields
2002/0601 -- typo, missing slash between month and day
2004/02/30 -- typo, no 30th of February
Because the dates are already formatted, I'm using the cast function to convert them to datetime.
cast(DateString as datetime)
The problem is when the cast function comes across an incorrect date, the query ends in error.
Is there are way to wrap just the function in a try...catch block? I see it is possible to wrap an entire query in a try...catch block, but the full query has multiple casts that must be done, and any combination could have typing errors.
I would use the built-in ISDATE() function. You can then write a CASE statement within your SELECT statement to either return the parsed date or either a null or some other result. Or, you can place it directly in the WHERE clause to only return those rows where there is a valid date.
A possible solution with the ISDATE() function in the SELECT list may look like:
select case
when ISDATE(DateString) = 1 then cast(DateString as datetime)
else null --or other error result
end as CastedDate
from TableName
If the logic is complicated (e.g. you want to try to correct the errors, such as missing slash, or nearest day in the case of 30th FEB), then one option is to create a user-defined function that contains the date parsing logic (string to date) logic in it, complete with error handling (e.g. scenario checking before casting). Then in the query, call the user defined function.
An outline:
CREATE FUNCTION udf_ParseDateString
(
#DateString nvarchar(20)
)
RETURNS DateTime
AS
BEGIN
DECLARE #returnDateTime datetime
-- Do any string checking, and date casting here
-- #DateString -> #returnDateTime
return #returnDateTime
END
Note that you won't be able to use TRY-CATCH in a UDF.
Alternatively, if your logic is simple, you could just use a cast inline, as suggested here.
SET DATEFORMAT ymd;
-- Incorporate this into query.
SELECT CASE WHEN ISDATE(#yourParameter) = 1
THEN CAST(#yourParameter AS DATETIME)
ELSE YourDefaultValue
END
If you were using SQL Server 2012, you could use the TRY_CAST function.