About Me

Professional Practical HumanBeing

Monday, January 3, 2011

What is the Main difference between String and StringBuilder and why do we use StringBuilder

Note 1 :-

Use StringBuilder if you are planning to change/concatenate a string a lot, as it's slightly more efficient.

This is because a string type is immutable within the CLR, this means that if you do this:

string test = String.Empty();

test = test + "Hello";

test = test + " World!";

You've actually created 3 strings, one for each time it's been changed

Whereas

StringBuilder test = new StringBuilder();

test.Append("Hello");

test.Append(" World!");

This has created just ONE StringBuilder

StringBuilder in the Java world is called StringBuffer. I think!

Hope this has cleared it up!

Note 2:-

String are Immutable (Not Modifiable). If you try to modify  the string it actually creates 
a new string and the old  string will be then ready for garbage collection.
StringBuilder when instantiated, creates a new string with  predefined capacity and upto that 
capacity it can  accodomate string without needing to create a new memory  location for the 
string....i mean it is modifiable and can  also grow as and when needed. 
When the string needs to be modified frequently, preferably  use StringBuilder as its optimized 
for such situations.

Note 3 :-

Both String and StringBuilder are classes used to handle the strings.
The most common operation with a string is concatenation. This activity has to be 
performed very efficiently.
When we use the "String" object to concatenate two strings, the first string is 
combined to the other string by creating a new copy in the memory as a string object,
and then the old string is deleted.
This process is a little long. Hence we say "Strings are immutable".
When we make use of the "StringBuilder" object, the Append method is used. 
This means, an insertion is done on the existing string.
Operation on StringBuilder object is faster than String operations, as the copy 
is done to the same location. Usage of StringBuilder is more efficient in case large
amounts of string manipulations have to be performed.

SQL SERVER – Get Common Records From Two Tables Without Using Join

I really enjoy answering questions which I receive from either comments or Email. My passion is shared by SQL Server Expert Imran Mohammed. He frequently SQL community members by answering their questions frequently and promptly.

Sachin Asked:

Following is my scenario,
Suppose Table 1 and Table 2 has same column e.g. Column1
Following is the query,

1. Select column1,column2 From Table1
2. Select column1 From Table2

I want to find common records from these tables, but i don’t want to use Join clause bcoz for that i need to specify the column name for Join condition. Will you help me to get common records without using Join condition? I am using SQL Server 2005.

Imran Mohammed Replied:

If you are using SQL Server 2005, then you can use Intersect Key word, which gives you common records.

SELECT column1
FROM table1
INTERSECT
SELECT
column1
FROM table2

If you want in the output both column1 and column2 from table1 which has common columns1 in both tables.

SELECT column1, column2
FROM table1
WHERE column1 IN
(
SELECT column1
FROM table1
INTERSECT
SELECT
column1
FROM table2
)

To do this, make sure your column1 is unique and do not have duplicate records.

This is good answer. INTERSECT is new operator in SQL Server which gives you similar answer without using JOIN. I have previously written article where I have compared INTERSECT with INNER JOIN I suggest that all user read that article for further clarity.


SQL SERVER – 2005 – Difference Between INTERSECT and INNER JOIN – INTERSECT vs. INNER JOIN

INTERSECT operator in SQL Server 2005 is used to retrieve the common records from both the left and the right query of the Intersect Operator. INTERSECT operator returns almost same results as INNER JOIN clause many times.

When using INTERSECT operator the number and the order of the columns must be the same in all queries as well data type must be compatible.

Let us see understand how INTERSECT and INNER JOIN are related.We will be using AdventureWorks database to demonstrate our example.

Example 1: Simple Example of INTERSECT

SELECT *
FROM HumanResources.EmployeeDepartmentHistory
WHERE EmployeeID IN (1,2,3)
INTERSECT
SELECT
*
FROM HumanResources.EmployeeDepartmentHistory
WHERE EmployeeID IN (3,2,5)

ResultSet:

Explanation:
The ResultSet shows the EmployeeID which are common in both the Queries, i.e 2 and 3.

Example 2: Using simple INTERSECTbetween two tables.

SELECT VendorID,ModifiedDate
FROM Purchasing.VendorContact
INTERSECT
SELECT
VendorID,ModifiedDate
FROM Purchasing.VendorAddress

ResultSet:



Explanation:

The Resultset shows the records that are common in both the tables. It shows 104 common records between the tables.

Example 3: Using INNER JOIN.

SELECT va.VendorID,va.ModifiedDate
FROM Purchasing.VendorContact vc
INNER JOIN Purchasing.VendorAddress va ON vc.VendorID = va.VendorID
AND vc.ModifiedDate = va.ModifiedDate

ResultSet:


Exlanation :
The resultset displays all the records which are common to both the tables. Additionally in example above INNER JOIN retrieves all the records from the left table and all the records from the right table. Carefully observing we can notice many of the records as duplicate records. When INNER JOIN is used it gives us duplicate records, but that is not in the case of INTERSECT operator.

Example 4: Using INNER JOIN with Distinct.

SELECT DISTINCT va.VendorID,va.ModifiedDate
FROM Purchasing.VendorContact vc
INNER JOIN Purchasing.VendorAddress va ON vc.VendorID = va.VendorID
AND vc.ModifiedDate = va.ModifiedDate

ResultSet:

Explanation:
The resultset in this example does not contain any duplicate records as DISTINCT clause is used in SELECT statement. DISTINCT removes the duplicate rows and final result in this example is exactly same as example 2 described above. In this way, INNER JOIN can simulate with INTERSECT when used with DISTINCT.

Summary :

INNER JOIN can simulate with INTERSECT when used with DISTINCT.


Source :-

http://blog.sqlauthority.com/2008/10/17/sql-server-get-common-records-from-two-tables-without-using-join/

http://blog.sqlauthority.com/2008/08/03/sql-server-2005-difference-between-intersect-and-inner-join-intersect-vs-inner-join/