0

my date format is like

2010-11-15 04:28:31

i want to pick just 2010-11-15 instead of 2010-11-15 04:28:31 ,

using MYSQL query ,

SELECT SUBSTR(my_date,0,6) FROM TBL ,

This query retrieving just empty value ,

How to form substr function in mysql QUERY ,

1 Answer 1

7

MySQL uses 1-based strings, so the first character is 1, while in php the first character is 0.

Better is to use the DATE function to get the date-part only from a date time field. That is, if your field is indeed a date time field, not a (var)char field.

If you want to display your date in a specific format you can also use

DATE_FORMAT(my_date, '%Y-%m-%d') 
Sign up to request clarification or add additional context in comments.

2 Comments

Slightly confusing to say "assumes the first character is 1". What you mean is that MySQL uses 1-based indexing, not 0-based indexing that is typically found in C-like languages.
You're right, I had trouble finding the correct word. I edited the answer. Thanks!

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.