Unix Technical Forum

String Manipulation SQL

This is a discussion on String Manipulation SQL within the SQL Server forums, part of the Microsoft SQL Server category; --> We have some rows that we need to do some tricky string manipulation on. We have a UserID column ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-29-2008, 05:40 AM
siddhartha mulpuru
 
Posts: n/a
Default String Manipulation SQL

We have some rows that we need to do some tricky string manipulation
on.

We have a UserID column which has userid entries in the format
firstname.lastname and i need to change each entry to
lastname.firstname

Can this be done by some script?

Thanks so much for your help.

Sid
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-29-2008, 05:40 AM
Ryan
 
Posts: n/a
Default Re: String Manipulation SQL

Try the following :

CREATE TABLE
UserNamesList(UserName varChar(50) NULL)

INSERT INTO UserNamesList SELECT 'Ryan.Offord' AS UserName
INSERT INTO UserNamesList SELECT 'Joe.Somebody' AS UserName

SELECT
UserName,
RIGHT(UserName, CharIndex('.',Reverse(UserName))-1) + '.' +
LEFT(UserName, CharIndex('.',(UserName))-1) AS Swapped

FROM
UserNamesList

DROP TABLE UserNamesList

It should swap them over using the full stop as the point where it needs
to do this.

Hope that helps

Ryan


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-29-2008, 05:41 AM
siddhartha mulpuru
 
Posts: n/a
Default Re: String Manipulation SQL

Ryan <anonymous@devdex.com> wrote in message news:<417fb2aa$0$24932$c397aba@news.newsgroups.ws> ...
> Try the following :
>
> CREATE TABLE
> UserNamesList(UserName varChar(50) NULL)
>
> INSERT INTO UserNamesList SELECT 'Ryan.Offord' AS UserName
> INSERT INTO UserNamesList SELECT 'Joe.Somebody' AS UserName
>



Do i have to create a new table inserting each record manually from
the existing table? is there any other way to do this
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-29-2008, 05:41 AM
Erland Sommarskog
 
Posts: n/a
Default Re: String Manipulation SQL

siddhartha mulpuru (justkule@yahoo.com) writes:
> Do i have to create a new table inserting each record manually from
> the existing table? is there any other way to do this


No, you could use an UPDATE statement, working from Ryan's sample.

Generally, when you post a question asking for a query, it is good
advice to post:

o CREATE TABLE statement for your table
o INSERT statements with sample data.
o The desired result.

This makes it easy for anyone to answers to cut and paste into Query
Analyzer to give you a tested solution. You did not post any of this,
and Ryan still took the task to post a tested solution, but he did it
in his way, so you could see the technique.




--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 02:50 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com