Tuesday, September 11, 2012

SSIS: Convert Gregorian Dates to Hijri Dates

Conversion of Gregorian Date to Hijri Date using SSIS Script Component:

Gregorian Dates can be converted to Hijri dates using SQL Script components.

Create the Output column needed in the script component.
After that add following Code to the Script Editor.

 Public string ConvertDateCalendar(DateTime DateConv, string Calendar, string DateLangCulture)
    {
        DateTimeFormatInfo DTFormat;
        DateLangCulture = DateLangCulture.ToLower();
        /// We can't have the hijri date writen in English. We will get a runtime error - LAITH - 11/13/2005 1:01:45 PM -

        if (Calendar == "Hijri" && DateLangCulture.StartsWith("en-"))
        {
            DateLangCulture = "ar-sa";
        }

        /// Set the date time format to the given culture - LAITH - 11/13/2005 1:04:22 PM -
        DTFormat = new System.Globalization.CultureInfo(DateLangCulture, false).DateTimeFormat;

        /// Set the calendar property of the date time format to the given calendar - LAITH - 11/13/2005 1:04:52 PM -
        switch (Calendar)
        {
            case "Hijri":
                DTFormat.Calendar = new System.Globalization.HijriCalendar();
                break;

            case "Gregorian":
                DTFormat.Calendar = new System.Globalization.GregorianCalendar();
                break;

            default:
                return "";
        }

        /// We format the date structure to whatever we want - LAITH - 11/13/2005 1:05:39 PM -
        DTFormat.ShortDatePattern = "dd/MM/yyyy";
        return (DateConv.Date.ToString("f", DTFormat));
    }

Modify the following function to call date converter function.

Here you go. By using above method you have converted Gregorian dates to hijri dates in SSIS.


No comments:

Post a Comment