Wednesday, July 8, 2009

How to Block/ Avoid Blankspaces while Password serching in SQL Server Querries

Sql server has drawback that it is case insensitive as well as it perform trim opertaion on tralling spaces.
thus if a user enters a password and apllies traling spaces to it.
sql server accept it.

To avoid it i use .
Datalength and len function of SQL

Datalength( ): Accepts trailing and leading spaces and give its count with White/blacnk spaces.

Len( ): This Perform Trim operation by default. This It avoid triling Spaces but accepts
Leading spaces.

Datalength('suyash1') =7
Datalength('suyash1 ') =8
Datalength('suyash1 ') =9
Datalength(' suyash1') =8

while

Len('suyash1') =7
Len('suyash1 ') =7
Len('suyash1 ') =7
Len(' suyash1') =8 ****** NOTE it


For Trim In Sql server
use: LTRIM(RTRIM('colname'))
(TRim() is not availabel in Sql server 2005)


Example:
select a.loginid,a.password from Login_ a
where a.loginid='suyash3211' and b.password = 'suyash1' COLLATE SQL_Latin1_General_CP1_CS_AS
and Len((b.password))=Datalength('suyash1' )