T-SQL: August is the beginning of next years financial year, code to dynamically to change the batch ids - sql

I have a column called batch_id with a list of dates - 2016080184 ie date 2016 08 01 84(84, I believe a time part).
I need to update the batch_id (varchar(25)) to change to 2017010184, based on another column voucher_date (datetime) = 2016-08-01 00:00:00.000
So if the voucher date was 2016-08-02 00:00:00.000, then the batch_id needs to change from 2016080278 to 2017010278 (78 at the end here doesn't matter)
August is the first month for the financial year, so August would effectively become January, September would become February etc.. and the year from Aug needs to indicate the following year ie this year is 2016 therefore batch_id should start with 2017.
Next August batch_id should indicate 2018 etc..
The file I receive is always a day behind to make things more complicated.

I am a little confused about your requirement for the year to change, but this should give you all you need to get started:
declare #BatchID nvarchar(10) = '2016080184'
select convert(nvarchar(8),dateadd(month,-7,cast(left(#BatchID,8) as date)),112) + right(#BatchID,2) as NewBatchID_SameYear
,convert(nvarchar(8),dateadd(month,5,cast(left(#BatchID,8) as date)),112) + right(#BatchID,2) as NewBatchID_NextYear
You can apply the above date changes with a CASE statement on the voucher_date column as required.

Related

After certain week of 2022 and continue with this new year

I would like to request some advice about how to set a Where Condition, but after a certain week
What I mean is:
I have dirty data before a specific week of 2022, so I made this:
DATEPART(WK, SA.FECHAE) >= 44
AND
YEAR(SA.FECHAE) >= 2022
But, We're on 2023, so, I need to add the new information of this new year year too into the query.
The query result shows me until 12-31-2022 and need it until today after the week 44 of 2022
...
WHERE (
DATEPART(WEEK, SA.FECHAE) >= 44
AND YEAR(SA.FECHAE) = 2022
)
OR (
YEAR(SA.FECHAE) >= 2023
)
In the OPs question they ask how to add an additional date range to their WHERE clause. The addition of this OR allows a second date range (in this case anything where the year is greater than or equal to 2023) to match the predicate and be returned, without impacting the original.
Plain English definition of the amended where clause:
Week 44 of 2022, or any week of any year from 2023 forward.

Daily Partitioning and archiving

I'm trying to partition a table by month say Jan, Feb, Mar. The column through which I'll partition is a datetime type column with an ISO Format ('20190101', '20190201', etc).
For example, I have sales data for jan, feb, mar. I'd like the data to be partitioned by daily partitioned . ('20190101', '20190201','20190301' etc)
E.X:
Jan, Feb, Mar etc. Also I would like to keep the data less so I would like to delete daily day wise data keeping only 1 month data maximum, for example I will create jan 31 , feb 28 , mar 31, apr 30. How do I manage partition dynamically as some month is 31 days some are 28 days and 30 days. Also I need to retain only one month data for example if its, 1st of sep then I need to keep aug 31 days data, and can delete 31st jul day data now on 2nd sep I can delete 1st august data so I need to delete daily data and keep only 30 days data .
My question is: is it even possible? If it is, how an I automate the process using SSIS?
You may try this. As you want to remove previous 30 days data from current date or any specific date, so you can easily calculate the date range by subtracting 30 days from the current date.
For 30 days
--- Instead of getdate() you may use any of your date column for filter
delete from yourtable where yourdate < DATEADD( day, -30, getdate()) ---- here on place of 30 you may use any days you want to delete
For 1 month
--- Instead of getdate() you may use any of your date column for filter
delete from yourtable where yourdate < DATEADD( month, -1, getdate()) ---- here on place of 30 you may use any days you want to delete
Instead of having your partition boundaries be the end of the month, have them be the beginning. That is, do something like:
[2019-01-01, 2019-02-01),
[2019-02-01, 2019-03-01),
[2019-03-01, 2019-04-01),
[2019-04-01, 2019-05-01),
[2019-05-01, 2019-06-01),
etc
That is, the left-hand boundary is in the partition and the right-hand boundary isn't. If you're using actual Partitioning, you'd define your partition function as a "boundary left" function. See the documentation for more details.

SQL - check if an order date occurs after the second Saturday in July

I am querying against a table of 4 yrs of order transactions (pk = order number) and I'm looking to tag each record with particular date flags based on the order date - e.g., calendar year, calendar month, fiscal year, etc. There are date attributes that are specific to our business (e.g., not easily solved by a datepart function) that I'm having trouble with.
I was able to add "School Year" (for us that runs Aug 1 - July 31) using a case statement:
case
when datepart(month, oline.order_date_ready) between 8 and 12 then datepart(year, oline.order_date_ready)
else (datepart(year, oline.order_date_ready)-1)
end as school_yr
So for 1/19/2017, the above would return "2016", because to us the 2016 school year runs from Aug 1 2016 to July 31 2017.
But now I'm having trouble repeating the same kind of case statement for something called "Rollover Year". All of our order history tables are reset/"rolled over" on the 2nd Saturday in July every calendar year, so for example the most recent rollover date was Saturday July 9th 2016. Click to view - rollover year date ranges
My above case statement doesn't apply anymore because I can't just add "datepart(month, oline.order_date_ready) = 7" - I don't need the whole month of July, I just need all the orders occurring after the 2nd Saturday in that July. So in this example, I need everything occurring from Sat July 9 2016 to today to be flagged as rollover_date = 2016.
Is there a flexible way to do this without hard coding previous/future rollover dates into another table? That's the only way I can think to solve it currently, but I'm sure there must be a better way.
Thanks!
If you ask for the day-of-the-week of July 1st, then from there it's simple arithmetic, right? This query gives results matching your image:
SELECT y,
CONCAT(y, '-07-01')::timestamp +
CONCAT(6 - EXTRACT(DOW FROM CONCAT(y, '-07-01')::timestamp) + 7, ' days')::interval
FROM generate_series(2013, 2020) s(y)
ORDER BY y DESC
;
So given any date d from year y, if it comes before the 2nd Saturday of July, give it fiscal year y - 1. Otherwise give it fiscal year (school year?) y.

Convert date to first of current month if date is previous to current month

I have a query with an IIF() expression in one column that I am using to identify if a date is previous to the current month to then amend it if so. So if I run the query on 19th March 2014 and the EffFrom date is before 1st March 2014, I would want that column entry to now appear as 1st March 2014.
I am using the below expression which is pretty much doing what I want, however I know it is not considering the year -- i.e. it is changing an entry of 1st Jan 2015 to be 1st March 2014.
EffFrom:
IIf(Month([Table.Efffrom])"Less than symbol"Month(Date()),Date()-Day(Date())+1,[Table.Efffrom])
Can someone correct the expression for me?
I interpreted "if I run the query on 19th March 2014 and the efffrom date is before 1st March 2014 I would want that column entry to now appear as 1st March 2014" to mean you want something like this from a query run today (Mar 19th 2014):
id Efffrom adjusted_date
1 1/1/2014 3/1/2014
2 3/1/2014 3/1/2014
3 3/31/2014 3/31/2014
4 1/1/2015 1/1/2015
If that is correct, your IIf expression can use DateSerial to check whether Efffrom is before the first of this month, and transform the older dates.
SELECT
y.id,
y.Efffrom,
IIf
(
y.Efffrom < DateSerial(Year(Date()), Month(Date()), 1),
DateSerial(Year(Date()), Month(Date()), 1),
y.Efffrom
) AS adjusted_date
FROM YourTable AS y;
Here's what i do to get the first day of the next month
EffFrom: DateAdd("m",1,CDate(Format([Table.Efffrom],"m/\1/yy")))

MS Access grouping query spanning start and end dates

I would like to get a running tally of how many widgets were/are rented at any one time, by month, by year. Data is held in an MS Access 2003 db;
Table name: rent_table
Fields:
rentid
startdate
enddate
rentfee
rentcost
bookingfee
Something like; Count number of rentid's that fall between month/year, then group them?
e.g. if a widget was rented from 5th Jan 2014 to 8th April 2014 it would appear as a count in Jan, Feb, Mar and April tally's.
Many thanks.
EDIT
More details (sorry);
Access db is fronted by classic ASP.
If possible I don't want to create any new tables.
No input is required in order to run the report.
There are around 350-400 widgets that could be rented at any one time.
Each widget is rented exclusively.
Report output example;
Month | Year | NumRented
Jan 2014 86
Feb 2014 113
...
Can a query pick up dates within dates? So literally do a count of the table where date >Dec 31st 2013 AND <1st Feb 2014 (to grab a count for all of January 2014) and would that include the example of the rent starting on the 5th Jan? So I could just do twelve counts for each year?
create a calendar table, e.g.
table = cal_yyyymm with one column dt_yyyymm as numeric field
populate the table with ... say 5 or 10 years of data
201401 201402 201403 ... 60 or 120 rows, a small table
make a sql
Select
dt_yyyymm,
count(*) as cnt
From cal_yyyymm
Left Join rent_table
On format(startdate,"yyyymm") >= dt_yyyymm
And dt_yyyymm >= format(enddate,"yyyymm")
think about the complications in the data -- --
widget was rented from 5th Jan 2014 to 8th Jan 2014
and again rented from 11th Jan 2014 to 21st Jan 2014
does this count at 1 or 2 in the month?
if it is 1, then the sql gets more complicated because
the rent_table first needs to have its dates converted
to yyyymm format, and second needs to be de-duped on rentid,
and third then joined to cal_ On the dates...