Reputation: 1001
How do I convert/cast the string April 04, 2016 12:00:00
to DATETIME
in sql server 2005?
UPDATE I would like to compare dates like this :
SELECT *
from ZeroAndLackingRequest
WHERE request_date BETWEEN CONVERT(DATETIME,'April 04, 2016 12:00:00',103)
AND CONVERT(DATETIME,'April 04, 2016 11:59:59',103)
but it is not working. Any Ideas why?
Upvotes: 1
Views: 488
Reputation: 12340
Select * from [ZeroAndLackingRequest] Z where Z.request_date = CONVERT(datetime, 'April 04, 2016 12:00:00', 120)
remember I choose 120 style you can chooose 103 or any other please see below link.
For a full discussion of CAST and CONVERT, including the different date formatting options, see the MSDN Library Link below:
http://msdn.microsoft.com/en-us/library/ms187928.aspx
If you want to compare
Select * from [ZeroAndLackingRequest] Z
where cast(Z.request_date as datetime) >= cast('April 04, 2016 12:00:00' as datetime)
and cast(Z.request_date as datetime) < cast('April 04, 2016 11:59:59' as datetime)
Upvotes: 0
Reputation: 6729
SELECT cast('April 04, 2016 12:00:00' AS datetime)
OR
SELECT CONVERT(DATETIME,'April 04, 2016 12:00:00')
Upvotes: 1