Jump to content

need geek help: ms sql


gapertimmy

Recommended Posts

  • Replies 10
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

That's a pain, I don't think there are built-in regular expressions for modifying text in that way, you would need to do a script.

 

If you have a perl install on your system and the DBD for MS SQL you could use the above to do the swap (putting in the sql commands instead of standard input). If need be, I could potentially do this from my unix setup here (I would have to hunt down the ms sql stuff, and you would have to trust me with access to your db, if your firewall allows), or you could dump all the data as a tab-delimited text file, run it through perl or something else to do the text swap, then do a load data back into the database. Or you could email me the txt file and I could do this.

Link to comment
Share on other sites

Real ugly but something like. . . .

1) find the index of the second space and select it into a tmp table

 

SELECT pkey, my_column, PATINDEX( '% _ %', my_column) as start, LEN(my_column) as length

FROM my_table INTO my_tmp_table

 

2) now go back

 

 

UPDATE my_table t

SET t.my_column =

(

select SUBSTRING(tmp.my_column, tmp.start, tmp.length) + ', ' + SUBSTRING(tmp.my_column, 0, tmp.start)

from tmp_table tmp where t.pkey=tmp.pkey)

Link to comment
Share on other sites

This works:

 

UPDATE names

SET nameColumn =

RIGHT(nameColumn,CHARINDEX(' ',REVERSE(nameColumn))-1) + ', ' +

LEFT(nameColumn,LEN(nameColumn)-CHARINDEX(' ',REVERSE(nameColumn)))

 

 

 

 

 

Here's a proof-of-concept you can copy-and-paste into Query Analyzer:

 

CREATE TABLE names (

nameColumn VARCHAR(50)

)

 

INSERT INTO names VALUES ('Timmy G Craford')

INSERT INTO names VALUES ('Gaper W Jeffey')

INSERT INTO names VALUES ('Joe Bachy')

INSERT INTO names VALUES ('John Ashcroft')

GO

 

SELECT * FROM names

GO

 

UPDATE names

SET nameColumn =

RIGHT(nameColumn,CHARINDEX(' ',REVERSE(nameColumn))-1) + ', ' +

LEFT(nameColumn,LEN(nameColumn)-CHARINDEX(' ',REVERSE(nameColumn)))

GO

 

SELECT * FROM names

 

 

As always (and I'm sure you're doing) make sure to backup your data first and consider working off a test table or a test column. If any names don't contain spaces (eg they're just 'Bob') the above script will throw an error. If you need it to not do so you should add a WHERE clause which filters out names that don't contain space, eg WHERE nameColumn like '% %'.

 

Hope this helps,

 

Jeff

 

 

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.




×
×
  • Create New...