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 May 14, 2008 | 01:43 PM
  #1  
Chip's Avatar
Chip
Thread Starter
*** Sierra RS Custard ***
iTrader: (3)
 
Joined: Dec 2006
Posts: 47,250
Likes: 22
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; May 14, 2008 at 01:52 PM.
Reply
Old May 14, 2008 | 01:46 PM
  #2  
Shonky's Avatar
Shonky
V6 Ka of Love
 
Joined: Jun 2004
Posts: 460
Likes: 0
From: South Wales
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')
Reply
Old May 14, 2008 | 01:46 PM
  #3  
Chip's Avatar
Chip
Thread Starter
*** Sierra RS Custard ***
iTrader: (3)
 
Joined: Dec 2006
Posts: 47,250
Likes: 22
Default

Ah, right function name, wrong syntax.

Many thanks

Last edited by Chip; May 14, 2008 at 01:48 PM.
Reply
Old May 14, 2008 | 02:00 PM
  #4  
pa_sjo's Avatar
pa_sjo
Colossal Pervert
 
Joined: Jun 2003
Posts: 3,678
Likes: 1
From: Location: Location:
Default

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

.. slightly more orthadox?
Reply
Old May 14, 2008 | 02:07 PM
  #5  
Chip's Avatar
Chip
Thread Starter
*** Sierra RS Custard ***
iTrader: (3)
 
Joined: Dec 2006
Posts: 47,250
Likes: 22
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; May 14, 2008 at 02:09 PM.
Reply
Old May 14, 2008 | 02:15 PM
  #6  
pa_sjo's Avatar
pa_sjo
Colossal Pervert
 
Joined: Jun 2003
Posts: 3,678
Likes: 1
From: Location: Location:
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
Reply
Old May 14, 2008 | 02:29 PM
  #7  
Chip's Avatar
Chip
Thread Starter
*** Sierra RS Custard ***
iTrader: (3)
 
Joined: Dec 2006
Posts: 47,250
Likes: 22
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.
Reply
Old May 14, 2008 | 03:33 PM
  #8  
Shonky's Avatar
Shonky
V6 Ka of Love
 
Joined: Jun 2004
Posts: 460
Likes: 0
From: South Wales
Default

No probs
Reply
Old May 14, 2008 | 03:54 PM
  #9  
frog's Avatar
frog
0-60 in 17 seconds (eek)
 
Joined: Apr 2004
Posts: 6,717
Likes: 0
From: Berkshire
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
Reply
Old May 14, 2008 | 03:56 PM
  #10  
Chip's Avatar
Chip
Thread Starter
*** Sierra RS Custard ***
iTrader: (3)
 
Joined: Dec 2006
Posts: 47,250
Likes: 22
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.
Reply
Old May 14, 2008 | 03:57 PM
  #11  
frog's Avatar
frog
0-60 in 17 seconds (eek)
 
Joined: Apr 2004
Posts: 6,717
Likes: 0
From: Berkshire
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; May 14, 2008 at 04:12 PM.
Reply
Old May 14, 2008 | 04:06 PM
  #12  
Chip's Avatar
Chip
Thread Starter
*** Sierra RS Custard ***
iTrader: (3)
 
Joined: Dec 2006
Posts: 47,250
Likes: 22
Default

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




All times are GMT. The time now is 04:55 AM.