Datum Und Uhrzeit

tobiastt

Erfahrenes Mitglied
Hallo

wie trage ein ein Datum + Uhrzeit in meine Datenbank ein.

Ich arbeite mit Oracle 8.

So gehts leider nicht:

create table t(
a date,
b number(10));

insert into t values('2000-11-28 16:59:57',1);

Danke

Gruß Tobi :suspekt:
 
Ich weiß nicht ob das in Oracle auch geht aber in MySQL würde ich es mit mit dem Datentyp varchar(10) versuchen. Das Problem ist das bei dir ja nicht nur Zahlen sonden auch die Trennzeichen drinnenstehen.
 
Du musst die TO_DATE Funktion nutzen. Sie erwartet ein Textfeld und einen Formatstring.

Code:
INSERT INTO t VALUES ( TO_DATE('2000-11-28 16:59:57', 'YYYY-MM-DD HH24:MI:SS'), 1);

Somit kannst du jedes beliebige Datumsformat angeben. Ein Feld DATE hat in Oracle auch IMMER die Zeit mit dabei.
 
ja das eintragen funktioniert aber bei der Ausgabe z.B mit select * from t bekomme ich nur das Datum ohne Zeit zurück?
 
Das ist nur eine Formatierung der Anzeige. Oracle liefert das Datum gemäß der aktuellen Session Einstellungen zurück. Diese kannst du mittels folgendem Befehl beliebig editieren:

Code:
ALTER SESSION SET NLS_DATE_FORMAT = 'DD.MM.YYYY HH24:MI:SS';
 
ich verwende PHP um die Daten aus der DB auszulesen dort wird mir nur das Datum angezeigt ohne Uhrzeit

Wenn ich die Datenbank verlasse muss ich wieder

ALTER SESSION SET NLS_DATE_FORMAT = 'DD.MM.YYYY HH24:MI:SS';

eingeben, um mir die Uhrzeit anzeigen zu lassen

Warum das?
 
Wie der Befehl schon sagt, gilt die Änderung nur für die aktuelle Session. D.h. wenn die Verbindung beendet wird, endet auch die Session.

Es gibt mehrere Möglichkeiten die NLS Settings zu editieren:

- As initialization parameters on the server
You can include parameters in the initialization parameter file to specify a default session NLS environment. These settings have no effect on the client side; they control only the server's behavior. For example:
Code:
NLS_TERRITORY = "CZECH REPUBLIC"

- As environment variables on the client
You can use NLS environment variables, which may be platform-dependent, to specify locale-dependent behavior for the client and also to override the default values set for the session in the initialization parameter file. For example, on a UNIX system:
Code:
% setenv NLS_SORT FRENCH

- With the ALTER SESSION statement
NLS parameters that are set in an ALTER SESSION statement can be used to override the default values that are set for the session in the initialization parameter file or set by the client with environment variables.
Code:
ALTER SESSION SET NLS_SORT = FRENCH;

- In SQL functions
NLS parameters can be used explicitly to hardcode NLS behavior within a SQL function. This practice overrides the default values that are set for the session in the initialization parameter file, set for the client with environment variables, or set for the session by the ALTER SESSION statement. For example:
Code:
TO_CHAR(hiredate, 'DD/MON/YYYY', 'nls_date_language = FRENCH')
 
aha und wie müsste ich das machen, dass nicht nur die Session davon betroffen ist

in UNIX z.B so oder

% setenv NLS_DATE_FORMAT = 'DD.MM.YYYY HH24:MI:SS';


komischerweise wird mir in der Datenbank das vollst,. Datum + Uhrzeit angezeigt bei einen Select * from t;


und wenn ich das gleiche in PHP übergeben wird mir das Datum ohne Zeit angezeigt?

Gruß Tobi
 
Zurück