Sunday, February 12, 2012

CONCATENATE() & SUBSTITUTE() in MS EXCEL

I'm using Microsoft Excel for formatted my data sheet. 
I want to merge few cells data to a one cell. The way of I do this
I want to remove some selected values in a cell. The way of I do this

Tuesday, February 7, 2012

DENSE_RANK()

My requirement is to order the contents of my selected SQL query. I can use ORDER BY for fulfill this task, but the problem is it will order my entire SQL query with ascending order or descending order according the way of using it. But my requirement is order the separate sub groups of my selected query. 

Let take this example. 
I want to order the each and every employees dependents, using their birthdays in ascending order with partition by the employee number. 


So I'm using the below SQL command. 

DENSE_RANK() OVER (PARTITION BY EMP_NUMBER ORDER BY DOB)
DENSE_RANK() OVER (PARTITION BY < PARTITION FIELD NAME> ORDER BY <ORDER FIELD NAME>)

Finally my result came as 


Sunday, February 5, 2012

Rename Schema in MS SQL Server

My requirement is to rename the schema in selected tables/views.
Let take a example like this.
Existing table name - HRADMIN.VW_HR_EMPLOYEE
Required table name - HRUSR. VW_HR_EMPLOYEE

You can simply do this by using below SQL command by login with sa account. Make sure your rename schema is exists under your Data Base. Otherwise you need to create it manually.

ALTER SCHEMA <HRUSR> TRANSFER  <HRADMIN>.< VW_HR_EMPLOYEE >

But it's not flexible enough  when we are considering the large database which consisted lot of tables/views under same schema. The below Stored Procedure will help you to generate one script for entire schema which you want to rename.

<Views/tables which exists under ADM schema will rename the schema of CHATHURA >


ALTER PROCEDURE SP_CHANGE_SCHEMA
AS
DECLARE cursore CURSOR FOR
SELECT TABLE_SCHEMA, TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA  =  'ADM'

DECLARE @schema sysname,
 @tab sysname,
 @sql varchar(500)

OPEN cursore  
FETCH NEXT FROM cursore INTO @schema, @tab

WHILE @@FETCH_STATUS = 0  
BEGIN

 SET @sql = 'ALTER SCHEMA CHATHURA TRANSFER ' + @schema + '.' + @tab  
 PRINT @sql  
 FETCH NEXT FROM cursore INTO @schema, @tab  
END

CLOSE cursore  
DEALLOCATE cursore

After creating the SP you can execute it by using this command.

EXEC SP_CHANGE_SCHEMA