Requête 1 : Fonction de conversion qui retourne une chaîne de caractères contenant la date selon le format « dd/mm/yyyy »;
CREATE FUNCTION [dbo].[fctDateToStr] (@Date DateTime) Returns Char(10) AS Begin RETURN Convert(Char(10), @Date,103) End
Requête 2 : Fonction de conversion qui retourne une chaîne de caractères contenant l’heure selon le format « hh:mm:ss »;
CREATE FUNCTION [dbo].[fctTimeToStr] (@Date DateTime) Returns Char(8) AS Begin RETURN Convert(Char(8), @Date,108) End
Requête 3 : Fonction de conversion qui retourne une chaîne de caractère contenant la date et l’heure selon le format « dd/mm/yyyy hh:mm:ss ».
CREATE FUNCTION [dbo].[fctDateTimeToStr] (@Date DateTime) Returns Char(19) AS Begin RETURN Convert(Char(10), @Date,103) + ' ' + Convert(Char(8), @Date,108) End
Requête 4 : Fonction de conversion qui retourne une chaîne de caractère au format indiqué.
CREATE FUNCTION [dbo].[fctFormatDateTime] (@Format AS Varchar(19), @Date DateTime)
Returns Varchar(19) AS
Begin
RETURN Case @Format
When 'dd/mm/yyyy' Then Convert(Char(10), @Date,103)
When 'yyyy-mm-dd' Then Convert(Char(10), @Date, 120)
When 'yyyy-mm' Then Convert(Char(7), @Date, 120)
When 'dd/mm/yyyy hh:mm:ss' Then Convert(Char(10), @Date,103) + ' ' + Convert(Char(8), @Date,108)
When 'yyyy-mm-dd hh:mm:ss' Then Convert(Char(19), @Date, 120)
When 'dd mmm yyyy' Then Case Month(@Date)
When 4 Then Cast(Day(@Date) AS Varchar(2)) + ' ' + LEFT(DateName(Month, @Date), 3) + ' ' + Cast(Year(@Date) AS Char(4))
When 5 Then Cast(Day(@Date) AS Varchar(2)) + ' ' + LEFT(DateName(Month, @Date), 3) + ' ' + Cast(Year(@Date) AS Char(4))
When 10 Then Cast(Day(@Date) AS Varchar(2)) + ' ' + LEFT(DateName(Month, @Date), 3) + ' ' + Cast(Year(@Date) AS Char(4))
When 11 Then Cast(Day(@Date) AS Varchar(2)) + ' ' + LEFT(DateName(Month, @Date), 3) + ' ' + Cast(Year(@Date) AS Char(4))
When 12 Then Cast(Day(@Date) AS Varchar(2)) + ' ' + LEFT(DateName(Month, @Date), 3) + ' ' + Cast(Year(@Date) AS Char(4))
Else Cast(Day(@Date) AS Varchar(2)) + ' ' + LEFT(DateName(Month, @Date), 4) + ' ' + Cast(Year(@Date) AS Char(4))
End
When 'dd mmmm yyyy' Then Cast(Day(@Date) AS Varchar(2)) + ' ' + DateName(Month, @Date) + ' ' + Cast(Year(@Date) AS Char(4))
When 'mmm' Then Case Month(@Date)
When 4 Then LEFT(DateName(Month, @Date), 3)
When 5 Then LEFT(DateName(Month, @Date), 3)
When 10 Then LEFT(DateName(Month, @Date), 3)
When 11 Then LEFT(DateName(Month, @Date), 3)
When 12 Then LEFT(DateName(Month, @Date), 3)
Else LEFT(DateName(Month, @Date), 4)
End
When 'hh:mm:ss' Then Convert(Char(8), @Date,108)
End
EndRequête 5 : Fonction qui retourne l’information de date en attribuant la valeur 0 (minuit) à la partie heure.
CREATE FUNCTION [dbo].[fctDateOf] (@Dte DateTime) Returns Datetime Begin RETURN Convert(DateTime, Floor(Convert(Float, @Dte))); end
Requête 6 : Fonction qui retourne l’information d’heure en attribuant la valeur 0 (01/01/1900) à la partie date.
CREATE FUNCTION [dbo].[fctTimeOf] (@Dte DateTime) Returns DateTime Begin RETURN Convert(DateTime, Convert(BINARY(4),@Dte)); End
Requête 7 : Fonction qui retourne le trimestre d’une date
CREATE FUNCTION [dbo].[fctQuarterOf] (@Dte DateTime) Returns Tinyint Begin RETURN DatePart(Quarter, @Dte); End
Requête 8 : Fonction qui retourne le semestre d’une date
CREATE FUNCTION [dbo].[fctSemesterOf] (@Dte DateTime) Returns Tinyint Begin RETURN Ceiling(Month(@Dte)/6.0); End