3

i have a data with all column types as text even date also stored in text type column. Now when i am using select query to access the column its working find but when i am trying to perform any date operation on that column it always return null.

Table:

enter image description here

if am running query select column_14 from mytable where id = 2 its working fine but when i am trying to find the difference between today date and column_14 its always return null

Query Running:

select DATEDIFF(NOW(),STR_TO_DATE(column_14,"%y-%m-%d")) from `mytable` where id = 2

enter image description here

can anyone please tell me what is wrong in my query ?

Structure View

enter image description here

5
  • Use this, DATEDIFF(NOW(),DATE(column_14)) Commented Apr 25, 2017 at 12:27
  • same result..... Commented Apr 25, 2017 at 12:30
  • your date column have y-m-d format, so try once like, DATEDIFF(NOW(),column_14) without DATE(). Commented Apr 25, 2017 at 12:33
  • is there any effect if my table have Engine=MyISAM? Commented Apr 25, 2017 at 12:35
  • i got the problem..there is mistake in my data. I just export a single row and its have \0 with date and after REPLACE that i am able to perform date operations..thanks Commented Apr 26, 2017 at 7:23

2 Answers 2

2

It should be %Y instead of %y. %Y takes 4 digit year.

DATEDIFF(NOW(),STR_TO_DATE(column_14,"%Y-%m-%d"))

Sign up to request clarification or add additional context in comments.

7 Comments

i agree with you but it always returns null
even if i am using simple date() it returns null
its just a header
Can you update the question to show the whole row including id = 2.
A simple select DATEDIFF(NOW(),STR_TO_DATE('2010-10-11','%Y-%m-%d')); gives proper date difference, must be something to do with the table or table name. Rest all seems to be fine.
|
1

Str_to_date will return null values if you pick the wrong format or the function detects an out of range month or day number. In your example you have picked a wrong format %y should be %Y. Here are some samples of what can happen. (nb: str_to_date is poor at this and doesn't check how many days are valid for a month)

MariaDB [sandbox]> create table t (dt text);
Query OK, 0 rows affected (0.28 sec)

MariaDB [sandbox]> insert into t values ('2017-13-01'),('2017-04-31'),('2017-04-33'),('2017-04-03'),('birth_date');
Query OK, 5 rows affected (0.02 sec)
Records: 5  Duplicates: 0  Warnings: 0

MariaDB [sandbox]>
MariaDB [sandbox]> select str_to_date(dt,'%y-%m-%d') Invalid_year_format,
    -> str_to_date(dt,'%Y-%m-%d') Valid_year_format_MM_and_DD,
    -> datediff(now(),str_to_date(dt,'%Y-%m-%d')) datediff_examples
    -> from t;
+---------------------+-----------------------------+-------------------+
| Invalid_year_format | Valid_year_format_MM_and_DD | datediff_examples |
+---------------------+-----------------------------+-------------------+
| NULL                | NULL                        |              NULL |
| NULL                | 2017-04-31                  |                -6 |
| NULL                | NULL                        |              NULL |
| NULL                | 2017-04-03                  |                22 |
| NULL                | NULL                        |              NULL |
+---------------------+-----------------------------+-------------------+
5 rows in set, 11 warnings (0.00 sec)

3 Comments

getting still same output...even after correct %Y-%m-%d this
There is no difference if the table is myisam or innodb. As I said problem is with data not function.
i got the problem..there is mistake in my data. I just export a single row and its have \0 with date and after REPLACE that i am able to perform date operations..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.