1

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?

2
  • SELECT cast('April 04, 2016 12:00:00' AS datetime) Commented Apr 4, 2016 at 6:56
  • I have updated my question, kindly check it Commented Apr 4, 2016 at 7:01

2 Answers 2

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

OR

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

For more details

Sign up to request clarification or add additional context in comments.

2 Comments

Plz check the date you entered in where clause, the lowest date should be first (change to eg:- SELECT * from ZeroAndLackingRequest WHERE request_date BETWEEN CONVERT(DATETIME,'April 01, 2016 12:00:00',103) AND CONVERT(DATETIME,'April 05, 2016 11:59:59',103))
if you don't need to check the time, them you can COVERT to DATE instead of DATETIME
0
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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.