×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

MS SQL: duplicates with DISTINCT

MS SQL: duplicates with DISTINCT

MS SQL: duplicates with DISTINCT

(OP)
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

RE: MS SQL: duplicates with DISTINCT

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.

RE: MS SQL: duplicates with DISTINCT

(OP)
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.

RE: MS SQL: duplicates with DISTINCT

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: www.essexsteam.co.uk

RE: MS SQL: duplicates with DISTINCT

(OP)
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)

RE: MS SQL: duplicates with DISTINCT

(OP)

T-SQL deals with ASCII strings

Yes, and unicode too. See nvarchar

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources