Changing the year of a date in SQL - sql

I am relatively new to programming. I have a table and in the table I have a column which shows when an employee started at the company. I am creating a procedure and within that procedure, I want to take the users start date and replace it with the current year, this will then be followed by an if statement. If the date that has just been calculated is before today, then change the year of the date to next year. Thanks and sorry if the question isn't fully clear or has been asked before.

I don't think this is the most elegant solution, but you could take the date apart to replace it with the current year. Then use a case statement to change it to the next year if it has already passed. Here's the code:
SELECT employee_name,
CASE
WHEN CONVERT(VARCHAR,MONTH(start_date))+'/'+CONVERT(VARCHAR,DAY(start_date))+'/'+CONVERT(VARCHAR,YEAR(getdate())) < GETDATE() THEN CONVERT(VARCHAR,MONTH(start_date))+'/'+CONVERT(VARCHAR,DAY(start_date))+'/'+CONVERT(VARCHAR,YEAR(getdate())+1)
WHEN CONVERT(VARCHAR,MONTH(start_date))+'/'+CONVERT(VARCHAR,DAY(start_date))+'/'+CONVERT(VARCHAR,YEAR(getdate())) >= GETDATE() THEN CONVERT(VARCHAR,MONTH(start_date))+'/'+CONVERT(VARCHAR,DAY(start_date))+'/'+CONVERT(VARCHAR,YEAR(getdate()))
END AS anniversary_date
FROM Employees

Related

SQL Server : get data where date + last 2 financial years

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

how to automate the date change in a query using transact sql

I work for a company where everyday I modify a query by changing the date of the day before, because the report is always from the previous day.
I want to automate the date change. I have made a table with two columns, one with all dates from this year and another with bits where if 0 is a working day and 1 if is a holiday.
I have successfully automated a little bit by telling if the day before is a working day then subtract 1 from the date (This is what happens everyday). But the problem is, that if is Monday appears as Friday, because Saturday and Sunday are not billable. And let's also say, that if today is Thursday and Wednesday and Tuesday we're holidays, then the report will run on Monday. I will leave you a picture, that shows how the table is made with dates.
Remembering, that if there is no holidays in the middle of the week, always will be subtract one.
The way to do this sort of thing is close to what you have done, but just extend it further. Create a BusinessDate table that has every date, and then every rule you have implemented inside it. You can go so far as to include a column such as ReportDate which will return,for every date, what date the report should be run for.
Do this once, and it will work forever more. You may have to update for future holidays once a year, but better than once a day!
It will also allow you to update things specific for your business, like quarter dates, company holidays, etc.
If you want to know more on the subject, look up topics around creating a date dimension in a data warehouse. Its the same general issue you are facing.
Too complicated for a comment and it involves a lot of guessing.
So everyday, your process starts by first determining if "today" is a work day. So you would do something like:
if exists (select * from <calendar> where date = cast (getdate() as date) and IsWorkday = 1")
begin
<do stuff>
end;
The "do stuff" section would then run a report or your query (or something that isn't very clear) using the most recent work day prior to the current date. You find that date using something like:
declare #targetdate date;
set #targetdate = (select max(date) from <calendar>
where date < cast (getdate() as date)
and IsWorkday = 1);
if #targetdate is not null
<run your query using #targetdate>
That can be condensed into less code but it is easier to understand when the logic is written step-by-step.

Oracle SQL: Dynamic timeframe calculation

Greetings all knowing Stack.
I am in a bit of a pickle, and I am hoping for some friendly assistance form the hive mind.
I need to write a query that returns the difference in days between a registration date (stored in a table column) and the first day of the last September.
For example; assuming the query was being run today (24-10-2016) for a record with a registration date of 14-07-2010, I would want the script to return the difference in days between 14-07-2010 and 01-09-2016
However had I run the same query before the end of last August, for example on 12-08-2016, I would want the script to return the difference in days between 14-07-2010 and 01-09-2015.
I'm fine with the process of calculating differences between dates, it's just the process of getting the query to return the 'first day of the last September' into the calculation that is tripping me up!
Any input provided would be much appreciated.
Thankyou =)
Try this approach:
add four months to the current date
truncate this date to the first of year
subtract four months again
Add_Months(Trunc(Add_Months(SYSDATE, 4), 'year'), -4)
Hope this might help.
WITH T AS (SELECT TO_DATE('14-07-2010','DD-MM-YYYY') REG_DATE,
SYSDATE EXEC_DATE
FROM DUAL)
SELECT CASE WHEN TO_CHAR(EXEC_DATE,'MM') >= 9
THEN ADD_MONTHS(TRUNC(EXEC_DATE,'YEAR'),8)
ELSE ADD_MONTHS(TRUNC(ADD_MONTHS(EXEC_DATE,-12),'YEAR'),8)
END
- REG_DATE AS DIFF
FROM T;

Get the month and year now then count the number of rows that are older then 12 months in SQL/Classic ASP

I know this one is pretty easy but I've always had a nightmare when it comes to comparing dates in SQL please can someone help me out with this, thanks.
I need to get the month and year of now then compare it to a date stored in a DB.
Time Format in the DB:
2015-08-17 11:10:14.000
I need to compare the month and year with now and if its > 12 months old I will increment a count. I just need the number of rows where this argument is true.
I assume you have a datetime field.
You can use the DATEDIFF function, which takes the kind of "crossed boundaries", the start date and the end date.
Your boundary is the month because you are only interested in year and month, not days, so you can use the month macro.
Your start time is the value stored in the table's row.
Your end time is now. You can get system time selecting SYSDATETIME function.
So, assuming your table is called mtable and the datetime object is stored in its date field, you simply have to query:
SELECT COUNT(*) FROM mtable where DATEDIFF(month, mtable.date, (SELECT SYSDATETIME())) > 12

T-SQL calculate with calendar week

I have a T-SQL question - I'm using SQL-Server 2012:
I have a varchar calendar week column (1-52). In my where clause I only want to select the last 6 weeks going back from the current calendar week of the current year. I also have a year varchar column with values of "2011" , "2012" and "2013".
But now I'm struggling to set the expression for the where clause.
Or do I need a Having clause to get this done?
I've tried it like this:
Having Calendarweek BETWEEN max(Calendarweek)-6 AND max(Calendarweek) AND Year=2013
but it returns all weeks.
Hope you can help me out with this.
thank you.
Try this
where Year ='2013'
and CAST(Calendarweek AS INT) >= datepart(week,getdate())-6