Solved

SQL Date Function

Posted on 2013-01-30
11
342 Views
Last Modified: 2013-02-01
Hi, I need help with a udf function in MSSQL, please.  We have a program that we put a due date in a particular field and we have child events that generate from that date and are uploaded to Outlook through Exchange.  What we are trying to do is make those child events never pop on a weekend.  If the date falls on a Saturday it needs to push back to Thursday and the Sunday date would push back to Friday.  We had someone write a script for us but we can't make it work.  I am pasting it in here in hopes that someone may be able to help.

Thanks,
Tony.

--------------------------------

USE [Lawbase]
GO

/****** Object:  UserDefinedFunction [dbo].[udf_WKDELIM]    Script Date: 01/21/2013 12:15:46 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE FUNCTION [dbo].[udf_WKDELIM] (@dtDate DATETIME, @strPrev VARCHAR(10))
RETURNS DATETIME
AS
BEGIN
DECLARE @intDay INT
DECLARE @rtResult DATETIME
SET @intDay = DATEPART(weekday,@dtDate)
--To find Previous working day
Set @strPrev = 'Previous'
IF @intDay = 1
SET @rtResult = DATEADD(d,-2,@dtDate)
ELSE
IF @intDay = 2
SET @rtResult = DATEADD(d,-3,@dtDate)

--Default case returns date passed to function
ELSE
SET @rtResult = @dtDate
RETURN @rtResult
END

GO
0
Comment
Question by:canteyhanger
  • 4
  • 3
  • 2
  • +1
11 Comments
 
LVL 11

Expert Comment

by:Simone B
ID: 38837183
Try this:

CREATE FUNCTION [dbo].[udf_WKDELIM2] (@dtDate DATETIME)
RETURNS DATETIME
AS
BEGIN

DECLARE @rtResult DATETIME

IF DATENAME(dw,@dtDate) = 'Saturday' OR DATENAME(dw,@dtDate) = 'Sunday'

SET @rtResult = DATEADD(d,-2,@dtDate)
ELSE

SET @rtResult = @dtDate

RETURN @rtResult
END
0
 

Author Comment

by:canteyhanger
ID: 38837271
Thanks, I will try it and let you know how it works out.
0
 

Expert Comment

by:owenja
ID: 38837334
This does seem to be working in that is does return the Friday date if that is passed to the script - I have stripped out the second variable as that seems to be the issue - the solution above will not work as Saturday will return Thursday as the last working day rather than Friday - the following code highlights that the script works - you just need to use the returned date without the variable as I don't know what it fulfils - following code will demonstrate
CREATE FUNCTION [dbo].[udf_WKDELIM](@dtDate DATETIME)
RETURNS DATETIME
AS
BEGIN
DECLARE @intDay INT
DECLARE @rtResult DATETIME
SET @intDay = DATEPART(weekday,@dtDate)
--To find Previous working day

IF @intDay = 1
SET @rtResult = DATEADD(d,-2,@dtDate)
ELSE
IF @intDay = 2
SET @rtResult = DATEADD(d,-3,@dtDate)

--Default case returns date passed to function
ELSE
SET @rtResult = @dtDate
RETURN @rtResult
END

GO 

SELECT [dbo].[udf_WKDELIM](GETDATE()-1) AS DayOfWeek

Open in new window


You will then get Friday for sat sunday or the current date if it is not weekend

Just set the getdate - number to show a weekend day such as -3 today will give you Saturday and you can see the effect.
0
 
LVL 51

Accepted Solution

by:
Mark Wills earned 500 total points
ID: 38839201
Mine is pretty much as per buttercup1...

Dunno what the setting of @prev does, so ignored it.

Always need to be careful with DATEPART and days of the week.here is a parameter that is used to help with days of week. Have a read of : http://msdn.microsoft.com/en-us/library/ms181598.aspx

So, if possible, use DATENAME if you are unsure of the DATEPART, or, explicitly set DATEFIRST in accordance with the above link (which you cannot do in a function, but can in a procedure).


ALTER FUNCTION [dbo].[udf_WKDELIM] (@dtDate DATETIME)
RETURNS DATETIME
AS
BEGIN
   IF DATENAME(weekday, @dtDate) in ('Sunday','Saturday') return dateadd(day,-2,@dtdate)
   RETURN @dtdate
END

Open in new window

0
 

Expert Comment

by:owenja
ID: 38839223
Hi mark_wills,
Just a quick question about the return dateadd(day,-2,@dtdate) clause - if it's Saturday and you apply a -2 wouldn't that give you Thursday rather than Friday?  Should there not be a case of -1 for Saturday and -2 for Sunday?

Regards
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Expert Comment

by:owenja
ID: 38839239
Sorry mark_wills just reread the requirement - thought a weekend was to be read as a Friday hence the case statement - I think the previous working day fro the original scriopt send me down the wrong road.
REgards
0
 
LVL 51

Expert Comment

by:Mark Wills
ID: 38839724
No worries :)
0
 

Author Comment

by:canteyhanger
ID: 38839869
First of all, thanks to everyone for the help.  We are SQL neophites and can get into trouble trying to do this on our own!

Buttercup,
We tried yours and still got no dates to move back.  Not sure what happened there.  

Owenja,
Yours worked for Sunday but Saturday stayed on Saturday.  

Mark_Wills,
Yours we have not tried yet but looks a lot simpler than the others.  I can even understand it!  I will let you know how it comes out as we try this solution.

You guys are great, all of you!
0
 
LVL 11

Expert Comment

by:Simone B
ID: 38839916
How are you testing? These are the results I got:

SELECT DATENAME(dw,dbo.udf_WKDELIM2('jan 28, 2013')) AS MondayTest,
DATENAME(dw,dbo.udf_WKDELIM2('jan 29, 2013')) AS TuesdayTest,
DATENAME(dw,dbo.udf_WKDELIM2('jan 30, 2013')) AS WednesdayTest,
DATENAME(dw,dbo.udf_WKDELIM2('jan 31, 2013')) AS ThursdayTest,
DATENAME(dw,dbo.udf_WKDELIM2('feb 1, 2013')) AS FridayTest,
DATENAME(dw,dbo.udf_WKDELIM2('feb 2, 2013')) AS SaturdayTest,
DATENAME(dw,dbo.udf_WKDELIM2('feb 3, 2013')) AS SundayTest

Open in new window


Image
0
 

Author Comment

by:canteyhanger
ID: 38839937
We are just adding the udf in a var. for a test case we set up.  We created the case and created a workflow within and called the udf from in there.  I guess we weren't smart enough to try it inside SQL!  That would have been the easy way.  I will get with my other compadre today and try it all again.

Thanks.
0
 

Author Closing Comment

by:canteyhanger
ID: 38844184
To all, thank you for helping us with this.  We got it to work as expected.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now