Auto Date Change for monthly reports - sql

pretty new to posting here so be gentle :p
I run monthly queries for SOX compliance reporting. My manager wants me to adjust the query so that we do not have to manually change the date range to the applicable month every time we run them. What would be method to automate this task so I dont have to manually change the date range prior to running the query. I have a .bat .log and spool my results to a .csv using sqlplus. Thanks for any help!!

You can use the getdate() function to return todays date, and then build from there.
WHERE MONTH(thingDate) = MONTH(getdate())
and YEAR(thingDate) = YEAR(getdate())

The last_day function is helpful for retrieving the last day of the current month. There is no simple function for the first day, but a simple function such as this will do the trick:
to_char(TRUNC(SYSDATE, 'MM'), 'DD-MON-YYYY')
So using both of those with the BETWEEN operator in your WHERE clause will get you where you need to be. You'll just need to possibly change the date format for your situation.

Related

Is there a way to use a variable/combo box to select a year in a date without using date picker

I am working on a calculator to track my (and my coworkers may use it) overtime at work. We have a bunch written in excel, but I am trying to do one in access. There is probably a simple solution, but having searched for a couple days for using a variable in a date has only given me how to use the date as a variable. So our pay periods are defined 1/1/yyyy to 1/15/yyyy etc. Hour pay is calculated on the number of days we have in a pay period which varies every year by days off. I have it calculating the proper number of days using VBA, but I am having difficulty getting it to use the combo box year which defaults to current year.
I have tried setting the control source to = 01/01/[cmbYearselect]
using variables, creating a string in vba and format date
Any help would be appreciated as I am going to have to use the variable in both the control sources and vba code.
Thank you
01/01/[cmbYearSelect] results in 12/30/1899 on my form
everything else has resulted in a type mismatch for the variables etc.
Use CDate with no year:
=CDate(#01/01#)
or:
=CDate("01/01")
to have primo of the current year.

Get date today, if no data - get data from yesterday (dynamic)

I have such an issue. I have a report that is built around today's date. We see data only from today. On Hand units from today.
However, we have many import issues and sometimes import fails and there is no data in this report. I was wondering if there may be a way to make it dynamic like to use MAX function with getdate() or anything similar. I always want to see data from today, however if there is no data available for today I would like to see the data from yesterday. If not from yesterday from a day before yesterday.
I need to change the date logic to just pull the data for the max date.
Does someone know how it can be done?
where sku.ohpost = cast(getdate()as date)
try this If column sku.ohpost type is date
where sku.ohpost = ( select max(sku.ohpost ) from mytable)

Remove One Month Data from a BigQuery Table via Query

I am trying to Remove data from One month (July) but I can't manage to write properly the SQL query in the console.
I have tried the following code and worked fine. I just need to find to way to write the range of one complete month (July).
DELETE
FROM Sandbox.SandboxTable
where Date = '2018-12-09T00:00:00'
I have tried different possibilities but had syntax errors. I will strongly appreciate any help!
DELETE
FROM Sandbox.SandboxTable
where
Date >= '2018-07-01T00:00:00' AND Date <= '2018-07-31T23:59:59'

Using #Prompt in sql using SAP BO WEBI 4.2 SP3

I'm running a series of reports where time window called in query is rolling, and individual per report.. Some reports look 400 days back, others look 10 weeks back, while others again look at -40days/+80days... and so on - many options.
All reports are scheduled in daily or weekly runs, meaning setting prompts will require a manual reset of prompt for every instance through the scheduler. Not optimal.
I know the universe designer can design specific filters to drag into the queries using the query designer, but with so many different options, I find it a bit of an issue that the universe designer should create specific filters for these specific purposes, adding a vast number of specific filters intended for specific use to various universes.
I'm after an option where it is possible to assign a calculation to a date field, which stay fixed for the purpose of the report for every scheduled instance.
For instance, looking at invoice date from 400 days before today and onwards would look like Where DIM_TIME_INV.DAY_DAY >= sysdate -400 - This I can hardcode into the SQl of the specific report, and it will stay through the scheduler and roll 1 day for every day the report is run. But if I decide to make a change in the query elements, the SQl is screwed, and I will have to manually add the modification to the SQL again.
I found an article reg. the use of #Prompt and would ask universe designer to try and sandbox this in our version of BO.
While I'm being impatient, I try myself using following code based on example 4 from linked article.
SELECT
#select('DIM_TIME_INV.DAY_DAY') >= sysdate -(#prompt('Invoiced, days before today:','N',[DIM_TIME_INV.DAY_DAY],mono,free))
FROM
DIM_TIME_INV
Testing the SQL gives following error:
ORA-00936
SAP kba 2054721
The whole idea is to have a flexible yet consistent dimension that will calculate every time the report is run, without losing the code whenever new items are added to the report.
Does anyone know of a way to use the #Prompt in SQL for SAP WEBI 4.2? - Or any other way to have 'flexible' time dimensions where it is possible to set a from-date or to-date independently or even a range, without having universe designer creating a s**t-load of filters and dump in various universes.
Thanks // C
With regard to your example code, I think you're on the right track but your syntax has some issues:
SELECT
#select('DIM_TIME_INV.DAY_DAY') >= sysdate -(#prompt('Invoiced, days before today:','N',[DIM_TIME_INV.DAY_DAY],mono,free))
FROM
DIM_TIME_INV
First, both #Select and #Prompt must refer to universe objects, not columns. The syntax for both is: class name\object name. Assuming that the DIM_TIME_INV.DAY_DAY is associated with a universe object named Day Day in a class named Dim Time, the above code should be:
SELECT
#select('Dim Time\Day Day') >= sysdate -(#prompt('Invoiced, days before today:','N','Dim Time\Day Day',mono,free))
FROM
DIM_TIME_INV
Also note that the object reference in the #prompt call is delimited by single quotes, not brackets.
Next, I'm assuming that DAY_DAY is a date field. Its reference in the #prompt call would cause the prompt to display a list of values, sourced from DAY_DAY. But you want a numeric value from the prompt, not a date, so I would just leave that out, which will let the users enter a numeric value:
SELECT
#select('Dim Time\Day Day') >= sysdate -(#prompt('Invoiced, days before today:','N',,mono,free))
FROM
DIM_TIME_INV
Next, even with this corrected syntax, there will be an issue using this code as you have it. A good way to debug #prompt issues is to view the SQL in the WebI report after you get the error -- the SQL will show the rendered result, with all functions (#select and #prompt) expanded. For the above, you might get SQL like:
SELECT
DIM_TIME_INV.DAY_DAY >= sysdate -(400)
FROM
DIM_TIME_INV
This, of course, is invalid - you can't have a condition in the SELECT clause. If this is truly intended to be a condition (which I think it is, based on your objective), then it should be a predefined condition rather than a dimension.
With that said, I think you're on the right track for what you want to do. With the above corrections, you would have a predefined condition that you could drop into reports, which would enable the users to select the starting period (by number of days ago) for the report. You could create additional prompts with different logic, ex:
#select('Dim Time\Day Day') >= sysdate -(#prompt('Invoiced, weeks before today:','N',,mono,free) * 7)
or
#select('Dim Time\Day Day')
BETWEEN sysdate - #prompt('Starting days ago:','N',,mono,free)
AND sysdate - #prompt('Ending days ago:','N',,mono,free)

SSRS 2008 R2 calulate new date from existing that's 1 year later and on Monday?

hoping someone can help. I have an textbox on a report that I need to calculate a new date value for from another date on the report.
I need to take an existing shipping date (say 5/5/2014) and calculate a new date which is one year later but on the Monday of that week (so would be 5/4/2015) -- we only ship product on Mondays.
I know I need to use DateAdd and probably DatePart but have been unable to find the proper formula.
can anyone help?
figured it out -- thanks for all the help. I took the idea that Gordon had, putting it in my select statement rather than trying to manipulate it in the expression area of the textbox on the report:`SELECT DATEADD(wk,DATEDIFF(wk,0,DATEADD(YY,1,o.REQ_DATE)),0) as NewDate'
works great! :)