title: SET DATEFIRST in FUNCTION
body: <p>I want to SET DATEFIRST in my function but it is not allowed.</p>
  
  <pre><code>SET DATEFIRST 1
  </code></pre>
  
  <p>I can add the code in a SP and call the SP from the function but I am not keen on doing that.</p>
  
  <p>I can SET the DATEFIRST before I call my function but I am not keen on doing that as well.</p>
  
  <p>Any other work around?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>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</p>
  
  <pre><code>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))) &gt; 5)
  SET @myDeduct = @myDeduct + 1
  
  IF (DATEPART(weekday,(DATEADD(dd,-(DAY(DATEADD(mm,1,@mydate))),DATEADD(mm,1,@mydate)))) &gt; 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
  </code></pre>