Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1020947
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:14:11+00:00 2026-05-16T11:14:11+00:00

I am trying to create a UDF that does 2 different things depending on

  • 0

I am trying to create a UDF that does 2 different things depending on the time. Below is my code. I am wondering if you can use the IF statement in a UDF because I am etting 4 errors, incorrect syntax near Begin and Returns and also a Return Statement with return value cannot be used in this context….Any suggestions?

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [dbo].[udf_TEST]
(
    @StartDate DATETIME,
    @EndDate DATETIME
)

--DECLARE @StartDate DATETIME
--DECLARE @EndDate DATETIME
--SET @StartDate = '2010-07-06 14:46:37.577' 
--SET @EndDate = '2010-07-09 09:04:31.290'
BEGIN
RETURNS VARCHAR(MAX)

(
IF (CONVERT(VARCHAR(13), @StartDate, 114) > CONVERT(VARCHAR(13), @EndDate, 114))
BEGIN
DECLARE @NonWorkTime1 INT
SET @NonWorkTime1 = 780
--How many minutes are between order start and end time including non working time
DECLARE @AllMins1 INT 
--Declares how many minutes are in a day and makes it float to get remainder minutes when divided
DECLARE @MinsInDay1 DECIMAL
SET @MinsInDay1 = 1440.0
--Finds how many minutes are between start and end time excluding weekends and assignes to variable 
SET @AllMins1 = ((DATEDIFF(mi, @StartDate, @EndDate)) 
  -(((DATEDIFF(wk, @StartDate, @EndDate) * 2) * 24) * 60)  
  -(((CASE WHEN DATENAME(dw, @StartDate) = 'Sunday' THEN 1 ELSE 0 END) * 24) * 60)
  -(((CASE WHEN DATENAME(dw, @EndDate) = 'Saturday' THEN 1 ELSE 0 END) * 24) * 60)) 
--Calculates how many days have elapsed in the minutes that the order has taken
DECLARE @MinDays1 INT
SET @MinDays1 = (@AllMins1/@MinsInDay1)
--Subtracts complete day non worked minutes from final minutes between orders
DECLARE @FinalMinutes1 AS DECIMAL
SET @FinalMinutes1 = (@AllMins1 - (@MinDays1 * @NonWorkTime1) - 360 - 420)
RETURN @FinalMinutes1
END
ELSE
BEGIN
--RETURNS VARCHAR(MAX)
--How many minutes a day are not worked for trips
DECLARE @NonWorkTime INT
SET @NonWorkTime = 780
--How many minutes are between order start and end time including non working time
DECLARE @AllMins INT 
--Declares how many minutes are in a day and makes it float to get remainder minutes when divided
DECLARE @MinsInDay DECIMAL
SET @MinsInDay = 1440.0
--Finds how many minutes are between start and end time excluding weekends and assignes to variable 
SET @AllMins = ((DATEDIFF(mi, @StartDate, @EndDate)) 
  -(((DATEDIFF(wk, @StartDate, @EndDate) * 2) * 24) * 60)  
  -(((CASE WHEN DATENAME(dw, @StartDate) = 'Sunday' THEN 1 ELSE 0 END) * 24) * 60)
  -(((CASE WHEN DATENAME(dw, @EndDate) = 'Saturday' THEN 1 ELSE 0 END) * 24) * 60)) 
--Calculates how many days have elapsed in the minutes that the order has taken
DECLARE @MinDays INT
SET @MinDays = (@AllMins/@MinsInDay)
--Subtracts complete day non worked minutes from final minutes between orders
DECLARE @FinalMinutes AS DECIMAL
SET @FinalMinutes = (@AllMins - (@MinDays * @NonWorkTime))
RETURN @FinalMinutes
END
)
END
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T11:14:12+00:00Added an answer on May 16, 2026 at 11:14 am

    You definitely can use IF’s in a UDF.
    There are a few syntax errors, but main issue is that you need to move the @FinalMinutes to outside the IF as it needs to be returned from the main scope.

    Try this:

    CREATE FUNCTION [dbo].[udf_TEST] 
    ( 
        @StartDate DATETIME, 
        @EndDate DATETIME 
    ) 
    RETURNS VARCHAR(MAX) 
    
    --DECLARE @StartDate DATETIME 
    --DECLARE @EndDate DATETIME 
    --SET @StartDate = '2010-07-06 14:46:37.577'  
    --SET @EndDate = '2010-07-09 09:04:31.290' 
    BEGIN 
    DECLARE @FinalMinutes AS DECIMAL 
    IF (CONVERT(VARCHAR(13), @StartDate, 114) > CONVERT(VARCHAR(13), @EndDate, 114)) 
    BEGIN 
    DECLARE @NonWorkTime1 INT 
    SET @NonWorkTime1 = 780 
    --How many minutes are between order start and end time including non working time 
    DECLARE @AllMins1 INT  
    --Declares how many minutes are in a day and makes it float to get remainder minutes when divided 
    DECLARE @MinsInDay1 DECIMAL 
    SET @MinsInDay1 = 1440.0 
    --Finds how many minutes are between start and end time excluding weekends and assignes to variable  
    SET @AllMins1 = ((DATEDIFF(mi, @StartDate, @EndDate))  
      -(((DATEDIFF(wk, @StartDate, @EndDate) * 2) * 24) * 60)   
      -(((CASE WHEN DATENAME(dw, @StartDate) = 'Sunday' THEN 1 ELSE 0 END) * 24) * 60) 
      -(((CASE WHEN DATENAME(dw, @EndDate) = 'Saturday' THEN 1 ELSE 0 END) * 24) * 60))  
    --Calculates how many days have elapsed in the minutes that the order has taken 
    DECLARE @MinDays1 INT 
    SET @MinDays1 = (@AllMins1/@MinsInDay1) 
    --Subtracts complete day non worked minutes from final minutes between orders 
    SET @FinalMinutes = (@AllMins1 - (@MinDays1 * @NonWorkTime1) - 360 - 420) 
    END 
    ELSE 
    BEGIN 
    --RETURNS VARCHAR(MAX) 
    --How many minutes a day are not worked for trips 
    DECLARE @NonWorkTime INT 
    SET @NonWorkTime = 780 
    --How many minutes are between order start and end time including non working time 
    DECLARE @AllMins INT  
    --Declares how many minutes are in a day and makes it float to get remainder minutes when divided 
    DECLARE @MinsInDay DECIMAL 
    SET @MinsInDay = 1440.0 
    --Finds how many minutes are between start and end time excluding weekends and assignes to variable  
    SET @AllMins = ((DATEDIFF(mi, @StartDate, @EndDate))  
      -(((DATEDIFF(wk, @StartDate, @EndDate) * 2) * 24) * 60)   
      -(((CASE WHEN DATENAME(dw, @StartDate) = 'Sunday' THEN 1 ELSE 0 END) * 24) * 60) 
      -(((CASE WHEN DATENAME(dw, @EndDate) = 'Saturday' THEN 1 ELSE 0 END) * 24) * 60))  
    --Calculates how many days have elapsed in the minutes that the order has taken 
    DECLARE @MinDays INT 
    SET @MinDays = (@AllMins/@MinsInDay) 
    --Subtracts complete day non worked minutes from final minutes between orders 
    SET @FinalMinutes = (@AllMins - (@MinDays * @NonWorkTime)) 
    END
    RETURN @FinalMinutes
    END 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 522k
  • Answers 522k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Yes, the default delimiters are whitespace characters, but you can… May 16, 2026 at 9:16 pm
  • Editorial Team
    Editorial Team added an answer I managed to find out the correct term for this… May 16, 2026 at 9:16 pm
  • Editorial Team
    Editorial Team added an answer Do all Ruby interpreters follow the same Ruby syntax defined… May 16, 2026 at 9:15 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Trying to create an app that does some socket communication (Writing only). I can
I'm trying to write a UDF to translate a string that is either a
I'm trying to create a user defined function in Oracle that will return a
Trying to create a grails ant task that has other environments besides prod for
I´m trying to create a VIEW in SQL Server 2005. The SQL code is
Im trying to create LinqDataSource that will represent data from dynamically created String list
I would like to create a SP or UDF where I supply a table
I'm trying to optimize a stored procedure I'm maintaining, and am wondering if anyone
I'm trying to generate some sql that is used to calculate some final scores
We have a third-party DLL that can operate on a DataTable of source information

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.