Showing posts with label containing. Show all posts
Showing posts with label containing. Show all posts

Monday, March 26, 2012

fetching a string containging character " ' " from Datatable

Hello! I have a problem with selecting some string from a datatable containing character " ' " under the values of some attributes. For example I have a table called Table and attribute type of NVCHAR(255) called SomeString. This attribute contains strings with a charcter " ' " and I am unable to pull out the value using SELECT statement.

For example:

SELECT SomeAttribute FROM Table WHERE SomeString =' He's riding a bike ';

Because character " ' " is reserved for borders of the string and is also a part of my string. Is there any possibility to solve such a "problem"?

Thanx

Just escape the quote char.


SELECT SomeAttribute FROM Table WHERE SomeString =' He''s riding a bike ';

A better thing is using paramitrimized queries. Then you never have to worry about format's or SQL Injection.

It's olso better for the preformance, because you don't need to have to concatenate a string for example:


string query = "SELECT * FROM Table1 WHERE ID = " + txtId.Text + " AND Name = \"" + "txtName.Text + "\"";

No escape characters needed, you doesn't have to think about using a " or not etc.

Parameters are like placeholders, you use them in Stored Procedures as well.

A little example:


// TODO: Set date variable.
DateTime date = DateTime.Now;
// Set query and parameters.
const string query = "SELECT * FROM Table1 WHERE MyDate = @.MyDate";
SqlParameter pMyDate = new SqlParameter("@.MyDate", SqlDbType.DateTime);
pMyDate.Value = date;
// Create connection and open it.
SqlConnection dbConn = new SqlConnection("ConnectingString");
dbConn.Open();
try
{
using(SqlCommand dbCommand = new SqlCommand(query, dbConn))
{
// Add paramter to Command.
dbCommand.Parameters.Add( pMyDate );
// Execute the query and get results.
SqlDataReader reader = dbCommand.ExecuteReader();
try
{
// Walkthrough results.
while(reader.Read())
{
// TODO: Do something with the data.
}
}
finally
{
// Close reader.
reader.Close();
}
}
}
finally
{
// Close connection.
dbConn.Close();
}

|||Thank you!

Friday, March 9, 2012

Faster Remove Duplicate SQL

I have a table containing over 100,000 email addresses. This email table gets duplicates in it, and our customers don't want a second (or third or fourth) copy of our news letter. To prevent this, we run the following SQL to kill the duplicates:

Code Snippet

DELETE FROM _email WHERE _email.eid IN
(
SELECT tbl1.eid FROM _email AS tbl1 WHERE Exists
(
SELECT emailaddress, Count(eid) FROM _email WHERE _email.emailaddress = tbl1.emailaddress GROUP BY _email.emailaddress HAVING Count(_email.eid) > 1
)
)
AND _email.eid NOT IN
(
SELECT Min(eid) FROM _email AS tbl1 WHERE Exists
(
SELECT emailaddress, Count(eid) FROM _email WHERE _email.emailaddress = tbl1.emailaddress GROUP BY _email.emailaddress HAVING Count(_email.eid) > 1
)
GROUP BY emailaddress
);


This query takes about 2hrs to run which is really hurting our server preformance. Is there any way to do this faster?

I am running SQL Server 2000

Thanks in advance

Create a unique non clustered index and turn on IGNORE_DUP_KEY after you run your code to delete all the existing duplicates. Then SQL Server will not insert duplicates to the column, try the link below for index options.

http://msdn2.microsoft.com/en-us/library/ms186869.aspx

|||

You can try the following queries,

Code Snippet

delete from _email

from _email A

join (select min(eid) eid,email from _email group by email) B

on A.email = B.email

Where

b.eid <> a.eid

Code Snippet

delete from _email

from _email E

Where exists

(

select 1 from _email A

join (select min(eid) eid,email from _email group by email) B

on A.email = B.email

Where

b.eid <> a.eid

and E.eid=A.eid

)

|||

If you are using SS 2005, then try:

;with cte

as

(

select *, row_number() over(partition by emailaddress order by eid ASC) as rn

from _email

)

delete cte

where rn > 1;

AMB

Wednesday, March 7, 2012

Fast look up of long (n)varchar

I have a table containing URLs. I want to be able to look up an URL very
fast, so I used an nvarchar to store the URL, and put an index on it
(maybe naive).

Anyway, I bump into:
"The index entry of length 911 bytes for the index 'UQ__URL__1367E606'
exceeds the maximum length of 900 bytes."

What's the best way to handle this? I want to do the look up fast. The
only thing I could think up was adding an extra column containing a digest
for the URL, and look up all URLs with the same digest, *and* having the
same value (which could give either 1 or 0 results).

I am new to MS SQL, so I might describe a silly solution, basically I want
to look up URLs to ID the fastest way possible.

--
John MexIT: http://johnbokma.com/mexit/
personal page: http://johnbokma.com/
Experienced programmer available: http://castleamber.com/
Happy Customers: http://castleamber.com/testimonials.htmlJohn,

use an index on checksum(url), as described here:

http://www.devx.com/dbzone/Article/30786|||>Use an index on checksum(url), as described here:

oh wow. that is brilliant. I have never run into that before.

that is a GREAT idea that I am filing away for futures.

thank you!|||John Bokma <john@.castleamber.com> wrote:

> I have a table containing URLs. I want to be able to look up an URL
> very fast, so I used an nvarchar to store the URL, and put an index on
> it (maybe naive).
> Anyway, I bump into:
> "The index entry of length 911 bytes for the index 'UQ__URL__1367E606'
> exceeds the maximum length of 900 bytes."
> What's the best way to handle this? I want to do the look up fast. The
> only thing I could think up was adding an extra column containing a
> digest for the URL, and look up all URLs with the same digest, *and*
> having the same value (which could give either 1 or 0 results).
> I am new to MS SQL, so I might describe a silly solution, basically I
> want to look up URLs to ID the fastest way possible.

To answer my own question: under CHECKSUM in SQL Server Books Online:

"The checksum index can be used as a hash index, particularly to improve
indexing speed when the column to be indexed is a long character column."

Comes with an example, etc.

--
John MexIT: http://johnbokma.com/mexit/
personal page: http://johnbokma.com/
Experienced programmer available: http://castleamber.com/
Happy Customers: http://castleamber.com/testimonials.html