Click here to Skip to main content
15,907,497 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
I have one table name SpaceRequest showing below

ID(numeric) Name(varchar) Requested_Space(varchar) Status


1 Pranav 1GB True

i want to update values to the column Requested_Space.
i.e 2GB + 1GB should make 3GB in all...through query
how i can do it??
HELP..:)
thanx in advance
Posted

Don't.

While it is technically possible, it's a pain to do, and should be avoided.
It can be done: you have to parse each string yourself, extract the numeric part and the "qualifier" ("GB" in your example) then convert the numeric to a number and "normalize" it to a "proper" number by working out what the qualifier means (so that 3GB + 3000MB will make 6GB, not 3003GB) then convert it back to an appropriate string again afterwards. Not nice, and very awkward.

Instead, store your value as a numeric value, preferably to a "rational" base value: KB for example.
So your 3GB value would be stored as 3 * 1024 * 1024 KB or 3145728 as a number.
You can then add numbers easily, and format them for display very simply when you need to.
 
Share this answer
 
OriginalGriff has given you a very good advice against doing it this way. If you insist, then take a look at the following query, see how complicated it is that you have to convert varchar to number for arithmetic operation then to have it converted back to varchar in order to concatenate the 'GB' to it:
SQL
update table1 set requested_space =
convert(varchar, (convert(int, substring(reverse(requested_space), 3, len(requested_space))) + 2)) +
'GB';
select * from table1

While it is technically possible, does not mean we have to do it, when it is not efficient.
 
Share this answer
 
v2
Comments
Pranav-BiTwiser 10-May-14 5:16am    
thanx..:)
i have solved it by myself....thanx..:)

update UserRegistration set AllotedSpace = CAST((select SUBSTRING(AllotedSpace,0,2) from UserRegistration where ID=7) + 2 as varchar)+' GB' where ID=7
 
Share this answer
 

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