Showing posts with label explain. Show all posts
Showing posts with label explain. Show all posts

Tuesday, March 20, 2012

Automatic RTRIM?

Could anyone explain why this happens:

-- All outputs works but only the first should

select 'works' where '1' = '1'

select 'works' where '1' = '1 '

select 'works' where '1 ' = '1'

Seems to me like trailing blanks are automatically trimmed, why?

SQL Server follows the ANSI/ISO SQL-92 specification (Section 8.2, <Comparison Predicate>, General rules #3) on how to compare strings with spaces. The ANSI standard requires padding for the character strings used in comparisons so that their lengths match before comparing them. The padding directly affects the semantics of WHERE and HAVING clause predicates and other Transact-SQL string comparisons. For example, Transact-SQL considers the strings 'abc' and 'abc ' to be equivalent for most comparison operations.

The only exception to this rule is the LIKE predicate. When the right side of a LIKE predicate expression features a value with a trailing space, SQL Server does not pad the two values to the same length before the comparison occurs. Because the purpose of the LIKE predicate, by definition, is to facilitate pattern searches rather than simple string equality tests, this does not violate the section of the ANSI SQL-92 specification mentioned earlier. (Microsoft, 2003).

Check this out at http://support.microsoft.com/default.aspx?scid=kb;en-us;316626

You can dig up lots of interesting ones, such as:


select 'works' where char(32) = ''

select 'works' where '1' + char (32) = '1'

In BOL, read about some of the string functions, such as Len:


LEN

Returns the number of characters, rather than the number of bytes, of the given string expression, excluding trailing blanks.

Hope this helps.

Lee Everest

MCP MCDBA

Adjunct SQL Instructor

North Lake College
www.texastoo.com/sqlblog

|||An interesting follow-up:

'1' = '1 ' is true
'1' like ' 1 ' is false

LIKE treats the trailing spaces as significant while = does not.
|||

Is there supposed to be a second "1" in your "like" example?

Currently it reads: '1' like ' ' is false

I'm thinking you meant: '1' like '1 ' is false

Or maybe I'm missing something?

Dan

|||You are correct. Typo fixed.
|||;-)

Thursday, February 16, 2012

Auto Stats

Books Online does not explain how this runs for SQL Server 2000 (we are on
SP3a).
Does it take any database or other locks at all ? So that it could
contribute to a timeout/disconnect problem we are getting ?
I don't believe so. I think it's unlikely that that is contributing to
any timeout problem, but you can always disable that option (ALTER
DATABASE <dbname> SET AUTO_UPDATE_STATISTICS OFF) and see if the timeout
problem goes away (just do the statistics updates manually (EXEC
sp_updatestats) out of hours).
*mike hodgson*
blog: http://sqlnerd.blogspot.com
Jim Trowbridge wrote:

>Books Online does not explain how this runs for SQL Server 2000 (we are on
>SP3a).
>Does it take any database or other locks at all ? So that it could
>contribute to a timeout/disconnect problem we are getting ?
>
>

Auto Stats

Books Online does not explain how this runs for SQL Server 2000 (we are on
SP3a).
Does it take any database or other locks at all ? So that it could
contribute to a timeout/disconnect problem we are getting ?This is a multi-part message in MIME format.
--010006090901020804020402
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
I don't believe so. I think it's unlikely that that is contributing to
any timeout problem, but you can always disable that option (ALTER
DATABASE <dbname> SET AUTO_UPDATE_STATISTICS OFF) and see if the timeout
problem goes away (just do the statistics updates manually (EXEC
sp_updatestats) out of hours).
--
*mike hodgson*
blog: http://sqlnerd.blogspot.com
Jim Trowbridge wrote:
>Books Online does not explain how this runs for SQL Server 2000 (we are on
>SP3a).
>Does it take any database or other locks at all ? So that it could
>contribute to a timeout/disconnect problem we are getting ?
>
>
--010006090901020804020402
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: 8bit
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=UTF-8" http-equiv="Content-Type">
<title></title>
</head>
<body bgcolor="#ffffff" text="#000000">
<tt>I don't believe so. I think it's unlikely that that is
contributing to any timeout problem, but you can always disable that
option (ALTER DATABASE <dbname> SET AUTO_UPDATE_STATISTICS OFF</tt><tt>)
and see if the timeout problem goes away (just do the statistics
updates manually (EXEC sp_updatestats) out of hours).</tt><br>
<div class="moz-signature">
<title></title>
<meta http-equiv="Content-Type" content="text/html; ">
<p><span lang="en-au"><font face="Tahoma" size="2">--<br>
</font></span> <b><span lang="en-au"><font face="Tahoma" size="2">mike
hodgson</font></span></b><span lang="en-au"><br>
<font face="Tahoma" size="2">blog:</font><font face="Tahoma" size="2"> <a
href="http://links.10026.com/?link=http://sqlnerd.blogspot.com</a></font></span>">http://sqlnerd.blogspot.com">http://sqlnerd.blogspot.com</a></font></span>
</p>
</div>
<br>
<br>
Jim Trowbridge wrote:
<blockquote cite="mid92C390C7-7B08-412E-9053-16C8E295AFBD@.microsoft.com"
type="cite">
<pre wrap="">Books Online does not explain how this runs for SQL Server 2000 (we are on
SP3a).
Does it take any database or other locks at all ? So that it could
contribute to a timeout/disconnect problem we are getting ?
</pre>
</blockquote>
</body>
</html>
--010006090901020804020402--

Auto Stats

Books Online does not explain how this runs for SQL Server 2000 (we are on
SP3a).
Does it take any database or other locks at all ? So that it could
contribute to a timeout/disconnect problem we are getting ?I don't believe so. I think it's unlikely that that is contributing to
any timeout problem, but you can always disable that option (ALTER
DATABASE <dbname> SET AUTO_UPDATE_STATISTICS OFF) and see if the timeout
problem goes away (just do the statistics updates manually (EXEC
sp_updatestats) out of hours).
*mike hodgson*
blog: http://sqlnerd.blogspot.com
Jim Trowbridge wrote:

>Books Online does not explain how this runs for SQL Server 2000 (we are on
>SP3a).
>Does it take any database or other locks at all ? So that it could
>contribute to a timeout/disconnect problem we are getting ?
>
>