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 8653019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T14:26:31+00:00 2026-06-12T14:26:31+00:00

Non-database programmer here. It happens so, that I need to create a function in

  • 0

Non-database programmer here. It happens so, that I need to create a function in T-SQL which returns workdays count between given dates. I believe that the easiest how it’s done is with while loop. Problem is, that as soon as I write something like

while @date < @endDate
begin

end

the statement won’t execute, claiming “incorrect syntax near the keyword ‘return'” (not very helpful). Where’s the problem?

P.S. Full code:

ALTER FUNCTION [dbo].[GetNormalWorkdaysCount] (
@startDate DATETIME,
@endDate DATETIME
)   
RETURNS INT

AS
BEGIN
    declare @Count INT,
            @CurrDate DATETIME
    set @CurrDate = @startDate

    while (@CurrDate < @endDate)
    begin

    end

    return @Count
END
GO
  • 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-06-12T14:26:32+00:00Added an answer on June 12, 2026 at 2:26 pm

    Unlike some languages, the BEGIN/END pair in SQL Server cannot be empty – they must contain at least one statement.


    As to your actual problem – you’ve said you’re not a DB programmer. Most beginners to SQL tend to go down the same route – trying to write procedural code to solve the problem.

    Whereas, SQL is a set-based language – it’s usually better to find a set-based solution, rather than using loops.

    In this instance, a calendar table would be a real help. Such a table contains one row for each date, and additional columns indicating useful information for your business (e.g. what you consider to be a working day). It then makes your query for working days look like:

    SELECT COUNT(*) from Calendar
    where BaseDate >= @StartDate and BaseDate < @EndDate and IsWorkingDay = 1
    

    Populating the Calendar table becomes a one off exercise, and you can populate it with e.g. 30 years worth of dates easily.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the fastest and most stable non-sql database to store big data and
I have a mysql database that has text stored in it with non encoded
I have a loop that enters each non-empty text field into a database: foreach
hobbyist/non-profession programmer here. I don't have a strong grasp on network addressing. I have
I am writing a PHP application targeted at non-geeks, non-programmers. I need to create
should a non-database data class I need be created as a Rails 3 model,
I am getting started with a non-SQL database (specifically, MongoDB) and Ruby on Rails
I'm developing an web application that uses a non-relational database as a backend (django-nonrel
Which Non-relational database should I choose(I know the question is a subjective one and
It is the first tie to use non sql database and first time also

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.