SQL SERVER - Function Adding hours to a datetime field with the result within working hours
$10-30 USD
Anuluar
Postuar over 2 years ago
$10-30 USD
Paguhet në dorëzim
I have the function below and it works fine, but now I need to improve it :
1-I need to append the lunch time. ( in lunch time the company don't work)
@StartOfLunch = 12.5,
@EndOfLunch = 13.5,
2- i have a table of DONT WORK (holidays / vacations / ...) ( holiday/vacations the company don't work)
3- i have a table for EXTRA WORK ( in some cases the company make extra-hours and work in days that normally do not work)
Example of table for don't-work and extra-work
name
data_start
data_end
hour_start
hour_end
extra ( 1 - is for work / 0 - for don't work)
....
---------------------------------------------------------------------------
ALTER FUNCTION [dbo].[addhoras] (@Date DATETIME, @DateAdd DATETIME)
RETURNS DATETIME
AS
--Select [login to view URL] ('2018-01-23 10:00:00.000' , '1900-01-01 02:00:00.000')
--DECLARE @Date DATETIME = '2018-01-23 10:00:00.000';
--DECLARE @DateAdd DATETIME = '1900-01-01 02:00:00.000';
BEGIN
DECLARE @StartOfDay FLOAT = 9.5 ;
DECLARE @EndOfDay FLOAT = 17.5 ;
DECLARE @Datefinal DATETIME
--fix up start date
--before start of day, move to start of day
IF ((CAST(@Date - DATEADD(dd,0, DATEDIFF(dd,0,@Date)) AS FLOAT) * 24) < @StartOfDay)
BEGIN
SET @Date = DATEADD(mi, @StartOfDay * 60, DATEDIFF(dd,0,@Date))
END
--after close of day, move to start of next day
IF ((CAST(@Date - DATEADD(dd,0, DATEDIFF(dd,0,@Date)) AS FLOAT) * 24) > @EndOfDay)
BEGIN
SET @Date = DATEADD(mi, @StartOfDay * 60, DATEDIFF(dd,0,@Date)) + 1
END
--move to monday if on weekend
WHILE DATENAME(dw, @Date) IN ('Saturday','Sunday')
BEGIN
SET @Date = @Date + 1
END
--get the number of hours to add and the total hours per day
DECLARE @HoursPerDay FLOAT
DECLARE @HoursAdd FLOAT
SET @HoursAdd = DATEDIFF(hh, '1900-01-01 00:00:00.000', @DateAdd)
SET @HoursPerDay = @EndOfDay - @StartOfDay
--date the time of geiven day
DECLARE @CurrentHours FLOAT
SET @CurrentHours = CAST(@Date - DATEADD(dd,0, DATEDIFF(dd,0,@Date)) AS FLOAT) * 24
--if we stay in the same day, all is fine
IF (@CurrentHours + @HoursAdd <= @EndOfDay)
BEGIN
SET @Date = @Date + @DateAdd
END
ELSE
BEGIN
--remove part of day
SET @HoursAdd = @HoursAdd - (@EndOfDay - @CurrentHours)
--,ove to next day
SET @Date = DATEADD(dd,0, DATEDIFF(dd,0,@Date)) + 1
--loop day
WHILE @HoursAdd > 0
BEGIN
--add day but keep hours to add same
IF (DATENAME(dw,@Date) IN ('Saturday','Sunday'))
BEGIN
SET @Date = @Date + 1
END
ELSE
BEGIN
--add a day, and reduce hours to add
IF (@HoursAdd > @HoursPerDay)
BEGIN
SET @Date = @Date + 1
SET @HoursAdd = @HoursAdd - @HoursPerDay
END
ELSE
BEGIN
--add the remainder of the day
SET @Date = DATEADD(mi, (@HoursAdd + @StartOfDay) * 60, DATEDIFF(dd,0,@Date))
SET @HoursAdd = 0
END
END
END
END
SET @Datefinal = (SELECT @Date)
Return @Datefinal
END
I can help you with sql server functions , please ping/connect me now I am available.
Please check my reviews and project which I have completed.
I can create sql queries for sql server database as per design, let me know when to proceed. I can work on future works for databases also if needed.
I will work for cheapest budget within time, budget can be re negotiated again, send the details for the project. Please contact me asap.
I have 12+ years of experience in database programming, website designing, powershell scripting and android development.
Feel free to message me anytime if you need to discuss this further.
$10 USD në 1 ditë
5,0 (4 përshtypje)
4,4
4,4
20 profesionistët e pavarur ofrojnë mesatarisht $26 USD oferta për këtë punë
Hello, I can develop the routine that you require to calculate the difference of days between a range of dates using sql server
Please contact me by chat
Thanks
Andres
Hello there, i've done this similar function like this before. Within weekends is okay but the holiday in a year, each country will be different so you should you a have table that contains of holiday to exclude. Waiting for your response if interested in discussing more detail. Waiting for your response if interested. Thank you!!!
Hello, I'm an IT expert with more than 15 years of experience in the IT industry . I'm Cisco Certified networking professional 300-100 and 300-115 and Linux Certified Professional lpi 101, 102 and red hat certified system administrator and VMware Certified Professional 4, 5,5.5 and Data Center and Microsoft System administrateur /Engineer ranging from 2003 to 2012 .
1. 10 Years of experience
2. Expert in ASP.net,C#.net,VB.net,Sql server,MySql,MS Access,Crystal Report
3. Worked in almost 150 projects.
4. Dedication,timely execution and perfection is our key Skill.
Hi I have 15 years of experience in SQL server Database development MySQL development postgress SQL server Database development. I will be able to help you with the project, please ping me offline to discuss further
Hi, hope you will fine. thanks for posting project. I have saw your project. I have extensive experienced to programming on Oracle Database SQL, PLSQL, Forms & Reports, Oracle Apex, MySQL, SQL Server. I will work on your project with great effort. I wish to work for you. please contact me for this project.
In shaa ALLAH I will satisfied you as your requirement.
Regards,
Muhammad Shoaib
I am working as Database Administrator Team Lead for the past 4 years in the Reputed Company ,I can help in Creating the New Function to fulfil your requirement. I think I am best for this Job. Contact me for more details
Hi,
My Name is Nittin. I have 14 years of experience as a Full stack developer.
I worked with big companies(CMM Level 5) and received appreciation from there.
I worked on multiple technologies Like: Asp.net, MVC, Sql Server, Angular , .Net Core.
Please let me know if we can discuss more on this.
Regards
Nittin
create a table where relative holidays are recorded for example July 2 in a range of validity per year (from the year 2021 - 2599) since there are some holidays decreed by the government on some occasions. And make query suing this table.
Need to know weather holiday table is present. Will find difference between a particular period omitting holidays and weekends.
Can create store procedure by getting start date and end date as variables.
1. calculate numer of days between start and end date without considering holiday.
2. Calculate noumber of holidays between given days , leap year is taken into consideration automatically.
3. Exlude holiday count to already calculated total days count.
4 Error can also be displayed when start dae is higher than the end date.
Hi, i can do this function with a table that contains holiday and weekend.
I have this function actually working in anothers projects. It is very fast and performant.
I can geet to work soon as posible
Hi,
I have more than 8 years of experience in implementing the Data Warehouse applications.
I have rich experience in writing complex SQL queries for any business requirement.
I can the start the work immediately.
A simple scalar function with a lookup table for holidays should do the job.
This task would take less 3 hours.
About me:
I have more than 10 years of experience in design and development in SQL Server stack.
I can handle SQL admin and maintenance tasks as well.