Showing posts with label datetime. Show all posts
Showing posts with label datetime. Show all posts

29 November 2006

Date Time Related Data Types And Functions In Oracle

Developers always use time related data types in their applications to audit information. Oracle give additional functionality supplied functions and data types. One of my previous posts i mentioned how to use interval data types. Now i want to give some examples how to use some of them.
First it is possible to convert interval data from string values:
SQL> SELECT to_yminterval('02-10') FROM dual;

TO_YMINTERVAL('02-10')
----------------------
+000000002-10

SQL> SELECT to_dsinterval('02 01:00:30') FROM dual;

TO_DSINTERVAL('0201:00:30')
---------------------------
+000000002 01:00:30


Second, i think more powerful, convert datetime strings from external programing languages such as C# or Java, Oracle timestamps and manipulate easiliy with them. Suppose you want to convert format of a datetime string. ( '23:12:54.899 +02:00 Thu Nov 29 2001' to '011129' )With oracle's supplied functions you are not need to make conversions such find-replace. Look at example below:

SQL> SELECT tz_offset('Asia/Istanbul') FROM dual;

TZ_OFFSET('ASIA/ISTANBUL')
--------------------------
+02:00

SQL> SELECT to_timestamp_tz('23:12:54.899 +02:00 Thu Nov 29 2001','HH24:MI:SS.FF3 TZH:TZM DY Mon DD YYYY') FROM dual;

TO_TIMESTAMP_TZ('23:12:54.899+'
-------------------------------------------------
29-NOV-01 11.12.54.899000000 PM +02:00

SQL> SELECT to_char( to_timestamp_tz('23:12:54.899 +02:00 Thu Nov 29 2001','HH24:MI:SS.FF3 TZH:TZM DY Mon DD YYYY'), 'YYMMDD') FROM dual;

TO_CHAR(TO_TIMESTAMP_TZ('23:12'
------------------------------
011129

23 September 2006

Using INTERVAL DAY TO SECOND Data Type In Oracle

Oracle has INTERVAL data types to store days or years. Sometimes it helps you to do less work. I demonstrate a simple example to show the usage.

Connected to Oracle Database 10g Express Edition Release 10.2.0.1.0
Connected as hr

SQL>
SQL> DECLARE
  2    diff INTERVAL DAY TO SECOND;
  3 
  4    d1 DATE := SYSDATE;
  5    d2 DATE := SYSDATE +
  6               (6 + 1 / 24 * 3 + 1 / 24 / 60 * 7 + 1 / 24 / 60 / 60 * 9);
  7 
  8    t1 TIMESTAMP;
  9    t2 TIMESTAMP;
 10  BEGIN
 11    t1 := to_timestamp(d1);
 12    t2 := to_timestamp(d2);
 13 
 14    diff := t2 - t1;
 15 
 16    dbms_output.put_line('Time 2 is ' || to_char(t2));
 17    dbms_output.put_line('Time 1 is ' || to_char(t1));
 18 
 19    dbms_output.put_line('Total time difference is ' || diff);
 20 
 21    dbms_output.put_line('Details:');
 22    dbms_output.put_line('Day is    ' || extract(DAY    FROM diff));
 23    dbms_output.put_line('Hour  is  ' || extract(HOUR   FROM diff));
 24    dbms_output.put_line('Minute is ' || extract(MINUTE FROM diff));
 25    dbms_output.put_line('Second is ' || extract(SECOND FROM diff));
 26  END;
 27  /

Time 2 is 29/09/2006 23:50:39,000000
Time 1 is 23/09/2006 20:43:30,000000
Total time difference is +06 03:07:09.000000
Details:
Day is    6
Hour  is  3
Minute is 7
Second is 9

PL/SQL procedure successfully completed
SQL>

13 September 2006

Oracle Date Formats RR and YY

calendar
RR ve YY format belirteçlerini kullanırken dikkat etmek gerekir. Örneğin 2006 yılında 7 yıl önceki bir değeri tarihe dönüştürmek istiyorsunuz. Bunu YY formatında yaparsanız 7 yıl önce yerine 93 yıl ileriye gidersiniz. Aynı yüzyılda olmadığınız anda(1999 ile 2006 aynı yüzyılda RR formatına göre; 2006 2099 aynı yüzyılda YY formatına göre)

Bunu aşağıdaki örnekte görebilirsiniz:



Connected to Oracle Database 10g Enterprise Edition Release 10.2.0.1.0
Connected as SYS


SQL>
SQL> select to_date('11.09.96','DD.MM.YY') date_ from dual;

DATE_
-----------
11.09.2096

SQL> select to_date('11.09.96','DD.MM.RR')date_ from dual;

DATE_
-----------
11.09.1996

SQL> select to_date('11.09.06','DD.MM.YY') date_ from dual;

DATE_
-----------
11.09.2006

SQL> select to_date('11.09.06','DD.MM.RR') date_ from dual;

DATE_
-----------
11.09.2006

SQL>

09 September 2006

How To Extract Date Values In Oracle

Kimi zaman uygulamalar içinde tarih değerleri arasındaki farkları alıp kullanabilirsiniz. Oracle'da iki tane date tipi ile normal sayısal değerler arasında fark almak gibi işlem yapılır. Sonuç gün cinsinden numerik olarak döner. İstediğiniz formata dönüştürebilirsiniz. Şöyleki;
DECLARE
start_date DATE;
end_date DATE;
time_difference NUMBER;
time_difference_in_seconds NUMBER;
time_difference_in_minutes NUMBER;

BEGIN
start_date := to_date('09.09.2006 09:57:49',
'DD.MM.YYYY HH24:MI:SS');
end_date := to_date('09.09.2006 09:59:34',
'DD.MM.YYYY HH24:MI:SS');
time_difference := end_date - start_date;

time_difference_in_seconds := time_difference * 86400; --24*60*60
time_difference_in_seconds := trunc(time_difference_in_seconds, 4);

time_difference_in_minutes := time_difference * 1440; --24*60
time_difference_in_minutes := trunc(time_difference_in_minutes, 4);

dbms_output.put_line('Start Time : ' ||
to_char(start_date, 'DD.MM.YYYY HH24:MI:SS'));
dbms_output.put_line('End Time : ' ||
to_char(end_date, 'DD.MM.YYYY HH24:MI:SS'));
dbms_output.put_line('Difference in seconds :' ||
time_difference_in_seconds);
dbms_output.put_line('Difference in minutes :' ||
time_difference_in_minutes);
END;

Çıktı ise
Start Time : 09.09.2006 09:57:49
End Time : 09.09.2006 09:59:34
Difference in seconds :105
Difference in minutes :1,75

Fark alma işleminin birçok yolu bulunmaktadır. Ben sadece birini aktardım.