Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

German Daynames with DATE_FORMAT() 1

Status
Not open for further replies.

m4uConcept

Programmer
Jul 22, 2002
4
0
0
AT
Is it possible to to get a german weekname from an DATE_FORMAT()

e.g. Date_FORMAT(datetime, '%a %d%m%Y');
schould end in Fre 25-05-2003 or Freitag 25-05-2003
instead of Fri 25-05-2003 or Friday 25-05-2003

Hope someone could answer my question
Roland


 
Hi Roland,

da du deutsche Wochentage brauchst, schreib ich dir auf deutsch.

Ich hab auch keinen Sprachsupport gefunden, also ist es nötig, eine kleine Umsetztabelle einzuführen. Das sieht auf den ersten Blick komplex aus, aber es funktioniert. Ich würde Dir empfehlen, den Select, wie ich ihn entworfen habe, in einen eigenen Zugriff zu schreiben, den du dann immer wieder aufrufen kannst.

Also, die Sache funktioniert so: Mit DayOfWeek bekommst du immer die laufende Nummer eines Wochentags, beginnend bei Sonntag.

Sonntag=1
Montag=2
Dienstag=3
usw.

Hier der Select(nicht erschrecken, ist gar nicht so wild wies zuerst aussieht):


SELECT
date_format('2003-04-24',
concat(
rtrim(
mid(
'Sonntag Montag Dienstag Mittwoch DonnerstagFreitag Samstag ',
((dayofweek('2003-04-24' )- 1)*10)+ 1,10)),
', %d-%m-%Y' )
)

Also: Der Dayofweek liefert dir eine Zahl zwischen eins und sieben. Mit dieser Zahl kann ich den Offset berechnen, ab wann ich meinen Tagnamenstring lesen muss. Also: Sonntag=1, also muss ich von der Position ((1-1=0)*10)+1 lesen, weil: Der längste Tagname ist Donnerstag mit 10 Zeichen, also müssen jetzt alle Tagnamen 10 Zeichen im String haben(achte auf die notwendigen Leerzeichen). Für Montag ergibt sich dann 2-1=(1*10)+1=11 als Startposition usf. Wenn ich in der Länge 10 lese, kann ich immer den vollen Tagnamen erhalten, lese ich nur in der Länge 2 oder 3, er halte ich Mo, Di Mi bzw. Mon, Die, Mit etc. Mit dem letzten Parameter der Mid-Funktion steuerst du also die Länge der Ausgabe (der gefettete Parameter). Rtrim schneidet nur die überflüssigen Blanks ab.

Lästig ist nur, daß ich das Datum zweimal an die Funktion übergeben muß, aber dafür hast du zwei Vorteile: Das ganze ist in eine wiederverwendbare Funktion gekapselt, und es ist ein reiner SQL-Aufruf ohne DB-Zugriff, d.h., es ist kein weiterer php- oder sonstwelcher Code zu pflegen und die Datenbank wird nicht belastet.

Das Ganze kann man dann natürlich auch auf Monate erweitern....


Thomas
 
here is a quick translation --

___________________________________


I also found no language support, therefore it is necessary to introduce a small transformation table. It looks complex at first sight, but it functions. I would recommend that you write the SELECT as I have written it, in one statement (module?), which you can then call again and again.

Okay, the query works like this:

With DayOfWeek you always get the serial number of a day of the week, beginning with Sunday.

Sonntag=1 (Sunday)
Montag=2 (Monday)
Dienstag=3 (Tuesday)
and so on

Here is the SELECT (do not be frightened, is not at all as wild as it first appears):

SELECT
date_format('2003-04-24',
concat(
rtrim(
mid(
'Sonntag Montag Dienstag Mittwoch DonnerstagFreitag Samstag ',
((dayofweek('2003-04-24' )- 1)*10)+ 1,10)),
', %d-%m-%Y' )
)


So DAYOFWEEK supplies a number between one and seven. With this number I can compute the offset, from which I can read the DayName string. Thus, Sonntag(Sunday)=1, therefore I must read from position ((1-1=0)*10)+1. Since the longest day name is Donnerstag (Thursday) with 10 characters, therefore all day names must be 10 characters long in the string (note the necessary number of spaces). For Montag (Monday), 2-1=(1*10)+1=11 is the initial position, and so on.

If I read a substring of length 10, I always receive the full day names, and if I read in only a length 2 or 3, I get Mo, Di, Mi... or Mon, Die, Mit... With the last parameter of the MID function you thus define the length of the substring. RTRIM cuts off only the excess spaces.

The only annoyance is that I must code the date twice in the function, but this gives you two advantages: everything is totally enclosed in a re-usable function, and it is a pure SQL call without database access, i.e. no further php or other code needs to be maintained, and the load on the database is avoided.

One can naturally extend the technique also to months....

___________________________________



rudy
 
Dear Rudy,

thank you very much for translating my posting in english so accurately and so fast.

I'm certainly aware that english is the "lingua franca" of IT, but I was in a little hurry and so I wrote it all in German.

Please, for this time, excuse my misbehaving, I won't do it again...

bye
Thomas
 
you are welcome

es war gut, meine muttersprache wieder zu benuetzen

(it was good to use my mother tongue again)

:)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top