I am working with an Excel spreadsheet that contains dates in the German format DD.MM.YYYY, and I’m trying to craft a formula that automatically generates an SQL insert statement for each row. I’m running into issues converting the German date to the SQL-expected format YYYY-MM-DD.
Here’s the formula I’m using:
="INSERT INTO Bestellungen_1NF (BestellNr, Bestelldatum, KundenNr, KundenVorname, KundenNachname, Bestellposition, ArtikelNr, Preis, Menge) VALUES (" & A2 & "; '" & TEXT(B2;"yyyy-mm-dd") & "'; " & C2 & "; '" & D2 & "'; '" & E2 & "'; " & F2 & "; " & G2 & "; " & H2 & "; " & I2 & ");"
The issue is that the date is output as yyyy-00-dd instead of the actual date 2023-04-05. Here is an example of the generated SQL statement that isn’t correct:
INSERT INTO Bestellungen_1NF (BestellNr, Bestelldatum, KundenNr, KundenVorname, KundenNachname, Bestellposition, ArtikelNr, Preis, Menge) VALUES (1; 'yyyy-00-dd'; 00520; 'Lisa'; 'Grub'; 1; 55220; 100; 3);
However, I need the following correct SQL statement:
INSERT INTO Bestellungen_1NF (BestellNr, Bestelldatum, KundenNr, KundenVorname, KundenNachname, Bestellposition, ArtikelNr, Preis, Menge) VALUES (1; '2023-04-05'; 00520; 'Lisa'; 'Grub'; 1; 55220; 100; 3);
How can I adjust the Excel formula to get the correct date in SQL format?
2
Answers
Try this once-
The MID function is used to extract the year, month, and day components from the German date in cell B2 and rearrange them in the SQL format.
You are on the right way.You can figure out the built in Excel function TEXT to do this job.
But please aware: This function use your locale settings. I assume, you are on a german based Excel. If so, you can convert the date as follows:
German date (Example):
Assumed, the date is in Column A1, B1 […] you can use the following formula to convert the date to MySQL Format:
The format string means for German locale(!):
Excel-Text function — German locale
Excel-Text function — US locale
Your code works like a charm, after a small modification, as described above.
One of the oldest excel annoyingness. The locale settings a relevant in native code. I think, many years ago, (source)code must also be translated, to break new ground.