Showing posts with label Query. Show all posts
Showing posts with label Query. Show all posts

SQL Server : How to produce zero without using any numbers using SQL query

Have you ever thought producing '0' ZERO with using any digit or number in your SQL query.

I came across a good trick to produce ZERO (0) without using any number in T SQL statement by Madhivanan.

If you have any idea about the same....lets know through your comments OR click here to view the original link.

If you find the above link useful, let us know your feedback, it will help us to improve our posting(s). or You can send your feedback linkOblast.
Report Broken Link

CountBusinessDays


A good functions that everyone can use to Calculate Business Days between two dates.

Click here to view the original link


Excerpts:

Working with Access, I needed to calculate business days between
dates, excluding holidays, and with an eventual conversion fo the DB to
SQL Server, rewrote the VBA into an SQL function, below:


set ANSI_NULLS ON

set QUOTED_IDENTIFIER ON

go


— =============================================

— Author: James Igoe

— Create date: 2008-08-29

— Description: Calculates business days between 2 dates

— =============================================

ALTER FUNCTION [dbo].[Fx_WorkdayCount]

(

@sStartDate As DATETIME,

@sEndDate As DATETIME

)

RETURNS SMALLINT

AS

BEGIN

— Declare the return variable here

DECLARE @BusinessDays SMALLINT


DECLARE @intDaysTotal As SMALLINT

DECLARE @intDaysWeeks As SMALLINT

DECLARE @intRemainder As SMALLINT

DECLARE @intRemainderPost As SMALLINT

DECLARE @intRemainderPre As SMALLINT


SET @intDaysTotal = DateDiff(day, @sEndDate, @sStartDate)

SET @intDaysWeeks = ((@intDaysTotal / 7) * 5)

SET @intRemainder = @intDaysTotal % 7


IF @intRemainderPre = 1

SET @intRemainder = @intRemainder - 1

ELSE IF @intRemainderPre = 7

SET @intRemainder = @intRemainder - 2


IF @intRemainderPost = 1

SET @intRemainder = @intRemainder - 1

ELSE IF @intRemainderPost = 7

SET @intRemainder = @intRemainder - 2


IF (@intRemainderPost < @intRemainderPre) And @intRemainderPre <> 7

SET @intRemainder = @intRemainder - 2