SET DATEFIRST功能 [英] SET DATEFIRST in FUNCTION

查看:169
本文介绍了SET DATEFIRST功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的功能中设置DATEFIRST,但不允许。

  SET DATEFIRST 1 

我可以在SP中添加代码,并从该函数调用SP,但我不热衷于这样做。 >

我可以在调用我的功能之前设置DATEFIRST,但是我也不喜欢这样做。



任何其他解决问题?



编辑



以下是我要使用的代码我的功能返回当月的总工作日。但是由于我的DATEFIRST,我无法将此代码添加到FUNCTION中。

  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不在(0,1)
SET DateFirst 1 - 设置为monday = 1(value)

- 一个月的第一天和最后一天的星期日和星期日将扣除1
IF(DATEPART(工作日,(DATEADD(dd, - (DAY(@mydate)-1),@ mydate)) )> 5)
SET @myDeduct = @myDeduct + 1

IF(DATEPART(工作日,(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)))

设置@TotalDays =(select(((@ my / 7) (@ my%7)) - @myDeduct))

选择@TotalDays


解决方案

orkaround是使用已知的日期进行比较。



例如,我需要检查一个日期是星期六。而不是依赖 DATEFIRST 或语言设置(使用 DATENAME ),而是说:

  DATEPART(工作日,DateToCheck)= DATEPART(工作日,'20120714')

我知道2012年7月14日是星期六,所以我没有依赖任何外部设置来执行支票。






表达式(DATEPART(工作日,DateToCheck)+ @@ DATEFIRST)%7 将始终为星期六产生值0, 1星期日,2星期一等。



所以,我建议你创建一个表:

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

填充此表是一次性练习。 NormalisedDay 将是上面给出的表达式计算的值。



要计算 DaysInMonth 给定一个特定的日期,您可以使用以下表达式:

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

现在所有的功能都要查看表中的值。



(当然, DaysInMonth 的所有行的结果将是20,所以只有29,30和31的行需要一点工作生产)


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

解决方案

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)

这篇关于SET DATEFIRST功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆