[Database][Mysql/SQL Server/Oracle] Output Query format Date


Deskripsi
      Berikut adalah cara dalam mendapatkan format output date dari 3 database yakni mysql , oracle , dan Sqlserver, menyebalkan memang jika berhubungan dengan date pada saat query ~_~!.

Howto

Fundamental Datetime
     Pada dasarnya penyimpanan format date pada database normalnya memiliki format :
 - 'YYYY/MM/DD HH:MM:SS AM/PM' => Oracle
-  'MM/DD/YYYY HH:MM:SS AM/PM' => SQL Server
-  'YYYY/MM/DD HH:MM:SS'              => Mysql

nah bagaimana cara narik datanya berdasarkan date ?? berikut adalah contoh query dari masing-masing database.

1. Oracle
    to_date ('10/05/2013','dd/mm/yyyy');
 
example : select *
               from table
              where creation_date > to_date ('10/05/2013','dd/mm/yyyy');


2. SQL Server
   CONVERT(type,field or date,code_format)

here's the code_format (source from w3schools)
Value
(century yy)
Value
(century yyyy)
Input/OutputStandard
-0 or 100mon dd yyyy hh:miAM (or PM)Default
1101mm/dd/yyUSA
2102yy.mm.ddANSI
3103dd/mm/yyBritish/French
4104dd.mm.yyGerman
5105dd-mm-yyItalian
6106dd mon yy
7107Mon dd, yy
8108hh:mm:ss
-9 or 109mon dd yyyy hh:mi:ss:mmmAM (or PM)Default+millisec
10110mm-dd-yyUSA
11111yy/mm/ddJapan
12112yymmddISO
-13 or 113dd mon yyyy hh:mi:ss:mmm (24h)
14114hh:mi:ss:mmm (24h)
-20 or 120yyyy-mm-dd hh:mi:ss (24h)
-21 or 121yyyy-mm-dd hh:mi:ss.mmm (24h)
-126yyyy-mm-ddThh:mi:ss.mmm (no spaces)ISO8601
-130dd mon yyyy hh:mi:ss:mmmAMHijiri
-131dd/mm/yy hh:mi:ss:mmmAMHijiri


Example:
             select  CONVERT(VARCHAR,create_date,103) as create_date
    from MS_CLIENT_H
             where create_date > '2013-02-01 14:26:40'

3. Mysql
    Date_Format(date,format)
 
here's the code_format (source from w3schools)
FormatDescription
%aAbbreviated weekday name
%bAbbreviated month name
%cMonth, numeric
%DDay of month with English suffix
%dDay of month, numeric (00-31)
%eDay of month, numeric (0-31)
%fMicroseconds
%HHour (00-23)
%hHour (01-12)
%IHour (01-12)
%iMinutes, numeric (00-59)
%jDay of year (001-366)
%kHour (0-23)
%lHour (1-12)
%MMonth name
%mMonth, numeric (00-12)
%pAM or PM
%rTime, 12-hour (hh:mm:ss AM or PM)
%SSeconds (00-59)
%sSeconds (00-59)
%TTime, 24-hour (hh:mm:ss)
%UWeek (00-53) where Sunday is the first day of week
%uWeek (00-53) where Monday is the first day of week
%VWeek (01-53) where Sunday is the first day of week, used with %X
%vWeek (01-53) where Monday is the first day of week, used with %x
%WWeekday name
%wDay of the week (0=Sunday, 6=Saturday)
%XYear of the week where Sunday is the first day of week, four digits, used with %V
%xYear of the week where Monday is the first day of week, four digits, used with %v
%YYear, four digits
%yYear, two digits



Example :
     SELECT datecolumn
     FROM `user`
     WHERE datecolumn = date_format('23-05-2013','%d-%m-%y')



Hope this helps :D

Post a Comment

Harap gunakan bahasa yang baik dan sopan, terima kasih