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


Is there any best way to REPLACE the query?


Is there any best way to REPLACE the query?

Author
Message
lokesha.b
lokesha.b
Grasshopper
Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)

Group: General Forum Members
Points: 14 Visits: 32
Hi there,

I have a below query which needs to replace the label to actual value. I can do this by using REPLACE() recursively. Is there any better way to bring the output?

I'm getting proper output from this query, however I'm curious to find better one. Thanks!

DECLARE @a AS VARCHAR(1000)
SET @A = 'This is <<FirstName>> <<LastName>> FROM <<PLACE>>, MY DOB IS ON <<DATE>> at <<TIME>>. THANKS'
SELECT REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(@A,'<<FirstName>>','LOKESH'),'<<LastName>>','GOWDA'),'<<PLACE>>','MANGALORE'),'<<DATE>>','2000-FEB-09'),'<<TIME>>','09:20pm')


Regards,
Lokesh GB
laurie-789651
laurie-789651
Ten Centuries
Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)

Group: General Forum Members
Points: 1422 Visits: 1272
Nested REPLACE is very efficient, so there will not be a faster way.
lokesha.b
lokesha.b
Grasshopper
Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)

Group: General Forum Members
Points: 14 Visits: 32
Thanks dude :-)
Chandra Sekhara Vyas Dhara
Chandra Sekhara Vyas Dhara
SSCertifiable
SSCertifiable (5K reputation)SSCertifiable (5K reputation)SSCertifiable (5K reputation)SSCertifiable (5K reputation)SSCertifiable (5K reputation)SSCertifiable (5K reputation)SSCertifiable (5K reputation)SSCertifiable (5K reputation)

Group: General Forum Members
Points: 5035 Visits: 1149
Using Replace is good logic and it will be faster



dwain.c
dwain.c
SSCoach
SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)

Group: General Forum Members
Points: 17315 Visits: 6431
laurie-789651 (9/26/2012)
Nested REPLACE is very efficient, so there will not be a faster way.


There's nearly always a faster way:


DECLARE @a AS VARCHAR(1000)
SET @A = 'This is <<FirstName>> <<LastName>> FROM <<PLACE>>, MY DOB IS ON <<DATE>> at <<TIME>>. THANKS'
SELECT REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(@A COLLATE Latin1_general_bin
,'<<FirstName>>','LOKESH'),'<<LastName>>','GOWDA'),'<<PLACE>>','MANGALORE'),'<<DATE>>','2000-FEB-09'),'<<TIME>>','09:20pm')




Explanation of how REPLACE is slow when using non-binary collations: http://connect.microsoft.com/SQLServer/feedback/details/512459/replace-function-extremely-slow-with-non-binary-windows-collation


My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
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