SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


LTRIM,RTRIM


LTRIM,RTRIM

Author
Message
daveriya
daveriya
SSC Eights!
SSC Eights! (808 reputation)SSC Eights! (808 reputation)SSC Eights! (808 reputation)SSC Eights! (808 reputation)SSC Eights! (808 reputation)SSC Eights! (808 reputation)SSC Eights! (808 reputation)SSC Eights! (808 reputation)

Group: General Forum Members
Points: 808 Visits: 236
hi

i have this code in where clause RTRIM(LTRIM(ODER_ID))= ' ',WHAT THis do
toddasd
toddasd
SSCrazy
SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)

Group: General Forum Members
Points: 2300 Visits: 3820

i have this code in where clause RTRIM(LTRIM(ODER_ID))= ' ',WHAT THis do


It filters out rows with non-blank oder_id values. RTRIM and LTRIM simply remove spaces from a string on the right and left.

As an example;

with SampleTable as (
select ' hi ' as oder_id, 1 as Col2 union all
select ' ', 2 union all
select '', 3
)
select * from SampleTable where rtrim(ltrim(oder_id))=''



______________________________________________________________________________
How I want a drink, alcoholic of course, after the heavy lectures involving quantum mechanics.
daveriya
daveriya
SSC Eights!
SSC Eights! (808 reputation)SSC Eights! (808 reputation)SSC Eights! (808 reputation)SSC Eights! (808 reputation)SSC Eights! (808 reputation)SSC Eights! (808 reputation)SSC Eights! (808 reputation)SSC Eights! (808 reputation)

Group: General Forum Members
Points: 808 Visits: 236
so it gives orderid values with no spaces on left or right ?


or it just says orderid should be blank.plz confirm me
ChrisM@Work
ChrisM@Work
SSC Guru
SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)

Group: General Forum Members
Points: 65948 Visits: 20214
daveriya (10/21/2011)
so it gives orderid values with no spaces on left or right ?


or it just says orderid should be blank.plz confirm me


Try experimenting with the sample code which Todd set up for you - you're far more likely to remember the lesson if you get involved.

“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
daveriya
daveriya
SSC Eights!
SSC Eights! (808 reputation)SSC Eights! (808 reputation)SSC Eights! (808 reputation)SSC Eights! (808 reputation)SSC Eights! (808 reputation)SSC Eights! (808 reputation)SSC Eights! (808 reputation)SSC Eights! (808 reputation)

Group: General Forum Members
Points: 808 Visits: 236
i tried it it gives me blank,not the valus,but i think rtrim and ltrim just remove blank spaces ,not the values
vince_sql
vince_sql
SSC Veteran
SSC Veteran (266 reputation)SSC Veteran (266 reputation)SSC Veteran (266 reputation)SSC Veteran (266 reputation)SSC Veteran (266 reputation)SSC Veteran (266 reputation)SSC Veteran (266 reputation)SSC Veteran (266 reputation)

Group: General Forum Members
Points: 266 Visits: 264
daveriya (10/21/2011)
hi

i have this code in where clause RTRIM(LTRIM(ODER_ID))= ' ',WHAT THis do


It's just removing extra blank spaces, but this is un-necessary as SQL views empty strings as empty strings regardless how many spaces there are.

So if your query is trying to return only empty strings, then you dont need to trim.

The code below highlights this, if SQL considered


'' != ' '



the above to be true, then the penultimate select query would only return 1 row, but it returns 2.


CREATE TABLE trimTest (
value varchar(50)
)
INSERT INTO trimTest
VALUES (''),
(' ')

SELECT *
FROM trimTest

SELECT *
FROM trimTest
WHERE value = ''

SELECT *
FROM trimTest
WHERE LTRIM(RTRIM(value)) = ''



http://sqlvince.blogspot.com/
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (340K reputation)SSC Guru (340K reputation)SSC Guru (340K reputation)SSC Guru (340K reputation)SSC Guru (340K reputation)SSC Guru (340K reputation)SSC Guru (340K reputation)SSC Guru (340K reputation)

Group: General Forum Members
Points: 340880 Visits: 42650
vince_sql (10/23/2011)
daveriya (10/21/2011)
hi

i have this code in where clause RTRIM(LTRIM(ODER_ID))= ' ',WHAT THis do


It's just removing extra blank spaces, but this is un-necessary as SQL views empty strings as empty strings regardless how many spaces there are.

So if your query is trying to return only empty strings, then you dont need to trim.

The code below highlights this, if SQL considered


'' != ' '



the above to be true, then the penultimate select query would only return 1 row, but it returns 2.


CREATE TABLE trimTest (
value varchar(50)
)
INSERT INTO trimTest
VALUES (''),
(' ')

SELECT *
FROM trimTest

SELECT *
FROM trimTest
WHERE value = ''

SELECT *
FROM trimTest
WHERE LTRIM(RTRIM(value)) = ''



AND, Vince's query uses a SARGable WHERE clause, as well!

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
ghanshyam.kundu
ghanshyam.kundu
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1283 Visits: 435
jeff can u tell us what is SARGable WHERE clause??
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (340K reputation)SSC Guru (340K reputation)SSC Guru (340K reputation)SSC Guru (340K reputation)SSC Guru (340K reputation)SSC Guru (340K reputation)SSC Guru (340K reputation)SSC Guru (340K reputation)

Group: General Forum Members
Points: 340880 Visits: 42650
SARG stands for "Search ARGument" and "SARGable" has come to mean that the search arguments in a WHERE clause, ORDER BY, and/or ON clause are capable of doing an INDEX SEEK if the appropriate index is available and used.

Search arguments that modify a column will only allow for either a table scan (includes Clustered Index Scan) or a non-Clustered Index Scan.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
ghanshyam.kundu
ghanshyam.kundu
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1283 Visits: 435
Thanks jeff
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search