Wednesday 27 February 2019

MySql | Find Data Between Dates When Date Is Stored As Varchar | Str_to_date | Select Statement

Code:-

mysql> select * from employee where str_to_date(b_date,'%Y-%m-%d') between str_to_date('2019-2-26','%Y-%m-%d') and str_to_date('2019-2-30','%Y-%m-%d');
| id | name        | b_date    |
|  2 | tony stark  | 2019-2-26 |
|  4 | black widow | 2019-2-27 |
2 rows in set (0.00 sec)


mysql> desc employee;
| Field  | Type        | Null | Key | Default | Extra |
| id     | int(11)     | NO   | PRI | NULL    |       |
| name   | varchar(20) | YES  |     | NULL    |       |
| b_date | varchar(20) | YES  |     | NULL    |       |
3 rows in set (0.03 sec)


mysql> select * from employee;
| id | name         | b_date    |
|  1 | steve rogers | 2019-2-24 |
|  2 | tony stark   | 2019-2-26 |
|  3 | thor odinson | 2019-2-25 |
|  4 | black widow  | 2019-2-27 |

4 rows in set (0.06 sec)


Hello guys, this is unpossible pog and in this video, I shall teach you how to find data between two dates if they are stored as varchar as variable character.

First I shall start mysql by entering username and password.
Then I shall connect to the database, this is case, its sys.

Then as you can see the employee data with the birth dates.
But the b_date is stored as varchar.


So in mysql, there is a function called str_to_date() which converts text to date.
So type command like this in where condition, the first argument will be the column name, which is b_date. Then in second argument, there will be a pattern.
In my database, the pattern is the year – month – date. So I shall type percentage year – percentage month – percentage date.

Thank you for watching, Dont forget to like share and subscribe.






1 comment: