Click here to Skip to main content
15,891,828 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to update a value of particular column partially in a table. but don't getting the idea about which command to use in sql query.

e.g.
SQL
Id   |Status
-------------
1    |lvl1=100;lvl2=100\rank=2\...


Let's say lvl1 and lvl2 are kind of levels of a game.

whenever player replays the game value should be lvl1=0\rank=0\...
or points get higher then rank should be changed accordingly.

Please help.
Posted
Comments
[no name] 23-Sep-14 13:46pm    
What language would you like an example in?
And you cant set values on a base object without the expression of its properties.
Please provide more details as to language, and controls you are using, etc...
Kschuler 23-Sep-14 13:46pm    
The value in the Status column is a delimited string with all kinds of level and rank information? Why don't you have separate columns for Level and Rank?
Sneha_10 24-Sep-14 1:42am    
I don't know why database developer has designed it like that as I've been asked to solve only this particular problem by them.
Kschuler 24-Sep-14 8:31am    
Yikes. Well I second what George Jonsson said. You'll want to research regular expressions. Good luck!

1 solution

Having one column with many values is a very bad design.
That is why you asking your question.
My best advice is to learn how to normalize your database as your current design will give you lots of headaches in the future.

Database normalization[^]

3 Normal Forms Database Tutorial[^]

Database Normalization Basics[^]

If this approach is the only way, then a regular expression can be used to extract the values.
C#
Regex regex = new Regex(@"lvl(?<level>[0-9]+)\=(?<value>[0-9]+)(\\rank=(?<rank>[0-9]+))?");
foreach (Match m in regex.Matches(@"lvl1=100;lvl2=100\rank=2\..."))
{
    int level = int.Parse(m.Groups["level"].Value);
    int value = int.Parse(m.Groups["value"].Value);
    int rank = int.Parse((m.Groups["rank"].Value == "") ? "0" : m.Groups["rank"].Value);
}

Without more info about the logic it is the best I can do.
 
Share this answer
 
v2
Comments
Sneha_10 24-Sep-14 1:37am    
Thanks for your help. Well I do know about Normalization and I also felt like this is bad structure..But this is someone else's problem & they asked me to help with this. So before saying anything I thought I should confirm first if it is possible or not.
So Thank You for clearing this out.
George Jonsson 24-Sep-14 2:21am    
Understood.
It is possible for sure to do, just a bad idea.
I would use Regex to interpret the column value, and do the logic in c#.
However, it is too little information to create the necessary logic.
Sneha_10 24-Sep-14 14:37pm    
Yes somehow it is possible. & I tried using Replace command in update query,But didn't helped as expected.
So can you give little more information or any reference links about what you mentioned?
George Jonsson 24-Sep-14 15:45pm    
Well, you need to give more info.
What is the logic for updating the level and rank?
Sneha_10 25-Sep-14 1:10am    
Sorry,I can't share coding here as it is someone else application.But as I said in example,Assume a situation of playing game. Multiple levels and their points and according to points player's rank is decided. After levels are finished there is opportunity to restart game from first level again with some bonus privileges. So when player restarts game we have to modify scores to 0 as shown in question(in Bold letters). Hope this will give enough idea. And yes this is just kind of assumption.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900