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
Related
I am trying to calculate wages data. Is there a way to calculate the number of days in a given month in sqlite3
There are multiple ways to do it, but you can use JULIANDAY to calculate the difference in days between the date in a month and the current date, which should give you the number of days of the current month.
For example to calculate the number of days in the month that contains the date 2010-05-31;
SELECT JULIANDAY('2010-05-31', '+1 month') - JULIANDAY('2010-05-31') days_of_month
> 31
An SQLfiddle to test with.
Use the DATE function to get from some date to its month's last day, e.g. from '2021-06-05' to '2021-06-30'. Then use STRFTIME to extract that day ('30' in the example). Then cast to INTEGER to get from the day string to a number ('30' -> 30).
SELECT
CAST(
STRFTIME(
'%d',
DATE(
'2021-06-05',
'start of month',
'+1 month',
'-1 day'
)
) AS INTEGER
);
Demo: https://dbfiddle.uk/?rdbms=sqlite_3.27&fiddle=45d8620632c831d554b435b501f98912
Assuming that you have the year and the month as a number, you can do it with strftime():
SELECT strftime('%d', '2021' || '-' || printf('%02d', ?) || '-01', '1 month', '-1 day') days
Replace '2021' with the year that you want and ? with the month number.
See the demo.
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)
How to get the current month end date through db2 query.
I should not need to modify the query for every month
Every month the sql is executing so it need to automatically take care
In Db2 11.1 this could be a solution:
select next_month(current date) - 1 day from sysibm.sysdummy1
Next_Month will return the first day of the next month from the data provided.
SELECT LAST_DAY(CURRENT_DATE) AS LASTDAY
FROM SYSIBM.SYSDUMMY1;
The query is working
LASTDAY
2019-04-30
The old way would be
CURRENT DATE - (DAY(CURRENT DATE)) DAYS + 1 MONTH
To get the last day of the current month, use this:
SELECT DATEADD (dd, -1, DATEADD(mm, DATEDIFF(mm, 0, GETDATE()) + 1, 0))
This will format the date for you
SELECT CONVERT(VARCHAR(10), DATE, 12)
See this for more information regarding the '12'. 12 is the right code
Another solution is to use TIMESTAMP_FORMAT as such:
TIMESTAMP_FORMAT('190404', 'YYMMDD')
I need to get last day from next month in this select.
select (last_day(month from :date+1)||'.'||
(extract(month from :date)+1)||'.'||
extract(year from :date))
from dual;
Everything is ok with month and year but I have a problem with last day function.
Using (extract(month from :date)+1) will not work for a date in December as you will end up with a 13th month. Similarly, extract(year from :date) will get you the current year - which may not be correct if you are looking for the last day of the next month from December and the result should be the January of the following year.
You do not need to extract all the fields separately, you can do it all in a single statement:
SELECT TO_CHAR(
LAST_DAY( ADD_MONTHS( :date, 1 ) ),
'DD.MM.YYYY'
)
FROM DUAL
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