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

Is there any best way to REPLACE the query? Expand / Collapse
Author
Message
Posted Wednesday, September 26, 2012 5:27 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, May 10, 2013 5:41 AM
Points: 12, 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

Post #1364579
Posted Wednesday, September 26, 2012 5:47 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: 320, Visits: 1,079
Nested REPLACE is very efficient, so there will not be a faster way.
Post #1364587
Posted Wednesday, September 26, 2012 5:50 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, May 10, 2013 5:41 AM
Points: 12, Visits: 32
Thanks dude
Post #1364591
Posted Wednesday, September 26, 2012 7:25 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: Monday, September 30, 2013 2:09 AM
Points: 3,131, Visits: 1,058
Using Replace is good logic and it will be faster


Post #1364661
Posted Thursday, September 27, 2012 12:03 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:44 PM
Points: 3,610, Visits: 5,223
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!
Post #1365041
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse