Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 123»»»

practical application of REVERSE function? Expand / Collapse
Author
Message
Posted Monday, July 30, 2012 10:44 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Wednesday, September 10, 2014 4:45 PM
Points: 308, Visits: 839
Will people kindly share practical applications of REVERSE sql function? I do not see any posts on this forum addressing it and am curious.
Thanks in advance.
Post #1337401
Posted Monday, July 30, 2012 10:46 AM


Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Friday, June 27, 2014 8:03 AM
Points: 317, Visits: 1,079
They must have been thinking of something when they added it. Don't know what though...
Post #1337405
Posted Monday, July 30, 2012 10:46 AM
Right there with Babe

Right there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with Babe

Group: General Forum Members
Last Login: Yesterday @ 4:55 PM
Points: 732, Visits: 6,167
I've used it to parse out address fields that have city, state and zip in one field. It's usually easier to look through it backwards with the idea that everything up to the first space will be the zip code, and going from there.
Post #1337406
Posted Monday, July 30, 2012 10:51 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Friday, September 12, 2014 5:21 PM
Points: 2,262, Visits: 5,406
I use it mostly to parse out the file names from a long file path. Example:
DECLARE @FilePath VARCHAR(100)
SET @FilePath = 'C:\Users\abcd\AppData\Local\Temp\Temp.txt'
SELECT REVERSE(LEFT ( REVERSE(@FilePath) , CHARINDEX('\',REVERSE(@FilePath))))

Instead of finding the last backslash throu string splitters, i used combination of functions along with REVERSE to do the task..
Post #1337410
Posted Monday, July 30, 2012 11:26 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 1:20 AM
Points: 5,168, Visits: 12,018
ColdCoffee (7/30/2012)
I use it mostly to parse out the file names from a long file path. Example:
DECLARE @FilePath VARCHAR(100)
SET @FilePath = 'C:\Users\abcd\AppData\Local\Temp\Temp.txt'
SELECT REVERSE(LEFT ( REVERSE(@FilePath) , CHARINDEX('\',REVERSE(@FilePath))))

Instead of finding the last backslash throu string splitters, i used combination of functions along with REVERSE to do the task..


Generalisation - sometimes you'll find that a certain character in a string is repeated several times, and you want to extract the chunk of the string between the last occurrence of this character and the end of the string.

Without REVERSE(), this would be quite involved. ColdCoffee's solution shows how easy it is with REVERSE().



Help us to help you. For better, quicker and more-focused answers to your questions, consider following the advice in this link.

When you ask a question (and please do ask a question: "My T-SQL does not work" just doesn't cut it), please provide enough information for us to understand its context.
Post #1337434
Posted Monday, July 30, 2012 11:36 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Yesterday @ 9:33 PM
Points: 3,771, Visits: 8,460
You can use it to check if 2 phrases are palindromes.
Just kidding.



Luis C.
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1337438
Posted Monday, July 30, 2012 11:56 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Wednesday, September 10, 2014 4:45 PM
Points: 308, Visits: 839
OK, so you reverse the expression, find the character you want, perform the operation, and then reverse the expression again to aright it.
I have seen such situations arise, I now realize. Thank you CC and everyone for the responses.
Post #1337447
Posted Monday, July 30, 2012 12:12 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 11:50 AM
Points: 13,082, Visits: 12,547
Luis Cazares (7/30/2012)
You can use it to check if 2 phrases are palindromes.
Just kidding.


create function IsPalindrome(@String1 varchar(100), @String2 varchar(100))
returns table
return
(
select Case when @String1 = REVERSE(@String2) then 1 else 0 end as IsPalindrome
)

go

with cte (string1, string2)
as
(
Select 'racecar', 'racecar' union all
select 'racetruck', 'racetruck'
)

select *
from cte
cross apply dbo.IsPalindrome(string1, string2)



_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1337457
Posted Monday, July 30, 2012 12:33 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Yesterday @ 9:33 PM
Points: 3,771, Visits: 8,460
Thanks Sean, but I was joking with this because I'm not sure palindromes count as a "practical application".


Luis C.
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1337463
Posted Monday, July 30, 2012 12:43 PM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Wednesday, September 10, 2014 4:45 PM
Points: 308, Visits: 839
when I said thanks to you, Luis, it was for the relationship advice, not the palindrome idea.
Post #1337467
« Prev Topic | Next Topic »

Add to briefcase 123»»»

Permissions Expand / Collapse