Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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
Comments
[no name] 16-Mar-15 11:23am    
Why don't you store the date as a date?
Sinisa Hajnal 16-Mar-15 11:27am    
Why all the converts? If you're passing date into the stored procedure, your table keeps dates as data, the column should be date column and you should be able to compare directly. If you pass varchar from outside and compare with database date column you should have fixed format that you're passing in. Keeping date field as varchar is a) bad design and b) hard to use as you're just realizing. The solution is to check the column for variations and find the format that works. Simple.
Uwakpeter 16-Mar-15 12:53pm    
Date column is dateTime type sir and i using GetDate() function to save date, i cant compare it directly because of the millisec and nanosec parts!
Sinisa Hajnal 17-Mar-15 3:11am    
Of course you can - see my solution.
Maciej Los 16-Mar-15 11:37am    
Do not Repost[^]!!!

Don't convert dates to varchar.

It looks like you're trying to ignore the time part, and retrieve all records for a particular date. If you're using Microsoft SQL Server 2008 or higher, you could change the column and the parameter to the date type[^], which doesn't store the time.

Otherwise, you'll need to look for all dates between the start and end of the specified date:
SQL
IF @AttndDate Is Null SET @AttndDate = GetUtcDate();
SET @AttndDate = DateAdd(dd, 0, DateDiff(dd, 0, @AttndDate));

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, 
    DateAdd(dd, 0, DateDiff(dd, 0, r.ATTNDDate)) AS ATTNDATE
FROM 
    RCORegister r
    INNER JOIN UserMgmt u ON r.Username = u.Username
WHERE
    r.ATTNDDate >= @AttndDate
And
    r.ATTNDDate < DateAdd(day, 1, @AttndDate)
ORDER BY
    r.ATTNDDate DESC
 
Share this answer
 
Comments
Uwakpeter 16-Mar-15 13:09pm    
This only selects records of today, and it doesnt use @AttndDate parameter criteria to select record base on the date user supplies.
Richard Deeming 16-Mar-15 13:11pm    
What are you talking about? If you pass the @AttndDate parameter, then that date is used.
Uwakpeter 16-Mar-15 13:28pm    
On C#, i am passing user input to @AttndDate parameter

cmd.Parameters.Add("@AttndDate", SqlDbType.DATETIME).Value = txtDate.Text;
Richard Deeming 16-Mar-15 13:37pm    
And? If you pass a date in the parameter, that date will be used in the query.
Uwakpeter 16-Mar-15 14:05pm    
date that is entered in txtDate is what i am passing to @AttndDate parameter in stored procedure
Since you store the date as a DATETIME value in the DB (or there would be no point in using CONVERT on it), and pass the comparison value as a DATETIME as well, why not just leave it where it is and check the actual value directly?
SQL
...
INNER JOIN UserMgmt u ON r.Username = u.Username
WHERE  r.ATTNDDate = @AttndDate
ORDER BY  r.ATTNDDate DESC
 
Share this answer
 
Comments
Uwakpeter 16-Mar-15 12:47pm    
i am going to accept date from users in this format: dd/mm/yyyy
so if i use WHERE r.ATTNDDate = @AttndDate, that wont work!
OriginalGriff 16-Mar-15 12:55pm    
You are passing the date from the user though as a DATETIME parameter:

@AttndDate DateTime = NULL

And DATETIME values don't have a format (that's one of the really good things about them) - they are stored as a number of milliseconds since a specific point in time, so it doesn't matter what format the user enters the date in - once it's a DATETIME it has lost all formatting and will compare against any other DATETIME value correctly.
Uwakpeter 16-Mar-15 12:59pm    
yes, i am passing the date from the user though as a DATETIME parameter:

@AttndDate DateTime = NULL
OriginalGriff 16-Mar-15 13:12pm    
So your two values are both DATETIME and will compare natively regardless of the format they were originally entered in.

The only thing you might want to consider (if your original INSERT was a GETDATE() call) is that a DATETIME stores the time to millisecond level - so it could be worth CASTing the two values to float and using FLOOR to remove the time when you do equality checks:

INNER JOIN UserMgmt u ON r.Username = u.Username
WHERE FLOOR((CAST(r.ATTNDDate AS FLOAT)) = FLOOR((CAST(@AttndDate AS FLOAT))
ORDER BY r.ATTNDDate DESC

Since SQL DATETIME is a float value, and the time is held in the fractional part, this compares just the Date parts very, very quickly.
Uwakpeter 16-Mar-15 13:25pm    
have tried this, this is the error message:
An expression of non-bolean type specified in a context where a condition is expected!
You should never involve strings into date comparisons. It is slow, easy to get wrong and hard to debug.

The following will all give you options for your select:

DATEADD(dd, DATEDIFF(dd, 0, r.ATTNDDate), 0) will give you date only
as will
CONVERT(DATE, r.ATTNDDate) - this assumes SQL Server 2008 or higher


You comparison could be:

DATEADD(dd, DATEDIFF(dd, 0, r.ATTNDDate), 0) = @AttNDDate if your variable is "pure" date (that is time component is zero)

Change the comparison operator as needed...

If, in your table, you have whole range of times then you could do
r.ATTNDDate BETWEEN @AttNDDate AND DateAdd(d, 1, @AttNDDate )


You could simplify this even further if you don't need time component in the database. Just change your insert statement to cast GetDate() as above to get only date component (and you'll have to run an update on the existing data of course). This way you can do plain and simple comparison
r.ATTNDDate = @ATTNDDate

I hope this helps. Good luck.
 
Share this answer
 
Comments
Uwakpeter 17-Mar-15 4:02am    
Thanks, Please should i change datetime type in the date column to date type? so it stores only date and time part will be zero?
Sinisa Hajnal 17-Mar-15 4:35am    
That would be ideal. But if you don't want (or cannot) change nad since you already have datetime you could only update it (and take care what get into it) :)
Uwakpeter 17-Mar-15 6:18am    
Thank you sir, i have changed the dateTime type to Date in the date column, i now change the stored procedure to this:

ALTER PROCEDURE [dbo].[SelectRCOByDate]
-- Add the parameters for the stored procedure here
@Username Varchar (50)=NULL,
@AttndDate Date =NULL
AS
IF @AttndDate is null
SET @AttndDate = getdate()
BEGIN


SET NOCOUNT ON;
--SET DATEFORMAT dmy
-- 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,
r.ATTNDDate AS ATTNDATE
FROM RCORegister r
INNER JOIN UserMgmt u ON r.Username = u.Username
WHERE r.ATTNDDate = @AttndDate
ORDER BY r.ATTNDDate DESC
END


This only returns current date records, and it doesnt check for @AttndDate parameter, i.e. i can run it without the parameter and it will still returns records. Please how can i handle this?
Sinisa Hajnal 17-Mar-15 7:00am    
It does wor @AttndDate parameter...its only that you've set it to current date.
If you pass it some other value it will take rows with given date.

It returns records because you supplied default values (NULL) to both parameters...if you don't want the SP to run without parameters remove = NULL from parameter declaration.
Uwakpeter 17-Mar-15 11:42am    
Thanks you sir, when i remove this lines:
IF @AttndDate is null
SET @AttndDate = getdate()

and declare:@AttndDate Date (without assigning null to it) it reports error!

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