Wednesday, March 7, 2012

How to compare the date in SQL Server 2000?

Hi all,
I have encountered a problem that I can't compare the date in SQL Server 2000.
But I know that maybe I have used "datetime" as data type for that field.
My question is that, how can I get ONLY the "date" in stead of "datetime" from the database?
Your attention is appreciated!
Thank you.

Regards,
katszetoYou can use Convert to get the date as a string, but SQL itself doesn't have a date only type. You need to use a temp var or datediff functions if you want to ignore the time.|||Is it first get the value from the database and then convert it to String and do comparison?|||you should always strive to avoid string comparisons for dates, what/where would be the ideal code you are looking for and we'll see what sort of solution you should have?|||Exactly what are you trying to do?
Give some requirements and then perhaps we can
give some more relevant help.|||One option:
Convert the date to a varChar specifying a format that does not involve time. Then CAST() the varchar back to datetime

WHERE CAST(CONVERT(varChar, myField, 112) AS SmallDateTime) = '1900/1/1'

Cheers
Ken|||Now, I want to enter a date as a parameter and search from the database.
Then display the result in the datagrid.
And the SQL Command is :Select * from [table] where date = [parameter]
I have tried that if I also input the date and time as parameter,
no record is found!!|||


CREATE PROC usp_myProc

@.Param smallDateTime

AS

-- Do some SELECT here

WHERE
CAST(CONVERT(varChar, myField, 112) AS SmallDateTime) = @.Param

Cheers
Ken|||You'd be much better off using DateDiff for that sort of operation.

No comments:

Post a Comment