Karen Schaefer
Karen Schaefer

Reputation: 109

How to convert Datetime to Date and use in Where clause?

What am I missing I need to convert datetime to just date and use it in a between statement in a where clause.

CreatedDate 2016-07-19 16:00:19.710

 WHERE convert(Date,CreatedDate) >='2016-06-01' AND convert(Date,CreatedDate) <= '2016-06-31'

I am getting conversion failed when converting date and/or Time from character string.

Upvotes: 2

Views: 20778

Answers (2)

Karen Schaefer
Karen Schaefer

Reputation: 109

thanks it turns out the I need to formate the search criteria to include date and time where CreatedDate>= '2016-07-01 00:00:00.000'AND CreatedDate<= '2016-07-30 00:00:00.000'

Upvotes: 0

xbb
xbb

Reputation: 2163

You can use cast(CreatedDate as Date) to convert. But I don't think you need to convert in this case to compare.

Just do:

WHERE CreatedDate between '2016-06-01' and '2016-06-31' should be fine

Upvotes: 3

Related Questions