Solved

T-SQL string help

Posted on 2011-09-28
4
236 Views
Last Modified: 2012-05-12
We have a horrible table that I have adopted where the data type is nvarchar.. The data is supposed to look like 99 in the Grade2 column and most of it does but some records have $99,$110,$299.. ALL THREE plus $.. So basically this is what I want to do.. IF the record has $99,$110,$299 in Grade2  I want to put the 99 in a new column called Grade1 and leave 110 in the column called Grade2 and drop the third value $299.. I only want to do this if the column called Desc is not null.. I know this will be a Case Select I think? I have attached a spreadsheet as an example.. Tab1 shows what it currently looks like and Tab2 shows what I want. How do I do this?
Example.xls
0
Comment
Question by:cheryl9063
  • 3
4 Comments
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 36742197
update yourtable
set grade1=99
    ,grade2=110
where grade2 ='$99,$110,$299'
AND DESC IS NOT NULL
0
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 36742640
SORRY

update yourtable
set grade1='99'
    ,grade2='110'
where grade2 ='$99,$110,$299'
AND DESC IS NOT NULL
0
 
LVL 50

Accepted Solution

by:
Lowfatspread earned 500 total points
ID: 36746637
or where you wanting this?

update yourtable
set grade1=substring(grade2,2,charindex(',',grade2)-2)
  ,grade2=substring(grade2,charindex(',',grade2)+3,charindex(',',grade2,charindex(',',grade2,2)+1)-charindex(',',grade2)-2)
where grade2 like '$%,$%,%'
and desc is not null
0
 
LVL 1

Author Closing Comment

by:cheryl9063
ID: 36905022
Thanks!!!
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now