Monday, July 27, 2020

delete duplicates using CTE

WITH CTE([FirstName],
    [LastName],
    [Country],
    DuplicateCount)
AS (SELECT [FirstName],
           [LastName],
           [Country],
           ROW_NUMBER() OVER(PARTITION BY [FirstName],
                                          [LastName],
                                          [Country]
           ORDER BY ID) AS DuplicateCount
    FROM [SampleDB].[dbo].[Employee])
DELETE FROM CTE
WHERE DuplicateCount > 1;

It removes the rows having the value of [DuplicateCount] greater than 1

https://www.sqlshack.com/different-ways-to-sql-delete-duplicate-rows-from-a-sql-table/

Friday, March 20, 2020

Rename Column name sql

Method 1:

Syntax:
ALTER TABLE TableName
CHANGE COLUMN OldColumnName NewColumnName Data Type;

Example
ALTER TABLE Test;
CHANGE COLUMN TestingID TestID INT;



Method 2:

Use sp_rename
Syntax :


EXEC sp_RENAME 'TableName.OldColumnName' , 'NewColumnName', 'COLUMN'

Thursday, March 5, 2020

find if table exists

SELECT *
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'mONTHLY'
AND TABLE_NAME ='table'

check if stored proc is exists
IF EXISTS (SELECT * FROM dbo.sysobjects 
WHERE id = object_id(N'[dbo].[your_procedure_name]') 
and OBJECTPROPERTY(id, N'IsProcedure') = 1)

Thursday, February 20, 2020

SSIS 2012 Oledb script component

 
#region Namespaces
using System;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using Microsoft.SqlServer.Dts.Runtime.Wrapper;
using System.Data.OleDb;
using System.Windows.Forms;
#endregion

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


            //Declaring the variables 
            string oleDBConnString = string.Empty;
            IDTSConnectionManagerDatabaseParameters100 connMrgParameters = default(IDTSConnectionManagerDatabaseParameters100);
            System.Data.OleDb.OleDbConnection oledbConn = null;
            ConnectionManager connMgr = Dts.Connections["LocalDB"];
            //Reading the Connection manager Properties and assigning to variables
            connMrgParameters = (IDTSConnectionManagerDatabaseParameters100)connMgr.InnerObject;
            oledbConn = connMrgParameters.GetConnectionForSchema() as OleDbConnection;
            oleDBConnString = oledbConn.ConnectionString;
            MessageBox.Show(oleDBConnString, "Connection1");

            //Declaring the variables 
            string oleDBConnString1 = string.Empty;
            IDTSConnectionManagerDatabaseParameters100 connMrgParameters1 = default(IDTSConnectionManagerDatabaseParameters100);
            System.Data.OleDb.OleDbConnection oledbConn1 = null;
            ConnectionManager connMgr1 = Dts.Connections["DevServer1"];
            //Reading the Connection manager Properties and assigning to variables
            connMrgParameters1 = (IDTSConnectionManagerDatabaseParameters100)connMgr1.InnerObject;
            oledbConn1= connMrgParameters1.GetConnectionForSchema() as OleDbConnection;
            oleDBConnString1 = oledbConn1.ConnectionString;
            MessageBox.Show(oleDBConnString1, "Connection2");
            Dts.TaskResult = (int)ScriptResults.Success;

        }


Friday, January 31, 2020

Get only numeric column values SQL




SELECT column1 FROM table WHERE ISNUMERIC(column1) = 1

OR

SELECT column1 FROM table WHERE column1 not like '%[^0-9]%' and column1 != ''

Thursday, January 2, 2020

Flat File destination

With the help of expressions you can make connection dynamic.
Select your flat file connection from Connection Managers pane. In Properties pane, click on Expression(...). Then choose ConnectionString Property from drop down list and in Expression(...) put your expression and evaluate it.
Expression build -

For day : (DT_STR,4,1252)DAY( DATEADD( "dd", -1, getdate() ))
For month: (DT_STR,4,1252)MONTH( DATEADD( "dd", -1, getdate() ))
For Year:  (DT_STR,4,1252)YEAR( DATEADD( "dd", -1, getdate() )
Example expression(you need to tweak as per your requirement) -
which is giving E:\Backup\EmployeeCount_20140627.txt as value.

Please note - You need a working flat file connection so first create flat file connection whose connectionString property is then going to be replaced automatically by expression.

You can follow these step by step articles as well.
enter image description here
enter image description here