dimanche 28 juin 2015

Convert SQL FUNCTION to MySQL

I need to convert the following Function of SQL Server To MySQL. I am new to MySQL. Help needed. the Function returns a generated Id based on the date : -S1 or S2 ..S5 : the letter S followed by the number of week on month start from monday -0X the month in two digits -15 the year in two digits

Ex :

2015/06/01 to 2015/06/07 is S10615
2015/06/29 to 2015/07/05 is S50615
2015/07/06 to 2015/07/12 is S10715

Function

Create Function GetIdPanier()
Returns Varchar(25)
As
Begin

      declare @week varchar(2) = DATEPART(DAY, DATEDIFF(DAY, 0, GETDATE())/7 * 7)/7 + 1
      declare @month int = case when DATEPART(DAY,GETDATE()) < 7 and @week > 1
                            then DATEPART(MONTH,DATEADD(MONTH,-1,GETDATE()))
                            else DATEPART(MONTH,GETDATE())
                       end
   return  'S' + @week + RIGHT('00' + CAST(@month AS varchar(2)), 2) + RIGHT(CAST(DATEPART(yyyy, GETDATE()) AS varchar(4)), 2)
End

Aucun commentaire:

Enregistrer un commentaire