iccsi ( ) writes:
> I have a datetime field which contains data like following:
>
> 03:15:00 PM
> 04:25:00 PM
>
> I have a query and would like to query on this field like following:
>
> Select myfield from mytable where mydatetime = @MyDateTime
>
> I pass '03:15:00 PM' to the function.
> For some reason, it does not return the record.
> I tried to use convert(char(15), @MyDateTime, 108) which does not work
> and tried to use cast which does not work either.
There is no time-only data type in SQL 2000. (It was added in SQL 2008),
so you cannot have a value of 02:12:23 in datetime column. If you do:
DECLARE @d datetime
SELECT @d = '03:15:00 PM'
SELECT @d
You will find that it returns a value of 1900-01-01 15:15:00.000,
1900-01-01, being the default date when no date is provided.
If you want to work with time-only data, the best is probably to stick
with this default date, and then filter it out in displays.
--
Erland Sommarskog, SQL Server MVP, esquel.RemoveThis@sommarskog.se
Links for SQL Server Books Online:
SQL 2008:
http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005:
http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx >> Stay informed about: pass pure time to my query