0

I Need All Dates In Result Exclude Sunday:

Input : Start Date = 2016-07-01 and End Date = 2016-07-31

S_No    Dates
1   01-07-2016
2   02-07-2016
3   04-07-2016
4   05-07-2016
5   06-07-2016
6   07-07-2016
7   08-07-2016
8   09-07-2016
9   11-07-2016
10  12-07-2016
11  13-07-2016
12  14-07-2016
13  15-07-2016
14  16-07-2016
15  18-07-2016
16  19-07-2016
17  20-07-2016
18  21-07-2016
19  22-07-2016
20  23-07-2016
21  25-07-2016
22  26-07-2016
23  27-07-2016
24  28-07-2016
25  29-07-2016
26  30-07-2016

I Need Result Like This. If I am giving input Input : Start Date = 2016-06-01 and End Date = 2016-06-30. All Dates Should Come in June'16 Exclude Sunday.

Kindly help me ?

1
  • Show us some code, please. Jul 26, 2016 at 9:29

6 Answers 6

0

Check this one

WITH mycte AS
 (
   SELECT CAST('2016-06-01' AS DATETIME) DateValue
   UNION ALL
   SELECT  DateValue + 1
   FROM    mycte   
   WHERE   DateValue + 1 < '2016-07-01'
 )

 SELECT  DateValue
 FROM    mycte
 WHERE ((DATEPART(dw, DateValue) + @@DATEFIRST) % 7) NOT IN (1)
 OPTION (MAXRECURSION 0);

the output will be..

DateValue
2016-06-01 00:00:00.000
2016-06-02 00:00:00.000
2016-06-03 00:00:00.000
2016-06-04 00:00:00.000
2016-06-06 00:00:00.000
2016-06-07 00:00:00.000
2016-06-08 00:00:00.000
2016-06-09 00:00:00.000
2016-06-10 00:00:00.000
2016-06-11 00:00:00.000
2016-06-13 00:00:00.000
2016-06-14 00:00:00.000
2016-06-15 00:00:00.000
2016-06-16 00:00:00.000
2016-06-17 00:00:00.000
2016-06-18 00:00:00.000
2016-06-20 00:00:00.000
2016-06-21 00:00:00.000
2016-06-22 00:00:00.000
2016-06-23 00:00:00.000
2016-06-24 00:00:00.000
2016-06-25 00:00:00.000
2016-06-27 00:00:00.000
2016-06-28 00:00:00.000
2016-06-29 00:00:00.000
2016-06-30 00:00:00.000
0

If you want a query that returns dates excluding Sunday try this:

declare @dateStart date
declare @dateEnd date

set datefirst 7 -- Setting Sunday as firstday of the week

set @dateStart = cast('2016-06-01' as date) -- Maybe as paramters?
set @dateEnd = cast('2016-06-30' as date)



;WITH T AS (SELECT @dateStart as d union all
            SELECT DATEADD(DAY,1,d) from T where d<@dateEnd)
select *
  from T
where datepart(WEEKDAY,d) <> 1

Output

d
2016-06-01
2016-06-02
2016-06-03
2016-06-04
2016-06-06
2016-06-07
2016-06-08
2016-06-09
2016-06-10
2016-06-11
2016-06-13
2016-06-14
2016-06-15
2016-06-16
2016-06-17
2016-06-18
2016-06-20
2016-06-21
2016-06-22
2016-06-23
2016-06-24
2016-06-25
2016-06-27
2016-06-28
2016-06-29
2016-06-30
0

Create a table of integers:

SELECT TOP 1000000
        IDENTITY(INT,1,1) as N
   INTO dbo.NumbersTable
   FROM Master.dbo.SysColumns 

Create a table for range of dates

DECLARE @DaysFromStart int
DECLARE @StartDate datetime
SET @StartDate = '10/01/2008'

SET @DaysFromStart  = (SELECT (DATEDIFF(dd,@StartDate,GETDATE()) + 1))

CREATE TABLE [dbo].[TableOfDates](
    [fld_date] [datetime] NOT NULL,
 CONSTRAINT [PK_TableOfDates] PRIMARY KEY CLUSTERED 
(
    [fld_date] ASC
)WITH FILLFACTOR = 99 ON [PRIMARY]
) ON [PRIMARY]


INSERT INTO
     dbo.TableOfDates
SELECT 
      DATEADD(dd,nums.n - @DaysFromStart ,CAST(FLOOR(CAST(GETDATE() as FLOAT)) as DateTime)) as FLD_Date
FROM dbo.NumbersTable nums

SELECT MIN(FLD_Date) FROM dbo.TableOfDates
SELECT MAX(FLD_Date) FROM dbo.TableOfDates

The final query for you to use:

SELECT FLD_Date FROM TableOfDates (NOLOCK) WHERE FLD_Date >= '2016-07-01' AND FLD_Date <= '2016-07-31' AND DATENAME(dw,FLD_Date) <> 'Sunday'

Reference: Stack overflow

0

solution to your problem is as given below :

DECLARE @START_DATE DATETIME='2:57 PM 7/01/2016',@END_DATE DATETIME='2:57 PM 7/26/2016'


WHILE(@START_DATE<=@END_DATE)
BEGIN
IF(DATENAME(WEEKDAY,@START_DATE)<>'Sunday')
BEGIN
   PRINT @START_DATE --Modify code according to your need.
END
SELECT @START_DATE=DATEADD(D,1,@START_DATE)
END

Result :

Jul  1 2016  2:57PM
Jul  2 2016  2:57PM
Jul  4 2016  2:57PM
Jul  5 2016  2:57PM
Jul  6 2016  2:57PM
Jul  7 2016  2:57PM
Jul  8 2016  2:57PM
Jul  9 2016  2:57PM
Jul 11 2016  2:57PM
Jul 12 2016  2:57PM
Jul 13 2016  2:57PM
Jul 14 2016  2:57PM
Jul 15 2016  2:57PM
Jul 16 2016  2:57PM
Jul 18 2016  2:57PM
Jul 19 2016  2:57PM
Jul 20 2016  2:57PM
Jul 21 2016  2:57PM
Jul 22 2016  2:57PM
Jul 23 2016  2:57PM
Jul 25 2016  2:57PM
Jul 26 2016  2:57PM
0

You can use the given query,

here dw in datepart() function indicates the "week day". You want to exclude Sunday so write 1 in the condition in where clause.

Query:

select S_No, Dates
from table_name 
where datepart(dw,Dates) <>1 and Dates >='2016-06-01' and Dates<='2016-06-30'

So according to this query you will get all the dates between 2016-06-01 and 2016-06-30 excluding sundays.

0

SELECT * FROM tableName WHERE DATE BETWEEN '2016-06-01 03:13:45.040' AND '2016-06-30 03:13:45.040' AND DATEPART(dw,Date)<>1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.