Could you please help me to write a hive query to find total number of days,number of week days and number of week ends in the current month.
Thanks in advance.
With this something like this you obtain the number of days in the current month:
SELECT datediff(CONCAT(y, '-', (m + 1), '-', '01'), CONCAT(y, '-', m, '-', '01')) FROM (SELECT month(current_date) as m, year(current_date) as y, day(current_date)) t
Related
Currently, I am using this code to look at the previous month's data for quicksight in Amazon's Athena (this first part works*):
SELECT month, count(1)
FROM table1
WHERE CAST(EXTRACT(month from now()) - 1 as VARCHAR(2)) = month
GROUP BY month
The challenge is how to ensure that this code will work once we roll over into a new year? I currently have
SELECT month, count(1)
FROM table1
WHERE CASE WHEN( month = '1' THEN month = '13'
ELSE month
END)
CAST(EXTRACT(month from now()) - 1 as VARCHAR(2)) = month
GROUP BY month
To clarify, month was input as a string, hence the CAST as VARCHAR(2) to get "01" through "12".
My thought process behind this was that if month = '01', then it reads it as '13', then extracts '1', equaling '12'. But not sure if that will work
You can use the date_add function to subtract one month from today:
SELECT DATE_ADD('month', -1, NOW())
Alternatively you can subtract an interval of one month to achieve the same results:
SELECT NOW() - INTERVAL '1' MONTH
In both cases you can then use MONTH(…) or EXTRACT(MONTH FROM …) to get the month number.
You seem to want:
where month = extract(month from now()) - 1 or
(extract(month from now()) = 1 and month = 12)
I wish to get the first and the last day of the month with
select CONVERT(varchar,dateadd(d,-1,dateadd(m,1,cast(cast('201812' as varchar(6))+'01' as date))),112)
Actually I can get the last day of month but I don't know how can I get the first day of month.
If you are starting with '201801asyyyymm`, then use:
select convert(date, yyyymm + '01') as first_day_of_month,
eomonth(convert(date, yyyymm + '01')) as last_day_of_month
I have a table with data and columns containing a date but separated into several columns :
a column with the year,
a column with the month,
a column with the day.
I would like to keep only the data with a date that is less than 2 months.
I have tried to concat the date but the month and the day does not always have 2 characters. Sometimes it is one number : 1 for january for example.
Could you give some tips to make this request?
Thanks in advance,
select *
from etude
where concat(year,month,day) > NOW()
It is not working as expected
The TIMESTAMP_FORMAT() function should be robust to months/days being either one or two digits:
SELECT *
FROM etude
WHERE TIMESTAMP_FORMAT(CONCAT(year, month, day), 'YYYYMMDD') > NOW();
But note that while this may fix your immediate problem, a much better long term solution would be to stop storing the various components of your date in separate columns. Instead, just maintain a single bona-fide date/timestamp column.
Use lpad function along with timestamp_format function
select *
from etude
where timestamp_format(year || lpad(month,2,'0') || lpad(day,2,'0'), 'YYYYMMDD') > now();
If keep only the data with a date that is less than 2 months means keep only the data with a date that is not earlier than 2 months from the current date, then try this as is:
SELECT dt
FROM
(
SELECT date(digits(dec(year, 4)) || '-' || digits(dec(month, 2))|| '-' || digits(dec(day, 2))) dt
FROM table
(
values
(2019, 1, 1)
, (2019, 7, 13)
) etude (year, month, day)
)
WHERE dt > current date - 2 month;
to combine your 2 questions into 1 answer and using Satya's answer
select *
from etude
where timestamp_format(year || lpad(month,2,'0') || lpad(day,2,'0'), 'YYYYMMDD') > now() - 2 months;
This is another option
select *
from etude
where DATE('0001-12-31') + (year -2) YEARS + month MONTHS + day DAYS > CURRENT DATE
How I can get week of quarter using Oracle SQL? I get week of month and week of year using simple function:
WITH SYSDATE_TABLE AS (
SELECT to_date('01-06-2015', 'dd-mm-yyyy') as date_dt
FROM DUAL
)
SELECT
to_char(DATE_DT,'W') as WEEK_OF_MONTH,
to_char(to_date(DATE_DT),'WW') as WEEK_OF_YEAR
FROM
SYSDATE_TABLE;
I get month of quarter using MOD function:
(CASE mod(to_number(to_char(DATE_DT, 'MM')), 3)
WHEN 0 THEN 3
WHEN 1 THEN 1
WHEN 2 THEN 2
END) AS MONTH_OF_QUARTER
But I don't know how I can get week of quarter. Does anyone has an idea how to solve this problem?
How about
select 1 + TO_CHAR(DATE_DT, 'WW') - TO_CHAR(TRUNC(DATE_DT, 'Q'), 'WW')
from SYSDATE_TABLE;
According to your explanation: First seven days of every quarter is first week. The next seven days is second week etc. you could try this one:
select 1 + TRUNC( (DATE_DT - TRUNC(DATE_DT, 'Q')) / 7 )
from SYSDATE_TABLE;
i have to get the last day of the current month. how can i get
SQLite (sqlite3)
Compute the last day of the current month.
SELECT date('now','start of month','+1 month','-1 day');
checkout this link as well if your using sqlite3
sqlite datetime functions
set dateformat dmy
select dateadd(d, -1, dateadd(m, 1, '01-' + convert(varchar, month(getdate())) + '-' + convert(varchar, year(getdate()))))
add one month to the first of this month, minus one day.
SQLITE QUERY FOR CALCULATE
NUMBER OF DAY IN A CURRENT MONTH
select strftime('%d',datetime('now','start of month','+1 month','-1 second'));
An alternative: add a month to the date, then subtract the resulting day value of the new month:
select day(dateAdd(d, -day(dateAdd(m,1,'2012-01-31')), dateAdd(m, 1, '2012-01-31')))
Oracle
SELECT LAST_DAY(SYSDATE) AS Last_day_of_month FROM DUAL;
SQL Server
SELECT DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE())+1,0)) AS Last_day_of_month