Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello Everyone,

I am trying to select specific record from sql server based on user date selection, date format for users is in this format:dd/mm/yyyy from Ajax calenda extender, and i use getdate() to store date in the table, i.e. date format in the table is in this format:2015-03-16 08:36:08.330.

I have tried this:

SQL
ALTER PROCEDURE [dbo].[SelectRCOByDate] 
	-- Add the parameters for the stored procedure here
	@Username Varchar (50)=NULL,
	@AttndDate DateTime = NULL
AS
IF @AttndDate is null
SET @AttndDate = CONVERT(VARCHAR, getdate(), 103)
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	SELECT r.Username AS Username, r.surname AS Surname, r.firstname AS Firstname,u.branch AS Branch, r.RTime AS ResumptionTime,r.CTime AS ClosureTime, CONVERT(VARCHAR, r.ATTNDDate, 103) AS ATTNDATE
	FROM RCORegister r
	INNER JOIN UserMgmt u ON r.Username = u.Username
	WHERE  CONVERT(VARCHAR, r.ATTNDDate,103)  = @AttndDate 
	ORDER BY  r.ATTNDDate DESC

END


but i am having this error:
Error converting data type varchar to datetime.

Please is there anything i have done wrong? Thanks in advance.
Posted

1 solution

Replace:
SQL
SET @AttndDate = CONVERT(VARCHAR, getdate(), 103)

with:
SQL
SET @AttnDate = DATEADD(dd, DATEDIFF(dd, 0, getdate()), 0)

to reject/remove time part then change WHERE condition to:
SQL
WHERE AttndDate BETWEEN @AttndDate AND DATEADD(ss, -1, DATEADD(dd, 1, @AttndDate))


DateAdd (SQL)[^]

[EDIT]
SQL
ALTER PROCEDURE [dbo].[SelectRCOByDate] 
	-- Add the parameters for the stored procedure here
	@Username Varchar (50)=NULL,
	@AttndDate DateTime = NULL
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
        
        SET DATETIMEFORMAT dmy;

       IF @AttndDate is null SET @AttndDate = GETDATE()

       SET @AttndDate = DATEADD(dd, DATEDIFF(dd, 0, @AttndDate), @AttndDate)
       DECLARE @AttEndDate DATETIME = DATEADD(dd, -1, DATEADD(ss, -1, @AttndDate))
 
    -- Insert statements for procedure here
	SELECT r.Username AS Username, r.surname AS Surname, r.firstname AS Firstname,u.branch AS Branch, r.RTime AS ResumptionTime,r.CTime AS ClosureTime, CONVERT(VARCHAR, r.ATTNDDate, 103) AS ATTNDATE
	FROM RCORegister r
	INNER JOIN UserMgmt u ON r.Username = u.Username
	WHERE  r.ATTNDDate >= @AttndDate AND r.ATTNDDate <= @AttEndDate
	ORDER BY  r.ATTNDDate DESC
        --OR WHERE  r.ATTNDDate BETWEEN @AttndDate AND @AttEndDate
 
END
[/EDIT]
 
Share this answer
 
v4
Comments
Uwakpeter 16-Mar-15 5:31am    
Yes, I have tried that, still same error!
Maciej Los 16-Mar-15 5:35am    
So, the problem is when date is passed as a parameter by Ajax Calender. Please, check what value is passed into SP...
Uwakpeter 16-Mar-15 6:02am    
The date format from Ajax Calenda is in this format: 16/03/2015, as in dd/mm/yyyy
Maciej Los 16-Mar-15 6:06am    
Also, you need to add SETDATEFORMAT dmy; at the begining of SP, just after SET NOCOUNT statement ;)
Move this:
IF (@AttndDate is null) SET @AttnDate = DATEADD(dd, DATEDIFF(dd, 0, getdate()), 0)
into SP body (between BEGIN and END).
Notify me about results ;)
Uwakpeter 16-Mar-15 6:21am    
Yes, the error is no longer there, but it returns zero rows! please how do i make it return rows based on the date criteria? Thanks

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