Click here to Skip to main content
15,911,848 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hello Everyone,
I have created report for timesheet. This run perfect but I get one issu in that.
suppose
on 12/1/2016 I have fill up timesheet and
on 13/1/2016 i have fill up timesheet.
if i saw the report for 13/1/2016 then it will work perfectly,but when i select report for 12/1/2016 that time this date record and 13/1/2016 all record display.
I will send report aspx page and store procedure for it.
plz help for this solution.


SQL
ET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[DailyTimeSheet_BasicReport]
	@StartDate		varchar(50) = '',
	@EmployeeId		INT = 0
	
AS 

	DECLARE @strQuery	varchar(5000)
	DECLARE @strCondition varchar(5000)
	DECLARE @strOrderBy	varchar(500)
	
	SET @strCondition = ' WHERE 1 = 1 '
	
	SET @strQuery=' SELECT CONVERT(VARCHAR, T.CreateDateTime,105) AS [LoginDate],
		
		 E.FullName,
	     T.TaskTimeIn,RIGHT(CONVERT(VARCHAR,T.TaskTimeIn,100),7) AS [TimeIn],
		 T.TaskTimeOut,RIGHT(CONVERT(VARCHAR,T.TaskTimeOut,100),7) AS [TimeOut],
		 T.TaskDescription,
	     CONVERT(varchar(5), DATEADD(minute, DATEDIFF(minute, TaskTimeIn, ISNULL(TaskTimeOut,SWITCHOFFSET(SYSDATETIMEOFFSET(), ''+05:30''))), 0), 114) AS [TimeSpent]  ,
		 DATEDIFF(minute, TaskTimeIn, ISNULL(TaskTimeOut,SWITCHOFFSET(SYSDATETIMEOFFSET(), ''+05:30''))) AS [TimeSpentMinutes],
		E.EmployeeId
	FROM  
		DailyTimeSheetTrackers T
		INNER JOIN Employees E ON E.EmployeeId = T.EmployeeId
	'
	
	if (@StartDate <> '')
		SET @strCondition = @strCondition + ' AND CAST(CONVERT(VARCHAR ,T.CreateDateTime,101) AS DATE) >= CONVERT(VARCHAR ,'''+ @StartDate +''',101) '
	

	if (@EmployeeId >0)
		SET @strCondition = @strCondition + ' AND E.EmployeeId = ' + CAST(@EmployeeId AS varchar(10))
	
	
	SET @strOrderBy = ' Order BY T.CreateDateTime,E.EmployeeId ' 
	
	PRINT @strQuery + @strCondition + @strOrderBy
	EXECUTE (@strQuery + @strCondition + @strOrderBy)
Posted
Comments
John C Rayan 13-Jan-16 9:07am    
what format you are sending @StartDate as ? You should use CAST(@CreateDateTime AS DATE) >= CAST(@StartDate as date) for your date comparison. I assume that you ignore the time portion.
Richard Deeming 13-Jan-16 9:40am    
Your code is vulnerable to SQL Injection[^]. NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.

From within SQL Server, use sp_executesql[^] to execute a parameterized query:

If @StartDate != '' SET @strCondition = @strCondition + ' AND CAST(T.CreateDateTime As date) >= @StartDate';
If @EmployeeId > 0 SET @strCondition = @strCondition + ' AND E.EmployeeId = @EmployeeId';
...
EXEC sp_executesql @strQuery + @strCondition + @strOrderBy, N'@StartDate date, @EmployeeId int', @StartDate = CAST(@StartDate As date), @EmployeeId = @EmployeeId;

1 solution

your input date is British date format use CONVERT(VARCHAR, T.CreateDateTime,103)
for all date comparison.
its will be work.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900