A function of SQL Server that gets how to break days between two dates

Source: Internet
Author: User
Tags datetime integer new features
server| function In fact, this function is not provided by SQL Server, is tofu written, we do not because of tofu piece of everyone
To use a stone to hit tofu: In fact, since this function generated after we use, it is impossible to distinguish between this
Whether the function is SQL Server or a custom function, this is one of the new features that SQL2K brings to us
Let's take a look at the meaning of this function, in fact, the person who originally asked me for this function is blue, but there was no sql2k,
Now that I've got sql2k, I've found some time to write this function. The function is in any two
Days to get rest between time. At present, only two repair days are processed:
Okay, let's take a look at this function, and by the way, we can learn to use this new feature of SQL2K.

CREATE FUNCTION Getvac (@Start datetime, @End datetime)
RETURNS Integer AS
BEGIN
Declare
@intNum Integer,
@intAllDay Integer,
@i Integer,
@intWeekDay Integer,
@tempDateTime datetime,
@intVac integer
Select @intAllDay =datediff (DD, @Start, @End)
Select @i=0
Select @intVac =0
while (@i< @intAllDay)
Begin
Select @tempDateTime =dateadd (dd,@i, @Start)
Select @intWeekDay =datepart (DW, @tempDateTime)-1
if (@intWeekDay =6) or (@intWeekDay =0)
Begin
Select @intVac = @intVac +1
End
Select @i=@i+1
End
Return @intVac
End
OK, after this function is generated, we can use this function, for example:
Select Aa=dbo. Getvac (2000-12-10 00:00:00.000, ' 2000-12-18 00:00:00.000 ')
The result is: 3 hehe



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.