0

Hello I'm trying to delete all data ranging from 04/15/2014 to 03/16/2016. My problem is that the data type for the dates is varchar. The format is m/d/Y. My current sql code is here

DELETE FROM ending_inventory WHERE STR_TO_DATE(dater, '%m/%d/%Y')  BETWEEN '04/15/2014' AND '03/16/2016'

and it's not doing anything.

Is there a way to do this without changing the data type to DATE? Because I already have tons of data..

Pardeep Dhingra
  • 3,916
  • 7
  • 30
  • 56
wobsoriano
  • 12,348
  • 24
  • 92
  • 162

3 Answers3

1

You are comparing a date to strings. You need to generate dates from the two strings you're trying to compare between. See Jon Skeets answer here: mysql date comparison with date_format

Community
  • 1
  • 1
1

you can try this. STR_TO_DATE FUNCTION change character into date and its default format is 'YYYY-mm-dd'

 DELETE FROM ending_inventory
 WHERE STR_TO_DATE(dater, '%m/%d/%Y') 
 BETWEEN '2014-04-15' AND '2016-03-16';

DATE FUNCTIONS Hope this will work for you.

Vipin Jain
  • 3,686
  • 16
  • 35
1

Try this to parse your date:

select date_format(str_to_date('12/31/2011', '%m/%d/%Y'), '%Y%m');