Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

T-SQL string help

Posted on 2011-09-28
4
Medium Priority
?
248 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

618 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