Nikolai Engel Nikolai Engel - 6 months ago 12
SQL Question

SQL Convert Date from dd.mm.yyyy to dd-mm-yyyy

I want to select data which is set in a specific date range. Unfortunately I get the date in this form:

01.05.2016 and 02.06.2016


In the database, the date are in the form:

2013-06-21


How can I convert the date in my sql query?

SELECT `artikel`.*
FROM `artikel`
WHERE (buchungsdatum >= '01.05.2016') AND (buchungsdatum <= '02.06.2016')

Answer

If this is MySQL, you can use STR_TO_DATE:

SELECT `artikel`.* 
FROM `artikel` 
WHERE 
    buchungsdatum >= STR_TO_DATE('01.05.2016','%d.%m.%Y')
    AND buchungsdatum <= STR_TO_DATE('02.06.2016', '%d.%m.%Y')

Check here for the available date formats.