This question already has answers here:
Alternate for decode function
(3 answers)
Closed 9 years ago.
I have a task to find the next business day of particular given date. It should exclude the holidays that are listed in holiday table (mst_holiday). I have a procedure using decode function to find the next business day when there is continuous holiday or weekend. For example if today is 21-07-2013 Wednesday, if I need to find 5 days later it should display 26-07-2013, if it is a holiday, it should display the successive non holiday.
The sample decode function I have used is below. Please help me to write alternate sql query.
DECODE
(ldate,
laps.holidaydate, DECODE
(ldate + 1,
laps.holidaydate + 1, DECODE
(ldate + 2,
laps.holidaydate + 2,ldate+3,ldate+2),ldate+1),ldate);`
Do this 3 steps:
1) Generate a range of dates you want to check for example:
select to_date(:start_date,'dd-mon-yyyy') + rownum -1
from all_objects
where rownum <= to_date(:end_date,'dd-mon-yyyy')-to_date(:start_date,'dd-mon-yyyy')+1
2) Select minus the table with the holidays table
3) Select ordered by date and get the first record
Put it all in one sql Select.
Related
My table has the following columns:
SerialNo
ProductNo
WarrantyBeginDt
WarrantyEndDT
I would like to get a monthly in warranty count looking back about 6 months. I know how to get a month by specifying in the where clause. Would like to have a query that generates the last 6 months with out having to specify the month in the where clause.
SELECT count(*)
FROM Supplemental_Warranty
WHERE WarrantyBeginDt <= '6-15-2022' AND WarrantyEndDt >= '6-15-2022'
How could I create a query that looks back 6 months from the current date?
UPDATED to add group by month for 6 months to get count by month.
This should give you 6 months worth of data using system date (subtract 6 months from today) and end date/time is now so you dont have to specify specific dates, just using date add functionality to subtract 6 months from stating date.
SELECT count(*), MONTH(WarrantyBeginDt) AS CountPerMonth
FROM Supplemental_Warranty
WHERE WarrantyBeginDt BETWEEN DATEADD(MONTH, -6, GETDATE()) AND GETDATE()
--if you have any flags or other logic to identify if it is underwarranty or not
AND IsUnderWarranty = 1
GROUP BY MONTH(WarrantyBeginDt)
NOTE: Not tested but this should do it depending on your SQL technology.
I wish to find data from where current date and also include data from the last 2 financial years
Basically I want data going back from 'today' to 01/04/2019. I want to add this line of data to dynamically happen, not just a one off for the future
Thus if the date today was 05/06/2022, I would want the date going from that date, backwards until what would be 01/04/2020
The column name is Date - please help
I am quite sure that it should be possible to do it in some simpler way, but you can use something like below:
declare #b date
set #b = datefromparts(year(getdate()),4,1) --first daty of fiscal year in current calendar year
select case when #b<getdate() then dateadd(year,-2,#b) else dateadd(year,-3,#b) end
This question already has an answer here:
SQL Server Management Studio make default date 5 days from now
(1 answer)
Closed 4 years ago.
I need to create a view everyday with all records reaching the "Prazo" date ('Prazo' stands for due date in Portuguese), so everyday I need to make a select showing all the records that are 5 days from reaching the due date. How can I do that?
You can do:
Select *
from Table as t
Where DATEDIFF(DD, GETDATE(), PrazoDate) = 5
It just says how many records from today are 5 days from reaching their due date.
This question already has answers here:
SQL Query to find the last day of the month
(15 answers)
Closed 5 years ago.
I have a table with a list of clients, with a column that shows the date their accounts were created. I want to make the last day of this date. I tried using the EOMONTH function, but it does not work.
For instance, if client 1 came on January 6th, and client 2 came on February 6th of this year, I want it to show 31-01-2018 and 28-02-2018.
Any ideas?
Thanks.
Gordon is right, I have tried EOMONTH for your given data and it returns perfect value look at below, in second parameter you have to pass value as per requirement if you pass value as 1 it will give you last date of next month i.e Jan+1=Feb
DECLARE #myDate Datetime='06-Jan-2018'
SELECT EOMONTH(#myDate,0) AS MyDate
If your version not support the EOMONTH function then try below query:
SELECT CAST(DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,#myDate)+1,0)) AS DATE)
Output:
I have 2 parameters in my report, Month and Year.
I also have a table which in one column has the count of rows for that particular month and another column which has the count of rows for that specific month and the next 5 months (Rolling 6 month).
Both of these columns have an expression which links to the parameters as shown below:
Month Expression:
6 Month Expression:
My parameters use specific values which i have put in (1 - 12) so when my rolling month adds 1 value each time as you can probably guess this wont work when I come near the latter months of the year as my expression does not roll over to next year and just stops at December.
Overview: Parameters are integer values 1 - 12 and I am not sure how to calculate a rolling 6 months which will carry over to the next year when selecting months late in the year. e.g. If I select November as my Month parameter, my rolling 6 months will only display the sum of rows for November and December, not going to the next year. I am assuming this is because my month values are integers and in my expression I add numbers to the integers for each month therefore my rolling 6 will be trying to add months 11, 12, 13, 14, 15, 16 when obviously months only go to 12.
This is not an answer to your question, but looking at this, you may be able to find a solution with SQL itself.
See the query below. Consider this as your report source sql.
DECLARE #A TABLE (ID INT IDENTITY(1,1),DT DATE)
INSERT INTO #A (DT)
VALUES
('2013-01-26'),('2013-02-23'),('2013-03-20'),
('2013-04-23'),('2013-05-23'),('2013-07-23'),
('2013-08-23'),('2013-08-29'),('2013-09-23'),
('2013-12-10'),('2014-03-01')
If you join the result with itself, some what like below, you will get the result in sql query itself, you need to only show it in report. See below.
SELECT DATEPART(YEAR,DT) [Y],DATEPART(MONTH,DT) [M],COUNT(*) [ROLLING 6]
FROM (
SELECT A.*
FROM #A A,#A B
WHERE ((DATEPART(YEAR,B.DT) * 12) + DATEPART(MONTH,B.DT)) BETWEEN
(DATEPART(YEAR,A.DT) * 12) + DATEPART(MONTH,A.DT) AND
((DATEPART(YEAR,A.DT) * 12) + DATEPART(MONTH,A.DT) + 6)) LU
GROUP BY DATEPART(YEAR,DT),DATEPART(MONTH,DT)
ORDER BY [Y],[M]