Reputation: 5077
I need to cast a string to a datetime so i can compare it later.
The varchar that i have is like this format:
29/11/2013 12:00:00 a.m.
And i need it to cast to:
2013-11-29 00:00:00.000
Im using MSSQL Server 2012
Thx for all
Upvotes: 1
Views: 33039
Reputation: 1
IF Your date is of type varchar in the database then if you need to retrieve then these cases arises:: case1:if your data is in the format of "dd-MM-yyyy" then you need to use query as follows Query:: select * from [yourTableName] where convert(datetime,YourDateColumn,103) between '2016-02-12'(yyyy-MM-dd) AND '2016-03-12'
case2:if your data is in the format of "dd-MM-yyyy HH:mm:ss"
then you need to use query as follows Query:: select * from [yourTableName] where convert(datetime,YourDateColumn,105) between '2016-02-12'(yyyy-MM-dd) AND '2016-03-12'
Upvotes: 0
Reputation: 1022
Please, have a look a CAST and CONVERT for more information.
Here are some examples:
-- converting from DD-MM-YYYY
select CONVERT(datetime, '29/11/2013 12:00:00 AM', 103)
-- converting from MM-DD-YYYY
select CONVERT(datetime, '11/29/2013 12:00:00 AM', 101)
Upvotes: 1