What method can I use to get a different date value for a stored field in my database? - sql

I am working on a project that is asking me to return different date values based on the scenario and I am not quite sure how to write this out. I think that I'm just not understanding how to apply the logic:
PAYFREQ is A then TRANS_DATE is 1st of the Year of COMPEFFDT
PAYFREQ is M then TRANS_DATE is 1st of the Month of COMPEFFDT
PAYFREQ is B then 1st day of the biweekly period ending with COMPEFFDT
PAYFREQ <> A and the only non-zero FIC amount for a calendar year has COMPEFFDT of December 31, then TRANS DATE is 1st of the Year of COMPEFFDT
Can anyone give me at least a base starting point in how to formulate this statement?

Assuming this is in a query, try the SWITCH statement. I'm not sure what you mean by the "1st of year of COMPEFFDT" though.
SELECT SWITCH (
PAYFREQ = "A", "1st of year of COMPEFFDT",
PAYFREQ = "M", "1st of the Month of COMPEFFDT",
...
) AS Result
Also, you should look at the guide for submitted a new question.

Related

Previous month expression

I need help to get a dynamic expression for the previous month (December 2022) compared to the current year's month, January 2023.
Perhaps the attached might provide some more clarity.
Depends on your data format a bit but in general you should be able to use something like:
= sum( {< Month = {"$(=MonthStart(AddMonths(Today(), - 1)))"} >} Sales)
The set analysis will filter the Sales and will be sum only values for which Month is the month start of the previous month (based on Today() function result)

How do i write a sql query that is contingent on 2 columns in one conditional?

Take the following psuedo query on a table called userscore:
select average of userscore.points
where (userid = userid)
and ((userscore.year != given year) && (userscore.week != given week))
How do i write a query that will filter out a specific week/year column combination? For example this week is the 29th week of 2020. How do i write a query that will filter out only the 29th week of 2020 and not the 29th weeks of 2019/2018/2017 etc?
How do i write a query that will filter out only the 29th week of 2020 and not the 29th weeks of 2019/2018/2017 etc?
This is more of a question about boolean logic than SQL.
Assuming that given_year and given_week are the parameters to your query, you want something like:
where not (year = given_year and week = given_week)
You can also express this with or:
where year <> given_year or week <> given_week

How to get Previous date from given date ex. If today is 5-Feb-2018 then i wants 5-Jan-2018

For ex. in case of 5-Jan-2018 I wants to get 05-December-2017 like in case of February i need only month back remain year was same but in case of January I need month back and year back also
if date is 05-March-2018
i need 05-february-2018
But
If date is 05-January-2018
i need 05-December-2017
Try this:
select DATEADD(mm,-1,getdate())

Oracle Week Number from a Date

I am brand new to Oracle. I have figured out most of what I need but one field is driving me absolutely crazy. Seems like it should be simple but I think my brain is fried and I just can't get my head around it. I am trying to produce a Sales report. I am doing all kinds of crazy things based on the Invoice Date. The last thing I need to do is to be able to create a Week Number so I can report on weekly sales year vs year. For purposes of this report my fiscal year starts exactly on December 1 (regardless of day of week it falls on) every year. For example, Dec 1-7 will be week 1, etc. I can get the week number using various functions but all of them are based on either calendar year or ISO weeks. How can I easily generate a field that will give me the number of the week since December 1? Thanks so much for your help.
Forget about the default week number formats as that won't work for this specific requirement. I'd probably subtract the previous 1 December from invoice date and divide that by 7. Round down, add 1 and you should be fine.
select floor(
(
trunc(invoiceDate) -
case
-- if December is current month, than use 1st of this month
when to_char(invoiceDate, 'MM') = '12' then trunc(invoiceDate, 'MM')
-- else, use 1st December of previous year
else add_months(trunc(invoiceDate, 'YYYY'), -1)
end
) / 7
) + 1
from dual;

Get month name with different month start and end dates

Getting the month for the current date is, obviously, straight forward, but I'm needing to get the month name with a different end date.
I need to get the month name with the start date of the month being the first Thursday after the first Wednesday of the month and the end date of the month being the first Wednesday of the following month. It's for an accounting thing, so I'm not going to argue with the spec!
e.g. for 2014, January would run from 9th Jan - 5th Feb, February would run from 6th February - 5th March, March would run from 6th March - 2nd April.
I would suggest that you create a table with your 'accounting months' in it, having a start date, end date and month name columns.
You could then query this to find the row where your date is between the start and end dates and return the month name. Putting this into a scalar function would then allow it to be reusable and relatively easily updated for next years months as well.
I think, as per Paddy's answer, a lookup table is the simplest thing to do. Here's one way to generate the rows for it:
; With Numbers(n) as (
select 4 union all select 5
), Months as (
select CONVERT(date,'20010104') as StartDt,CONVERT(date,'20010207') as EndDt,
DATENAME(month,'20010103') as Month
union all
select DATEADD(week,n1.n,StartDt),DATEADD(week,n2.n,EndDt),
DATENAME(month,DATEADD(week,n1.n,StartDt))
from Months,Numbers n1,Numbers n2 --Old-skool join, just for once
where DATEPART(day,DATEADD(week,n1.n,StartDt)) between 2 and 8 and
DATEPART(day,DATEADD(week,n2.n,EndDt)) between 1 and 7 and
StartDt < '21000101'
)
select * from Months option (maxrecursion 0)
(CW since this is effectively just an extension to Paddy's answer but I don't want to edit their answer, nor is it suitable for a comment