systate()/current_date() not working in HQL - hive

while doing this operation in HQl i am getting
java sql exception:error 10011 - invalid function sysdate(). getting same for current date too..
WHERE TO_DATE (CONCAT(year,'-',month,'-',day)) >= sysdate()-8
AND TO_DATE (CONCAT(year,'-',month,'-',day)) <= sysdate()-2
how i can rewrite it?
help me out of this

as of hive 1.2.0 you can use current_date() , on previous versions it can be achieved with >=date_sub(from_unixtime(unix_timestamp()),8) f.i.

You should be able to use unix_timestamp() / from_unixtime(unix_timestamp()) (in your case) according to hive manual no matter which version of hive you are using

You can Use the Condition in where clause :
(((unix_timestamp(usage_dt,'yyyy-MM-dd')) >= (unix_timestamp(date_sub(FROM_UNIXTIME(UNIX_TIMESTAMP(),'yyyy-MM-dd') ,8),'yyyy-MM-dd'))) and ((unix_timestamp(usage_dt,'yyyy-MM-dd')) >= (unix_timestamp(date_sub(FROM_UNIXTIME(UNIX_TIMESTAMP(),'yyyy-MM-dd') ,2),'yyyy-MM-dd'))))
Here usage_dt is you column name for date, and the DateFormat In Hive used as "yyyy-MM-dd" format so if you are havig any other format you can mention as here yyyyMMdd is the customr format of the sate.

Related

GreenPlum SQL to HiveSQL migration of query statement

We have to migrate the GreenPlum SQL to HIVESQL as below statement, kindly help us.
to_date(b.birthday,'yyyymmddhh24miss')
extract(year from age(iDATE, to_date(b.birthday,'yyyymmddhh24miss')))
Above the two statement needs to convert SQL to HIVE. please help me.
hive version : 3.1.X
Equivalent of to_date(b.birthday,'yyyymmddhh24miss')
is
from_unixtime(unix_timestamp(b.birthday,'yyyyMMddHHmmss'))
However, i do not understand this extract(year from age(iDATE, to_date(b.birthday,'yyyymmddhh24miss'))). Do you need difference of iDate and birthdate ?
If yes, you can use to get date difference in years.
datediff(iDate,from_unixtime(unix_timestamp(b.birthday,'yyyyMMddHHmmss')) )/365
Hive also has extract (year from datetimestamp) functions.
One more method of extracting timestamp is using regexp_replace:
regexp_replace(b.birthday,'^(\\d{4})(\\d{2})(\\d{2})(\\d{2})(\\d{2})(\\d{2})$','$1-$2-$3 $4:$5:$6')
And age in years is:
cast(datediff(current_date,
regexp_replace(b.birthday,'^(\\d{4})(\\d{2})(\\d{2})(\\d{2})(\\d{2})(\\d{2})$','$1-$2-$3 $4:$5:$6')
)/365.25 as int)

How to extract day/month/year etc from varchar date field, using Presto?

I currently have tables with dates, set up as VARCHAR in the format of YYYY-MM-DD such as:
2017-01-01
The date column I'm working with is called 'event_dt'
I'm used to being able to use day(event_dt), month(event_dt), year(event_dt) etc. in Hive, but Presto just gives me error executing query with no other explanation when the queries fail.
So, for example, I've tried:
select
month(event_dt)
from
my_sql_table
where
event_dt = '2017-01-01'
I would expect the output to read:
01
but all I get is [Code: 0, SQL State: ] Error executing query
I've tried a few other methods listed in the Presto documentation but am having no luck at all. I realize this is probably very simple but any help would be much appreciated.
You can use the month() function after converting the varchar to a date with the date() function:
presto> select month(date('2017-01-01'));
_col0
-------
1
(1 row)
Thanks to #LukStorms in the comments to the original question, I've found two solutions:
Using month(cast(event_dt as date))
Using month(date(event_dt))

sql teradata filtering on date - database version Teradata 15.10.06.02 and provider version Teradata.Net 15.11.0.0

my table has a date column. its data type is date. I confirmed it by going to table name>>columns and it says MTH_END_DT [DATE, Not NULL]
I want to filter my data for a particular date. If I put a condition where MTH_END_DT = '6/1/2018' I get an error select failed [3535] A character string failed conversion to a numeric value.
I followed this page. I used where MTH_END_DT = date '6/1/2018' and i get an error syntax error invalid date literal
I tried where cast(timestamp_column as date) = date '2013-10-22'; something like this and it throws error too
How should i filter my data?
There's only one reliable way to write a date, using a date literal, date 'yyyy-mm-dd'
where MTH_END_DT = DATE '2018-06-01'
For a Timestamp it's
TIMESTAMP '2018-06-01 15:34:56'
and for Time
TIME '15:34:56'
In SQL Assistant it's recommended to switch to Standard SQL format YYYY-MM-DD in Tools-Options-Data Format-Display dates in this format
I did have the similar problem when I was filtering a particular date for my query with Teradata. First method I tried was putting 'DATE' term as the following:
WHERE saledate = DATE'04/08/01' but this did not solve the problem.
I then used an approach I stumbled upon when surfing, finally it worked.
WHERE extract(year from saledate)=2004 AND extract(MONTH from saledate)=8 AND extract(DAY from saledate)= 1 source
I think this really should not be this long, but it worked.
It seems to me it’s most likely you have input the date format incorrectly? Maybe it includes a time by default.
For example
where MTH_END_DT = ‘2013-10-22-00:00:00:00’

ORA-01843 not a valid month- Comparing Dates

I have a problem when try to select data from a table filtering by date.
For example:
SELECT * FROM MYTABLE WHERE MYTABLE.DATEIN = '23/04/49';
The Oracle Error is:
Informe de error:
Error SQL: ORA-01843: mes no válido
01843. 00000 - "not a valid month"
*Cause:
*Action:
Probably the source data of table is corrupted, in this case:
How can i solve this problem?
Can I change this dates for null?
The results of this select, select * from nls_session_parameters; , is:
PARAMETER VALUE
------------------------------ ----------------------------------------
NLS_LANGUAGE SPANISH
NLS_TERRITORY SPAIN
NLS_CURRENCY ¿
NLS_ISO_CURRENCY SPAIN
NLS_NUMERIC_CHARACTERS ,.
NLS_CALENDAR GREGORIAN
NLS_DATE_FORMAT DD/MM/RR
NLS_DATE_LANGUAGE SPANISH
NLS_SORT SPANISH
NLS_TIME_FORMAT HH24:MI:SSXFF
NLS_TIMESTAMP_FORMAT DD/MM/RR HH24:MI:SSXFF
NLS_TIME_TZ_FORMAT HH24:MI:SSXFF TZR
NLS_TIMESTAMP_TZ_FORMAT DD/MM/RR HH24:MI:SSXFF TZR
NLS_DUAL_CURRENCY ¿
NLS_COMP BINARY
NLS_LENGTH_SEMANTICS BYTE
NLS_NCHAR_CONV_EXCP FALSE
You should use the to_date function (oracle/functions/to_date.php
)
SELECT * FROM MYTABLE WHERE MYTABLE.DATEIN = TO_DATE('23/04/49', 'DD/MM/YY');
You are comparing a date column to a string literal. In such a case, Oracle attempts to convert your literal to a date, using the default date format.
It's a bad practice to rely on such a behavior, as this default may change if the DBA changes some configuration, Oracle breaks something in a future revision, etc.
Instead, you should always explicitly convert your literal to a date and state the format you're using:
SELECT * FROM MYTABLE WHERE MYTABLE.DATEIN = TO_DATE('23/04/49','MM/DD/YY');
If you don't need to check exact timestamp, use
SELECT * FROM MYTABLE WHERE trunc(DATEIN) = TO_DATE('23-04-49','DD-MM-YY');
otherwise, you can use
SELECT * FROM MYTABLE WHERE DATEIN = TO_DATE('23-04-49 20:18:07','DD-MM-YY HH24:MI:SS');
Here, you use hard code date,if you directly compare then you must use DD-MM-YY HH24:MI:SS else you might get ORA-01849: hour must be between 1 and 12.
I know this is a bit late, but I'm having a similar issue. SQL*Plus executes the query successfully, but Oracle SQL Developer shows the ORA-01843: not a valid month error.
SQL*Plus seems to know that the date I'm using is in the valid format, whereas Oracle SQL Developer needs to be told explicitly what format my date is in.
SQL*Plus statement:
select count(*) from some_table where DATE_TIME_CREATED < '09-12-23';
VS
Oracle SQL Developer statement:
select count(*) from some_table where DATE_TIME_CREATED < TO_DATE('09-12-23','RR-MM-DD');
Just in case this helps, I solved this by checking the server date format:
SELECT * FROM nls_session_parameters WHERE parameter = 'NLS_DATE_FORMAT';
then by using the following comparison (the left field is a date+time):
AND EV_DTTM >= ('01-DEC-16')
I was trying this with TO_DATE but kept getting an error. But when I matched my string with the NLS_DATE_FORMAT and removed TO_DATE, it worked...
In a comment to one of the answers you mention that to_date with a format doesn't help. In another comment you explain that the table is accessed via DBLINK.
So obviously the other system contains an invalid date that Oracle cannot accept. Fix this in the other dbms (or whatever you dblink to) and your query will work.
Having said this, I agree with the others: always use to_date with a format to convert a string literal to a date. Also never use only two digits for a year. For example '23/04/49' means 2049 in your system (format RR), but it confuses the reader (as you see from the answers suggesting a format with YY).
If the source date contains minutes and seconds part, your date comparison will fail.
you need to convert source date to the required format using to_char and the target date also.
If you are using command line tools, then you can also set it in the shell.
On linux, with a sh type shell, you can do for example:
export NLS_TIMESTAMP_FORMAT='DD/MON/RR HH24:MI:SSXFF'
Then you can use the command line tools and it will use the specified format:
/path/to/dbhome_1/bin/sqlldr user/pass#host:port/service control=table.ctl direct=true
Try using:
SELECT *
FROM MYTABLE
WHERE MYTABLE.DATEIN is not null
AND MYTABLE.DATEIN = '23/04/49';
Use the month as a string.
Example:
(12-Apr-2002) or (12-April-2002)
Although the answers using TO_DATE are correct, I prefer to use the ANSI SQL format for dates:
DATEIN = DATE '1949-04-23'
It works in Oracle and other DBMS ANSI SQL compliant. This is specially important if your application is DBMS agnostic.
Try alter session set NLS_DATE_FORMAT='DD/MM/YY'; -- or whatever format you want
I faced the same problem, on PROD, all the code were already in this format, but on preprod, it's not set,
So this means you change the default date format used by oracle
ALTER session set NLS_LANGUAGE=’AMERICAN’;

Query from oracle doesn't work in SSIS

For Some reason this query doesn't work in SSIS
Select
IDLOGARCHIVOS,
NOMBREARCHIVO,
FECHACREACION
from
ATEN_TDCMARK.LOGARCHIVOS
where
trunc(FECHACREACION, 'DDD') = trunc(sysdate, 'DDD')
But when I tested in Toad works great what am I doing wrong? Is there a different way to treat dates? And I know that the problem it's related with the comparison of dates because when I remove this condition the query returns values
This works in Toad? I would have expected it to fail due to to_date(sysdate, 'dd/mm/yyyy'). You're trying to convert a date into a date, which Oracle normally raises an error on.
What you seem to want to do is truncate the date to include year, month and day only, i.e. no time component.
This would be trunc(sysdate, 'DD'). trunc(sysdate) would return the same value as 'DD' and it's synonyms are the default, but it's nice to be explicit.
Are you doing the same with your column FECHACREACION? If not and it's a character can I recommend that you store it as a date?
for system date you have to use GETDATE() function in ssis in place of sysdate..
for more information
First To solve my problem I need to modify NS_LANG in oracle registry on my machine locate at (HKEY_LOCAL_MACHINE\SOFTWARE\ORACLE\HOME0). Then I restart Machine and base on the answer of #Ben I modified my query and works in the etl OF SSIS
Select
IDLOGARCHIVOS,
NOMBREARCHIVO,
FECHACREACION
from
ATEN_TDCMARK.LOGARCHIVOS
where
trunc(FECHACREACION, 'DDD') = trunc(sysdate, 'DDD')