02 February, 2011

Combine Multiple Records Comma Separated In One Column MSSQL 2005

In this example i am going to describe how to combine multiple records in a column in MS SQL into one record comma separated.

Here is the scenario

I have a table having Employees names and their respective Department names, now i want to show Employees names separated by comma into one column and respective Department name in another column.

My table schema is shown in the image below



And this is Data into table



I want output in following format
Department---------------FirstName
IT----------------------amiT,Emp1,Emp5
Admin-------------------Shobhit, Emp3,Emp7
and so on

To get this desired result we need to write below mentioned query
SELECT DISTINCT Department,
EmpNames = substring( ( SELECT ', ' + FirstName
FROM Employees e2
WHERE e2.Department = e1.Department FOR XML path(''), elements),2,500)
FROM Employees e1

And the output of this SQL Query would be

27 January, 2011

No need to know old password for change password in LAN?

Open Command Prompt.....

Then C:\>net user username newpassword

for example: your user name imran  and change password to 786786786

C:\>net user imran 786786786

N.B.: You must login as a administrator

10 October, 2010

DML, DDL, DCL and TCL in SQL Server

DML

DML is abbreviation of Data Manipulation Language. It is used to retrieve, store, modify, delete, insert and update data in database.

Examples: SELECT, UPDATE, INSERT, DELETE statements

DDL

DDL is abbreviation of Data Definition Language. It is used to create and modify the structure of database objects in database.

Examples: CREATE, ALTER, DROP statements

DCL

DCL is abbreviation of Data Control Language. It is used to create roles, permissions, and referential integrity as well it is used to control access to database by securing it.

Examples: GRANT, REVOKE statements

TCL

TCL is abbreviation of Transactional Control Language. It is used to manage different transactions occurring within a database.

Examples: COMMIT, ROLLBACK statements