General Car Related Discussion. To discuss anything that is related to cars and automotive technology that doesnt naturally fit into another forum catagory.

SQL help - SORTED - Thanks Shonky

Thread Tools
 
Search this Thread
 
Old 14-05-2008, 01:43 PM
  #1  
Chip
*** Sierra RS Custard ***
Thread Starter
iTrader: (3)
 
Chip's Avatar
 
Join Date: Dec 2006
Posts: 47,250
Received 22 Likes on 14 Posts
Default SQL help - SORTED - Thanks Shonky

Total mental block here as not needed to do this for a while, whats the command to replace text in a variable on the way out (ie the equivalent of a javascript VAR.replace() type command)

Ie

Select Name.replace('Chip', 'Cunt') from Users

Its NOT that, just hopefully that should make sense as to what im trying to do!

Last edited by Chip; 14-05-2008 at 01:52 PM.
Old 14-05-2008, 01:46 PM
  #2  
Shonky
V6 Ka of Love
 
Shonky's Avatar
 
Join Date: Jun 2004
Location: South Wales
Posts: 460
Likes: 0
Received 0 Likes on 0 Posts
Default

Originally Posted by Chip-3Door
Total mental block here as not needed to do this for a while, whats the command to replace text in a variable on the way out (ie the equivalent of a javascript VAR.replace() type command)

Ie

Select Name.replace('Chip', 'Cunt') from Users

Its NOT that, just hopefully that should make sense as to what im trying to do!

replace(a.name, 'chip', 'cunt')
Old 14-05-2008, 01:46 PM
  #3  
Chip
*** Sierra RS Custard ***
Thread Starter
iTrader: (3)
 
Chip's Avatar
 
Join Date: Dec 2006
Posts: 47,250
Received 22 Likes on 14 Posts
Default

Ah, right function name, wrong syntax.

Many thanks

Last edited by Chip; 14-05-2008 at 01:48 PM.
Old 14-05-2008, 02:00 PM
  #4  
pa_sjo
Colossal Pervert
 
pa_sjo's Avatar
 
Join Date: Jun 2003
Location: Location: Location:
Posts: 3,678
Likes: 0
Received 1 Like on 1 Post
Default

UPDATE Users SET Name = 'Cunt' WHERE Name = 'Chip'

.. slightly more orthadox?
Old 14-05-2008, 02:07 PM
  #5  
Chip
*** Sierra RS Custard ***
Thread Starter
iTrader: (3)
 
Chip's Avatar
 
Join Date: Dec 2006
Posts: 47,250
Received 22 Likes on 14 Posts
Default

Originally Posted by pa_sjo
UPDATE Users SET Name = 'Cunt' WHERE Name = 'Chip'

.. slightly more orthadox?
No, because that will change the data in the table, I dont want to touch the actual data, I just want to manipulate a copy of it.

What im actually doing is crossreferencing from one table to another, and the data in the first table can be tagged with thing like <asset>some-item-name</asset> to say that when rendering a page from that table you need to go and insert an asset with that item name, and some software im writing needs to do a similar thing, but I cant just do where "this = that" I have to do where "this = replace(replace(that,'<asset>',''),'<asset>','')

So that: <asset>some-item-name</asset>
becomes: some-item-name
during the query, but the underlying data isnt altered.


So my actual query ended up like this:

select bv_editorial.author, main_graphic, ad_abstract, location from bv_editorial, bv_advertisement, tgb_assets where tgb_assets.name = replace(replace(ad_abstract,'</asset>',''),'<asset>','') and ad_name=main_graphic and bv_editorial.store_id = 101 and bv_editorial.status =1 and bv_editorial.deleted=0 and main_graphic is not null and ad_abstract like '<asset>%'

Last edited by Chip; 14-05-2008 at 02:09 PM.
Old 14-05-2008, 02:15 PM
  #6  
pa_sjo
Colossal Pervert
 
pa_sjo's Avatar
 
Join Date: Jun 2003
Location: Location: Location:
Posts: 3,678
Likes: 0
Received 1 Like on 1 Post
Default

Fair enough, I didn't read the original question properly. Personally i'm a fan of doing this in the code itself* rather in the query, but I doubt either of us can be arsed to do a profiling exercise to display which method is better

* Unless this is in stored proc
Old 14-05-2008, 02:29 PM
  #7  
Chip
*** Sierra RS Custard ***
Thread Starter
iTrader: (3)
 
Chip's Avatar
 
Join Date: Dec 2006
Posts: 47,250
Received 22 Likes on 14 Posts
Default

doing it in the code itself, in the environment i work in normally, would mean moving all the data into memory on the machine where the code executes, rather than manipulating it on the DB server where it already is, so for that reason id use a stored procedure

Anyway, in this particular instance its just for a report, so only needs to be run directly in SQL anyway, so without a doubt is the best way of doing so.
Old 14-05-2008, 03:33 PM
  #8  
Shonky
V6 Ka of Love
 
Shonky's Avatar
 
Join Date: Jun 2004
Location: South Wales
Posts: 460
Likes: 0
Received 0 Likes on 0 Posts
Default

No probs
Old 14-05-2008, 03:54 PM
  #9  
frog
0-60 in 17 seconds (eek)
 
frog's Avatar
 
Join Date: Apr 2004
Location: Berkshire
Posts: 6,717
Likes: 0
Received 0 Likes on 0 Posts
Default

Originally Posted by pa_sjo
UPDATE Users SET Name = 'Cunt' WHERE Name = 'Chip'

.. slightly more orthadox?
As chip said, that would update the table directly, however, what you're suggesting isn't the same as:

update users set name = replace(name,'Cunt','Chip') which would update all occurrences of Cunt in the name column regardless of its contents (rows where name is 'My name is Cunt', 'I am a Cunt', etc...)

TRANSLATE is also a very useful function if you want to replace single characters with others
Old 14-05-2008, 03:56 PM
  #10  
Chip
*** Sierra RS Custard ***
Thread Starter
iTrader: (3)
 
Chip's Avatar
 
Join Date: Dec 2006
Posts: 47,250
Received 22 Likes on 14 Posts
Default

Frog, correct, in this instance I was only after replacing a small part of the text (removing in fact) not doing an update of only rows with EXACTLY that as the whole entry.
Old 14-05-2008, 03:57 PM
  #11  
frog
0-60 in 17 seconds (eek)
 
frog's Avatar
 
Join Date: Apr 2004
Location: Berkshire
Posts: 6,717
Likes: 0
Received 0 Likes on 0 Posts
Default

Originally Posted by pa_sjo
Fair enough, I didn't read the original question properly. Personally i'm a fan of doing this in the code itself* rather in the query, but I doubt either of us can be arsed to do a profiling exercise to display which method is better

* Unless this is in stored proc
Regardless of profiling and performance, this raises a whole host of questions in terms of code stability and maintainability where database structures are inherently stored in non DB code (e.g. linked queries, db updates in application server or web server), as opposed to abstracted by means of stored procedures, functions, packages and views

Last edited by frog; 14-05-2008 at 04:12 PM.
Old 14-05-2008, 04:06 PM
  #12  
Chip
*** Sierra RS Custard ***
Thread Starter
iTrader: (3)
 
Chip's Avatar
 
Join Date: Dec 2006
Posts: 47,250
Received 22 Likes on 14 Posts
Default

Yes, also true
Related Topics
Thread
Thread Starter
Forum
Replies
Last Post
boeb
Ford Escort RS Turbo
5
09-01-2020 05:11 AM
Fiddy
Pictures, video & Photoshop Forum
21
29-09-2015 06:10 PM
Shaunc
Escort Range
4
23-09-2015 06:00 AM
wheelwizardrefurbs
Technical help Q & A
5
22-09-2015 05:45 PM



Quick Reply: SQL help - SORTED - Thanks Shonky



All times are GMT. The time now is 06:38 PM.