My Blog List

LOVE IS LIFE

#htmlcaption1 #htmlcaption2 Stay Connected

Pages

Wednesday, June 17, 2015

Convert all row data in single column in EXCEL

Convert all row data in single column in EXCEL


Case:- We have data in excel as shown below arranged in rows and columns and we want to have all the row and column data into a single column.

















Step 1: - Select the data range from A1 to C4 and rename it as MyData as shown in figure.

Step 2 : - Go to any other cell as for example "E1", and then enter the below excel formula and then drag the formula below to the other cells to get all the data items.

=INDEX(MyData,1+INT((ROW(A1)-1)/COLUMNS(MyData)),MOD(ROW(A1)-1+COLUMNS(MyData),COLUMNS(MyData))+1)

We will be getting the desired result that is all data in a single column as shown in the image below.




Thursday, July 10, 2014

SSIS- script task to export data from database table to .xls format with each data separated by column in tab delimited form

SSIS- script task in C# to export data from database table to .xls format with each data separated by column in tab delimited form


Recently I faced a challenge while writing a script task in SSIS (C#) exporting data from database table to ".xls" format where in each data of a row should be under different column in EXCEL.

While I tried with the solution to export data from database table to ".csv" form using my earlier post as given in the link below, but I got stuck after the data got exported. I found the whole data of a row under one column separated by (","). The link below gives you C# code to export data from database table ".csv" format comma separated.

http://neil037.blogspot.in/2013/07/ssis-script-task-to-export-data-from.html


So after lot of analysis I found out the solution which I implemented below. The solution uses 
Microsoft.Office.Interop.Excel to render the data in separate column.

The code written by me is as below. Please let me know if anyone has any doubts on the same.


/*
   Microsoft SQL Server Integration Services Script Task
   Write scripts using Microsoft Visual C# 2008.
   The ScriptMain is the entry point class of the script.
*/

using System;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
using System.Web;
using System.Collections.Generic;
using v= Microsoft.Office.Interop.Excel;

//using System.Linq;
//using System.Web.Security;
//using System.Web.UI;
//using System.Web.UI.WebControls;
//using System.Web.UI.Adapters;
//using System.Web.UI.WebControls.WebParts;
//using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Text;
using System.IO;


namespace ST_f9e64226a54d4da28a70f42ab090a637.csproj
{
    [System.AddIn.AddIn("ScriptMain", Version = "1.0", Publisher = "", Description = "")]
    public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
    {
        string _FileName = "";
       // string unicode = "utf-8";
        #region VSTA generated code
        enum ScriptResults
        {
            Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
            Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
        };
        #endregion
       
        /*
            The execution engine calls this method when the task executes.
            To access the object model, use the Dts property. Connections, variables, events,
            and logging features are available as members of the Dts property as shown in the following examples.

            To reference a variable, call Dts.Variables["MyCaseSensitiveVariableName"].Value;
            To post a log entry, call Dts.Log("This is my log text", 999, null);
            To fire an event, call Dts.Events.FireInformation(99, "test", "hit the help message", "", 0, true);

            To use the connections collection use something like the following:
            ConnectionManager cm = Dts.Connections.Add("OLEDB");
            cm.ConnectionString = "Data Source=localhost;Initial Catalog=AdventureWorks;Provider=SQLNCLI10;Integrated Security=SSPI;Auto Translate=False;";

            Before returning from this method, set the value of Dts.TaskResult to indicate success or failure.
          
            To open Help, press F1.
      */

        public void Main()
        {
            // TODO: Add your code here
            string path = Dts.Variables["User::Input_File_Folder"].Value.ToString();
            String fname = Dts.Variables["User::Output_File_Folder"].Value.ToString() + "\\" + GetFileNames(path);
            Dts.TaskResult = (int)ScriptResults.Success;
            //GetFileNames(path);
            CreateCSVFile(GetTableData(), fname);
        }
        //public static Encoding GetEncoding(string unicode);

        public string GetFileNames(string path)
        {

            string tFileName = new DirectoryInfo(path).GetFiles("*.xls")[0].Name;

            //string[] files = Directory.GetFiles(path);
            //string tfileName=files[0].ToString(); //as assuming always one file
            string[] fileparts = tFileName.Split('.');
           


            _FileName = tFileName;
            return _FileName;
        }


        public System.Data.DataTable GetTableData()
        {

            String connstring = Dts.Variables["User::outputdatabaseConnectionString"].Value.ToString();

            SqlConnection conn = new SqlConnection(connstring);

            SqlCommand cmd = new SqlCommand("SELECT * from TABLE_NAME",conn);
            cmd.CommandType = CommandType.Text;
            SqlDataAdapter adap = new SqlDataAdapter(cmd);

            System.Data.DataTable dt = new System.Data.DataTable();
            adap.Fill(dt);
            return dt;
        }


        public void CreateCSVFile(System.Data.DataTable dt, string strFilePath)
        {
           
            v.Application tExcel = new v.Application();
           

            tExcel.Visible = true;

            //v.Workbook wk = (v.Workbook)(tExcel.Workbooks.Add(System.Reflection.Missing.Value));
            //v.Worksheet sheet = (v.Worksheet)wk.ActiveSheet;
            v.Workbook wk = tExcel.Workbooks.Add(v.XlWBATemplate.xlWBATWorksheet);
            v.Worksheet sheet = (v.Worksheet)wk.Worksheets[1];
            sheet.Name = "sans nom";

            if (sheet == null)
            {
                Console.WriteLine("Worksheet could not be created. Check that your office installation and project references are correct.");
            }

            int iColCount = dt.Columns.Count;
            for (int i = 0; i < iColCount; i++)
            {
                ((v.Range)sheet.Cells[1,i+1]).Value2 =dt.Columns[i].ToString();
            }
            // Now write all the rows.
            int j = 0;
            foreach (DataRow dr in dt.Rows)
            {
                for (int i = 0; i < iColCount; i++)
                {
                    ((v.Range)sheet.Cells[j+1,i+1]).Value2 = dr[i].ToString();
                }
                j++;
            }
            wk.SaveAs(strFilePath,
            v.XlFileFormat.xlXMLSpreadsheet, Type.Missing, Type.Missing,
                         Type.Missing, Type.Missing,
                          v.XlSaveAsAccessMode.xlNoChange,
                         Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
            wk.Close(true,Type.Missing,Type.Missing);
    }
}

}


Friday, June 27, 2014

SSIS - Script task export import variable

  public void Main()
        {
            // TODO: Add your code here

            string str = Dts.Variables["User::Internal_Sales_File_Name"].Value.ToString();

            str = str.Substring(str.Length - 9, 4);

            Dts.Variables["Internal_Sales_Year"].Value = str;

           // string test = Dts.Variables["User::Internal_Sales_Year"].Value.ToString();

       
            Dts.TaskResult = (int)ScriptResults.Success;
        }
    }

}

Wednesday, June 18, 2014

String matching with data in SQL server using COLLATE

String matching with data in SQL server using COLLATE


update CLEAN.Product_Ref set laboratories_Clean = UPPER(laboratories_Clean)
where LOCAL_PACK_ID in 
(select LOCAL_PACK_ID from CLEAN.Product_Ref
where laboratories_Clean != UPPER(laboratories_Clean) collate Latin1_General_CS_AI)                                                                                                                                              

HOW TO CHECK IF STRING CONTAINS LOWER CASE LETTER OR UPPER CASE LETTER USING T-SQL

HOW TO CHECK IF STRING CONTAINS LOWER CASE LETTER OR UPPER CASE LETTER USING T-SQL


Use the below SQL query:-

select distinct laboratories_Clean from CLEAN.Product_Ref
where laboratories_Clean != UPPER(laboratories_Clean) collate Latin1_General_CS_AI

Tuesday, June 17, 2014

Populating Slowly Changing Dimensions with SQL Server 2008 MERGE statement

Populating Slowly Changing Dimensions with SQL Server 2008 MERGE statement

MERGE statement

MERGE is a new feature in SQL Server 2008 that provides an efficient way to perform multiple DML operations. Using MERGE statement INSERT, UPDATE or DELETE can be performed on a target table based on the result of a join with a source table.
The MERGE statement has three WHEN clause, each performs a specific DML operation on a given row in the result set
WHEN MATCHED allows you to UPDATE or DELETE the given row in the target table for every row that exists in both source and target table
WHEN NOT MATCHED [BY TARGET] allows you to INSERT a row into the target table for every row that exists in the source table but not in the target
WHEN NOT MATCHED BY SOURCE allows you to UPDATE or DELETE the given row in the target table for every row that exists in target table but not in source table
You can also specify a search condition with each of the WHEN clause to choose the DML operation to be performed on the selected row
OUTPUT clause of the MERGE statement includes a virtual column $action which is used to identify the DML action performed in each row of the result set

Slowly Changing Dimensions

The data in the Slowly Changing Dimensions (SCD) changes periodically. The SCD may be a customer dimension, whereas any new customers can be added or the contact information of the existing customers may be changed.
Earlier this type of dimensions can be handled using multiple DML statements. With MERGE statement in SQL Server 2008 populating SCD can be achieved using a single statement.
Let us take DimCustomer as a dimension table which tracks the customer contact information (phone) for each customer of a particular retail store. Every day thecustomers table in the transactional system is updated with changes to the existing customer or inserting a new customer record. At the end of each month the changes in the customer table are to be applied in the DimCustomer table. A row has to be inserted for each new customer. Existing customers whose contact information changed need to be updated with IsCurrent as 0 and a new record for the existing customer has to be inserted with current contact information.
CustomerKey
CustomerName
Phone
IsCurrent
5011John Smith5650 578 632
1
5012Patrick Brown7451 241 584
1
Table 1: DimCustomer

CustomerKey
CustomerName
Phone
5011John Smith8845 001 863
5013Jimmy7554 846 563
Table 2: Customer
 The following Transact-SQL statement updates the Phone for the customer John Smith and adds a new record for Jimmy in the DimCustomer table.
 INSERT INTO DimCustomer(CustomerKey, CustomerName, Phone, IsCurrent)
  SELECT CustomerKey, CustomerName, Phone, 1
  FROM
    (
        MERGE DimCustomer as trgCustomerDim
        USING Customer AS CustSrc
        ON (trgCustomerDim. CustomerKey = CustSrc. CustomerKey and trgCustomerDim.IsCurrent = 1)
        WHEN MATCHED THEN
            UPDATE SET trgCustomerDim.IsCurrent = 0
        WHEN NOT MATCHED THEN
            INSERT VALUES (CustSrc. CustomerKey, CustSrc. CustomerName, CustSrc. Phone, 1)
        OUTPUT $action, CustSrc. CustomerKey, CustSrc. CustomerName, CustSrc. Phone
    ) AS Changes(action, CustomerKey, CustomerName, Phone)
  WHERE action = ‘UPDATE’;

The output of MERGE statement is
$action
CustomerKey
CustomerName
Phone
UPDATE5011John Smith8845 001 863
INSERT5013Jimmy7554 846 563
The OUTPUT of the MERGE statement is filtered with the action ‘Update’ to insert the new record for the changes in existing customer details. This will make the current information as available and also maintains the old information of the customer.
 The DimCustomer table after processing the MERGE statement would be like this
CustomerKey
CustomerName
Phone
IsCurrent
5011John Smith5650 578 632
0
5012Patrick Brown7451 241 584
1
5013Jimmy7554 846 563
1
5011John Smith8845 001 863
1

Thursday, June 5, 2014

SQL Server 2008 INDEX

SQL Server Indexing features - 

SQL server gave us the feature of indexing for speeding up the performance in how we retrieve data through queries. Imagine we have a book with many chapters, if we are required to turn pages and find a particular chapter of our interest it would take more time.

Indexing in a book gives a flexibility to look-up the page number of the book and directly go to that page number to find our chapter of interest and hence in a way more quick.

Similarly we have indexing in SQL server to retrieve data based a key value.


Types of indexes in SQL server
  • Clustered
  • Non-clustered
  • Unique
  • Filtered
  • XML
  • Full text
  • Spatial
  • Column store
  • Index with included columns
  • Index on computed columns
Today we will be discussing about Clustered, Non-Clustered and Unique indexes.

Clustered Index - 

It determines the way in which the data is physically ordered in a table. A table can have only one clustered index.

To understand this better let us create a table as shown below :-

Create table tblEmployee
(
[EmpID] int primary key,
[EmpName] varchar(20),
[Gender] char(1),
[EmpSalary] int
)

After creation of the table open "Object Explorer", expand tables in the database where the table tblEmployee was created expand "Key" and "Indexes", you can see the creation of primary key and an Index.

So we can now conclude for the above example that creation of primary key creates clustered index by default. Please see below screenshot for reference.









So whats actually the use of this clustered index creation!!!

As we stated above that indexing determines the way data is physically stored in a database, in the below example we will see how it stores the data.


In the creation of the table tblEmployee we have used EmpID as the primary key and the clustered index is created on this column. Lets try to insert values in the table tblEmployee.

Insert into tblEmployee values(3,'Indranil','M',25000)
Insert into tblEmployee values(1,'Vipul','M',35000)
Insert into tblEmployee values(5,'Deepika','F',18000)
Insert into tblEmployee values(4,'Mukul','M',19000)
Insert into tblEmployee values(2,'Shikha','F',21000)


We can see that while insertion of values in the table, the order of the EmpID is not inserted orderly, but as we have the clustered index on the EmpID listed, when we view the data through select statement, the data are automatically ordered in ascending order of EmpID. So the clustered index on EmpID helped the data get stored in accordance to ascending order of EmpID.

select * from tblEmployee