Selasa, 30 Juni 2015

Menghitung selisih tahun dan Bulan (Mysql Function)



Berikut ini adalah kodenya:
DELIMITER $$

CREATE DEFINER=`itk`@`localhost` FUNCTION `menghitunghari`(awal datetime,akhir datetime) RETURNS char(30) CHARSET latin1
BEGIN
declare tahun int;
declare bulan int;
declare hasil char(100);

set tahun=year(akhir)-year(awal);
set bulan=month(akhir)-month(awal);

if sign(bulan) <=0 then
set bulan=12 - abs(bulan);
set tahun=tahun - 1;
end if;

 set hasil =concat(convert(tahun,char(30)),'Tahun','Bulan',convert(bulan,char(30)));


RETURN hasil;
END
---------------------------------------------------------------------------------------------------------

Kemudian untuk menyeleknya :
select menghitunghari('2015-02-12','2015-10-12') as 'Hitung seleisih tanggal'

Tidak ada komentar:

Posting Komentar

how coffee can prevent cancer

Coffee and Cancer: An Examination of the Relationship   Coffee, one of the most consumed beverages worldwide, has been the subject of extens...