I am looking for help to convert below sql query into hive supported date format. kindly assist.
GP: SQL
select to_date('19800302000000','yyyymmddhh24miss') date_of_birth
GP Output : 1980-03-02
GP query :
extract(year from age(current_date-1, to_date(b.birthday,'yyyymmddhh24miss'))) age
we are looking similar out in hive. please help us.
For select to_date('19800302000000','yyyymmddhh24miss') use this
select from_unixtime(unix_timestamp('19800302000000','yyyyMMddhhmmSS')).
If you dont want time part, use this
select to_date(from_unixtime(unix_timestamp('19800302000000','yyyyMMddhhmmSS'))).
For extract(year from age(current_date-1, to_date(b.birthday,'yyyymmddhh24miss'))) age
use below code. it should give difference of years between yesterday and DOB.
select
year(current_date() - interval 1 day ) -
year(from_unixtime(unix_timestamp('19800302000000','yyyyMMddhhmmSS'))) age
Related
I am currently working with a dataset for which i have to calculate the ages of the employees, however the format of the birth date is the following:
1987-05-17T00:00:00
How do I remove the 'T00:00:00' from the Birth date?
From there on I would like to calculate the age, which i tried to do in this way already:
FLOOR(DATE_DIFF(CAST(CURRENT_DATE() AS DATE)), CAST(Birthdate AS DATE))) AS Age
However, when i keep getting the following error:
Error: No matching signature for function DATE_DIFF for argument types: DATE. Supported signature: DATE_DIFF(DATE, DATE, DATE_TIME_PART) at [27:9]
I think this error arises as a consequence of the weird date format to begin with. But im not entirely sure.
Can anyone help me solve this problem?
Below is for BigQuery Standard SQL
FLOOR(DATE_DIFF(CURRENT_DATE(), PARSE_DATE('%Y-%m-%dT00:00:00', Birthdate), YEAR)) AS Age
This is a query I use to calculate age in years, months and days:
SELECT
username
,dob
,DATE_FORMAT(CURDATE(), '%Y') - DATE_FORMAT(dob, '%Y') - (DATE_FORMAT(CURDATE(), '00-%m-%d') < DATE_FORMAT(dob, '00-%m-%d')) AS years
,PERIOD_DIFF( DATE_FORMAT(CURDATE(), '%Y%m') , DATE_FORMAT(dob, '%Y%m') ) AS months
,DATEDIFF(CURDATE(),dob) AS days
FROM users
Below is what worked for me in bigquery to get age
FLOOR(DATE_DIFF(CURRENT_DATE(), CAST(Birthdate AS DATE), day/365) AS Age
I have been battling for two days now, please could someone give me a bit of assistance on below. I am trying to select data where a date field/column must equal today's date.
SELECT *
FROM stock
WHERE DATE(PREVSELLPRICE1DATE)=DATE(now());
Please assist if you can, I need to rollout this report.
it is better not to manipulate DATE column using functions like TRUNC to mach the date without hour precision (matching year-month-day), it recommended for performance to use something like:
SELECT *
FROM stock
WHERE PREVSELLPRICE1DATE between trunc(sysdate) and trunc(sysdate+1)
this way you'll compare for the required day only + the TRUNC function will be applied only 2 times instead of on each row.
For sql server below is fine:
SELECT *
FROM stock
WHERE CAST(PREVSELLPRICE1DATE as date) = CAST(GETDATE() as date)
Below script
select cast(getdate() as date)
will give you result:
2017-06-29
How can I get the last day of a month in H2 SQL? In MySQL the following would work:
SELECT LAST_DAY(GETDATE())
Thank you.
EDIT:
Ended up with using the following:
SELECT TIMESTAMPADD(DAY, -DAY(TIMESTAMPADD(MONTH,1,GETDATE())), TIMESTAMPADD(MONTH,1,GETDATE()));
The reason for this is that it also support MySQL. Just replaced the functions from Vijaykumar's answer.
please try :
SELECT DATEADD(dd, -DAY(DATEADD(m,1,#Today)), DATEADD(m,1,#Today))
SELECT day(dateadd(dd,-day(ym_next),ym_next)) last_day_of_month
FROM (SELECT DATEADD(m,1,ym) ym_next
FROM (SELECT parsedatetime(concat(2016,'-',2,'-1'),'yyyy-MM-dd') ym)ym)ym
Suppose I have a date 2010-07-29. Now I would like to check the result of one day ahead. how to do that
For example,
SELECT *
from table
where date = date("2010-07-29")
How to do one day before without changing the string "2010-07-29"?
I searched and get some suggestion from web and I tried
SELECT *
from table
where date = (date("2010-07-29") - 1 Day)
but failed.
MySQL
SELECT *
FROM TABLE t
WHERE t.date BETWEEN DATE_SUB('2010-07-29', INTERVAL 1 DAY)
AND '2010-07-29'
Change DATE_SUB to DATE_ADD if you want to add a day (and reverse the BETWEEN parameters).
SQL Server
SELECT *
FROM TABLE t
WHERE t.date BETWEEN DATEADD(dd, -1, '2010-07-29')
AND '2010-07-29'
Oracle
SELECT *
FROM TABLE t
WHERE t.date BETWEEN TO_DATE('2010-07-29', 'YYYY-MM-DD') - 1
AND TO_DATE('2010-07-29', 'YYYY-MM-DD')
I used BETWEEN because the date column is likely DATETIME (on MySQL & SQL Server, vs DATE on Oracle), which includes the time portion so equals means the value has to equal exactly. These queries give you the span of a day.
If you're using Oracle, you can use the + and - operators to add a number of days to a date.
http://psoug.org/reference/date_func.html
Example:
SELECT SYSDATE + 1 FROM dual;
Will yield tomorrow's date.
If you're not using Oracle, please tell use what you ARE using so we can give better answers. This sort of thing depends on the database you are using. It will NOT be the same across different databases.
Depends of the DateTime Functions available on the RDBMS
For Mysql you can try:
mysql> SELECT DATE_ADD('1997-12-31',
-> INTERVAL 1 DAY);
mysql> SELECT DATE_SUB('1998-01-02', INTERVAL 31 DAY);
-> '1997-12-02'
If youre using MSSQL, you're looking for DateAdd() I'm a little fuzzy on the syntax, but its something like:
Select * //not really, call out your columns
From [table]
Where date = DateAdd(dd, -1, "2010-07-29",)
Edit: This syntax should be correct: it has been updated in response to a comment.
I may have the specific parameters in the wrong order, but that should get you there.
In PL SQL : select sysdate+1 from dual;
using a Oracle 10g db I have a table something like this:
create table x(
ID NUMBER(10) primary key,
wedding DATE NOT NULL
);
how can I
select * from x where wedding is in june 2008???
I know it is probably an easy one but I couldn't find any satisfying answer so far.
Help is very much appreciated.
Use:
SELECT *
FROM x
WHERE x.wedding BETWEEN TO_DATE('2008-JUN-01', 'YYYY-MON-DD')
AND TO_DATE('2008-JUL-01', 'YYYY-MON-DD')
Use of TO_DATE constructs a date with a time portion of 00:00:00, which requires the end date to be one day ahead unless you want to use logic to correct the current date to be one second before midnight. Untested:
TO_DATE('2008-JUN-30', 'YYYY-MON-DD') + 1 - (1/(24*60*60))
That should add one day to 30-Jun-2008, and then subtract one second in order to return a final date of 30-Jun-2008 23:59.
References:
TO_DATE
This is ANSI SQL, and supported by oracle as of version 9i
SELECT *
FROM x
WHERE EXTRACT(YEAR FROM wedding) = 2008
AND EXTRACT(MONTH FROM wedding) = 06
Classic solution with oracle specific TO_CHAR():
SELECT *
FROM x
WHERE TO_CHAR(wedding, 'YYYY-MMM') = '2008-JUN'
(the latter solutions was supported when dinosaurs still walked the earth)