Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations waross on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

MS SQL: duplicates with DISTINCT

Status
Not open for further replies.

ricka0

Materials
Jul 13, 2004
4
US
SELECT DISTINCT
LTRIM(RTRIM(queryParam)) AS qpTrimmed
FROM queryParamValTbl

has duplicates (it removes most duplicates)
I assumed that
scngs_zone
scngs_zone

was really
'scngs_zone'
'scngs_zone ' -- trailing whitespace

So I tried the following:

SELECT
DISTINCT
LTRIM(RTRIM(queryParam)) AS qpTrimmed
INTO #rTmp
FROM queryParamValTbl
ORDER BY qpTrimmed

SELECT DISTINCT * FROM #rTmp

-- But it has the exact same duplicates
Note, most duplicates are removed.
SQL 2000 & Win03 - lastest updates on each
 
Replies continue below

Recommended for you

It seems the server excutes DISTINCT before the trimminng. I'm not using MS SQL but I do know it has the feature to work with temporary tables (which is a nice feature). So I'll create a temporary table with the trimmed values and I'll make a SELECT DISTINCT on it.
 
Nope. I figured out the problem is:

RTRIM
Returns a character string after truncating all trailing blanks.


But I don't have blanks, I have '\r\n'
apparently there is no T-SQL function to remove white space.
 
T-SQL deals with ASCII strings, and shouldn't be expected to translate arbitrary character sequences that may represent white space in some other languages.

Good Luck
johnwm
________________________________________________________
To get the best from these forums read faq731-376 before posting

UK steam enthusiasts:
 
I suppose you could argue that, but SQL also lets you specify the language you are using. That same arguement applies double to Java, C#, C++ etc, yet their Trim functions remove whitespace of the character set installed.

It's such a common problem every high level language has the Trim methods to deal with it. Here is how I solved the problem.

CREATE FUNCTION xTRIM (@rVarParam VARCHAR(1024))
RETURNS VARCHAR(1024)
AS
BEGIN

RETURN LTRIM(RTRIM(
REPLACE(REPLACE(@rVarParam,CHAR(10),''),CHAR(13),'')
))
END


Then changed my Query to:

SELECT DISTINCT dbo.xTRIM(queryParam)
FROM queryParamValTbl
WHERE queryParamValue
NOT IN ( SELECT queryName FROM queryNameTbl)

 

T-SQL deals with ASCII strings

Yes, and unicode too. See nvarchar
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top