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.


Monday, September 10, 2012

Create DB Script and Restore DB using SqlCmd Utility and .sql file

How to Restore DB from Script:
If you have got db script with data in .sql file then no need to worry. You can run your SQL script with out opening it using SqlCmd utility as follows.


sqlcmd -S servername\instancename -i C:\abc.sql


How to create DB Script:
RightClick DB -->Generate Scripts



Here you go. This way you can create DB script with data included.