JanLeeYu
JanLeeYu

Reputation: 1001

How to convert sql string datetime

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

Answers (2)

Rajarshi Das
Rajarshi Das

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

Abdul Rasheed
Abdul Rasheed

Reputation: 6729

SELECT cast('April 04, 2016 12:00:00' AS datetime)

OR

SELECT CONVERT(DATETIME,'April 04, 2016 12:00:00')

For more details

Upvotes: 1

Related Questions