. Advertisement .
..3..
. Advertisement .
..4..
Hi developer experts, I have a small but frustrating use case, and so far, I couldn’t get my head around this problem & ideal solution. I am running my command and facing one problem with the conversion of a varchar data type to a datetime data type resulted in an out-of-range value. Below is the command I used:
SELECT CAST(CONVERT(VARCHAR, LoginTime, 101) AS datetime) FROM AuditTrail
When I run it, I get the following error:
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
I am looking forward to gaining some knowledge from all experts. Thank you, guys!
The cause: The first query selects
mm/dd/yyyy
format, while the second query attempts to filter usingdd/mm/yyyy
format. Either of these will fail, resulting in an error.Solution: To fix the problem, make sure you’re filtering dates in the correct format for your database.
The different one:
We hope you find this helpful.
You can use built-in functions like Year and Month to filter this datetime.