HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlMajor

Using Replace in SQL

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
sqlreplaceusing

Problem

I have a table and I need to update some names but I was wondering about the

following queries :

Will both do the same?

Query1

Update mytable
Set Name = Replace(Name,'Jeff','Joe')


Query2

Update mytable
Set Name = 'Joe'
where Name = 'Jeff'

Solution

No, they will not do the same thing.

-
The amount of effort required by the SQL engine is completely different. In the first query, the engine must go through every row and perform a string replace operation on the Name column. In the second query, it is searching through the table where the name is "Jeff" and just updating the Name column to be Joe.

-
String replacement is wildcard. So in the first query, the Name "Jeffrey" would become "Joerey".

Context

StackExchange Database Administrators Q#163875, answer score: 26

Revisions (0)

No revisions yet.