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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T13:38:44+00:00 2026-06-11T13:38:44+00:00

I want to SET DATEFIRST in my function but it is not allowed. SET

  • 0

I want to SET DATEFIRST in my function but it is not allowed.

SET DATEFIRST 1

I can add the code in a SP and call the SP from the function but I am not keen on doing that.

I can SET the DATEFIRST before I call my function but I am not keen on doing that as well.

Any other work around?

EDIT

Below is the code I want to use in my FUNCTION to return the total working days of the month. But I cant add this code into the FUNCTION because of my DATEFIRST

DECLARE @my int
DECLARE @myDeduct int
DECLARE @day INT
DECLARE @mydate DATETIME
DECLARE @TotalDays INT

SET @mydate = GETDATE()

SET @myDeduct = 0
IF (@@DATEFIRST + DATEPART(DW, @mydate)) % 7 not in (0,1)
SET DateFirst 1 -- Set it monday=1 (value)

--Saturday and Sunday on the first and last day of a month will Deduct 1
IF (DATEPART(weekday,(DATEADD(dd,-(DAY(@mydate)-1),@mydate))) > 5)
SET @myDeduct = @myDeduct + 1

IF (DATEPART(weekday,(DATEADD(dd,-(DAY(DATEADD(mm,1,@mydate))),DATEADD(mm,1,@mydate)))) > 5)
SET @myDeduct = @myDeduct + 1

SET @my = day(DATEADD(dd,-(DAY(DATEADD(mm,1,@mydate))),DATEADD(mm,1,@mydate)))

Set @TotalDays = (select (((@my/7) * 5 + (@my%7)) - @myDeduct))

Select @TotalDays
  • 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-11T13:38:45+00:00Added an answer on June 11, 2026 at 1:38 pm

    My usual workaround is to use “known-good” dates for my comparisons.

    Say, for instance, that I need to check that a date is a saturday. Rather than relying on DATEFIRST or language settings (for using DATENAME), I instead say:

    DATEPART(weekday,DateToCheck) = DATEPART(weekday,'20120714')
    

    I know that 14th July 2012 was a Saturday, so I’ve performed the check without relying on any external settings.


    The expression (DATEPART(weekday,DateToCheck) + @@DATEFIRST) % 7 will always produce the value 0 for Saturday, 1 for Sunday, 2 for Monday, etc.

    So, I’d advise you to create a table:

    CREATE TABLE WorkingDays (
        NormalisedDay int not null,
        DaysInMonth int not null,
        WorkingDays int not null
    )
    

    Populating this table is a one off exercise. NormalisedDay would be the value computed by the expression I’ve given above.

    To compute the DaysInMonth given a particular date, you can use the expression:

    DATEDIFF(day,
          DATEADD(month,DATEDIFF(month,0,DateToCheck),0),
          DATEADD(month,DATEDIFF(month,'20010101',DateToCheck),'20010201'))
    

    Now all your function has to do is look up the value in the table.

    (Of course, all of the rows where DaysInMonth is 28 will have 20 as their result. It’s only the rows for 29,30 and 31 which need a little work to produce)

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

Sidebar

Related Questions

I want set Listbox background to transparent but not working Is there any idea?
I want set result to variable from MVC controller method. function someFunction(){ var result;
I have a subclass from control and I want set text for that. When
i want set value of input type if condition is true but my function
As my title, I want set multiple conditions in will_paginate, that's my model's code
i want set class for repeater when doPostBack for those dataID i wan but
i have a multiple marker Google map, it works fine but i want set
I want to set a repeating alarm monthly; but my app resets the alarm
I want to set the type of a list to be ints but I
After complete of asynchronous call to WCF service I want set success message into

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.