Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Calculating No of Week Days

Author  Topic 

GirishKumarSharma
Starting Member

10 Posts

Posted - 2011-11-09 : 00:12:24
Hi,
After searching a lot in google i think i have to create my login in sql forum to get SQL Server 2005 help.
I need a procedure which will accept a number i.e. month and will return me the number of days something like this :

Days Count
Sunday Number of Sundays in given month
Monday Number of Mondays in given month
Tuesday ....
Wednesday ....
Thursday
Friday
Saturday

I am using SQL Server 2005 and visual basic 6 for a project. In this project, user will select Sunday,Monday... from combo box and in another combo box which will be populated by above procedure by number of sunday,mondays respectively. I wish to pass the current month to the procedure and then respective calculations.

Please guide me, how do i get. I am sure, the code is available in the forum somewhere, but not finding (new bie).

Thanks and Regards
Girish Sharma

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-11-09 : 00:59:17
something like

CREATE PROC DayinMonthCount
@MonthDate datetime,
@day varchar(10)
AS
DECLARE @MonthStart datetime,@EndDate datetime
SELECT @MonthStart=DATEADD(mm,DATEDIFF(mm,0,@MonthDate),0),@EndDate=DATEADD(mm,DATEDIFF(mm,0,@MonthDate)+1,0)-1

SELECT [Day],COUNT(*) AS daycnt
FROM dbo.CalendarTable(@MonthStart,@EndDate,0,0)
GROUP BY [Day]

calendar table can be found in below link

http://visakhm.blogspot.com/2010/02/generating-calendar-table.html


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

GirishKumarSharma
Starting Member

10 Posts

Posted - 2011-11-09 : 01:38:25
Thank you very much for such a great help. Kindly let me know how do i execute and pass the variable.

I have created the procedure though.

When i tried to run using Management Studio's Query Analyzer windows :
USE [ScheduleTask]
GO

DECLARE @return_value int

EXEC @return_value = [dbo].[DayinMonthCount]
@MonthDate = N'09/11/2011',
@day = N'Sunday'

SELECT 'Return Value' = @return_value

GO
I got :
Friday 5
Monday 4
Saturday 4
Sunday 4
Thursday 5
Tuesday 4
Wednesday 4

I wish to order by too as starting from Sunday and ending at Saturday.

Regards
Girish Sharma
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-11-09 : 01:46:11
quote:
Originally posted by GirishKumarSharma

Thank you very much for such a great help. Kindly let me know how do i execute and pass the variable.

I have created the procedure though.

When i tried to run using Management Studio's Query Analyzer windows :
USE [ScheduleTask]
GO

DECLARE @return_value int

EXEC @return_value = [dbo].[DayinMonthCount]
@MonthDate = N'09/11/2011',
@day = N'Sunday'

SELECT 'Return Value' = @return_value

GO
I got :
Friday 5
Monday 4
Saturday 4
Sunday 4
Thursday 5
Tuesday 4
Wednesday 4

I wish to order by too as starting from Sunday and ending at Saturday.

Regards
Girish Sharma


sorry didnt get that
how you want to pass variable?
do you mean return values through output variable?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2011-11-09 : 01:48:01
quote:
Originally posted by visakh16

something like

CREATE PROC DayinMonthCount
@MonthDate datetime,
@day varchar(10)
AS
DECLARE @MonthStart datetime,@EndDate datetime
SELECT @MonthStart=DATEADD(mm,DATEDIFF(mm,0,@MonthDate),0),@EndDate=DATEADD(mm,DATEDIFF(mm,0,@MonthDate)+1,0)-1

SELECT [Day],COUNT(*) AS daycnt
FROM dbo.CalendarTable(@MonthStart,@EndDate,0,0)
GROUP BY [Day]

calendar table can be found in below link

http://visakhm.blogspot.com/2010/02/generating-calendar-table.html


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/




what is the purpose of @day ? It is not used


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

GirishKumarSharma
Starting Member

10 Posts

Posted - 2011-11-09 : 01:57:54
>how you want to pass variable?

I wish to pass the current date to the procedure and then in visual basic 6 i have one combo box with day names like :
Sunday, Monday.... Saturday.
User selected any day...
After that selection of day there will be 4/5 check boxes will be visible and in these check boxes user will check/uncheck the value of these check boxes.
These check boxex values will be stored in a table something like :


NameofDay Ch1, Ch2, Ch3, Ch4, Ch5
Wednesday Yes Yes


Means, user has selected Wednesday and he checked the first and 3rd check box. Here 5 check boxes are visible because there are 5 wednesdays in the current month / given date from dtpicker control.
If user selected a day which is having 4 counts then 4 check boxes will be visible accordingly.

Please let me know, i am clear in my reply or not?

Regards
Girish Sharma
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-11-09 : 02:44:03
quote:
Originally posted by GirishKumarSharma

>how you want to pass variable?

I wish to pass the current date to the procedure and then in visual basic 6 i have one combo box with day names like :
Sunday, Monday.... Saturday.
User selected any day...
After that selection of day there will be 4/5 check boxes will be visible and in these check boxes user will check/uncheck the value of these check boxes.
These check boxex values will be stored in a table something like :


NameofDay Ch1, Ch2, Ch3, Ch4, Ch5
Wednesday Yes Yes


Means, user has selected Wednesday and he checked the first and 3rd check box. Here 5 check boxes are visible because there are 5 wednesdays in the current month / given date from dtpicker control.
If user selected a day which is having 4 counts then 4 check boxes will be visible accordingly.

Please let me know, i am clear in my reply or not?

Regards
Girish Sharma


the checkbox logic have to be done at front end

at sql this should be proc


CREATE PROC DayinMonthCount
@MonthDate datetime,
@day varchar(10)
AS
DECLARE @MonthStart datetime,@EndDate datetime
SELECT @MonthStart=DATEADD(mm,DATEDIFF(mm,0,@MonthDate),0),@EndDate=DATEADD(mm,DATEDIFF(mm,0,@MonthDate)+1,0)-1

SELECT [Day],COUNT(*) AS daycnt
FROM dbo.CalendarTable(@MonthStart,@EndDate,0,0)
WHERE [Day]=@day
GROUP BY [Day]


and cell it like
EXEC DayinMonthCount '2011-10-01','Tuesday'

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

GirishKumarSharma
Starting Member

10 Posts

Posted - 2011-11-09 : 03:26:09
Thank you so much.

Regards
Girish Sharma
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-11-09 : 03:30:18
wc

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -