Contents
Can I store date as varchar?
There is nothing stopping you putting non-date data in the VARCHAR column in the database. The VARCHAR version is culture specific. You can’t easily sort the dates.
Should I store date string?
Is it safe to store dates as a string in mysql? It is safe as long as the format that you use to represent your dates is unambiguous (that is, each value maps to a unique date). But it is always inefficient not to use the proper datatype to store a value.
When should I use varchar or string?
1 Answer
- Use varchar if you want to have a length constraint.
- Use string if you don’t want to restrict the length of the text.
Can a date be stored in A varchar column?
You can store date in a VARCHAR column, you can also store non-dates in that VARCHAR column.
How to compare dates which are stored as string in MySQL?
Dates are stored in this fashion: For ex: ‘2014-10-30 10:10:10’ in database. Now I want to compare two dates and fetch records from database by using query, 2014-09-10 10:10:10 ( fromdate) to 2014-10-10 10:10:10 ( todate) How to fetch all accurate records.. Is there any kind of solution.. Thanks. Just compare the string without extra overhead.
How big is a varchar date in PHP?
Which means your VARCHAR date is at least 2.67 times the size of a DATE. 3.33 if you include separators. Or much larger if your string dates are not formatted sets of digits. That’s not huge, and probably not a concern for space on disk. But for space in memory, when you need to fetch or sort the data, it adds up.
Can you cast A varchar to a date?
You can CAST your varchar values to DATETIME or DATE type. This might not be an optimal solution, because you may lose the benefit that indexes provide. The problem is that the string data has to be in the default datetime format in order for CAST/CONVERT to work – need to use STR_TO_DATE if the format doesn’t match.