Click here to Skip to main content
15,911,762 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am stuck with a small project while updating into database.

i have data as " book-0001", "book-0002", "book-0003" in the book table where alpha part would be same and numeric part is auto incremented.
for eg next data could be "copy-0004", "copy-0005"

while updating condition is Only the alphabetic part has to be changed like "book" to "abcd" for all items containing "book" and its numeric part would be same as "0001".
for eg.
after update it should be like
"abcd-0001", "abcd-0002" , "abcd-0003"


any help regarding would be greatly appreciated.


thanks in advance :)
Posted

UPDATE yourtable SET yourcolumn = REPLACE ( yourcolumn , "book" , "abcd" )

http://msdn.microsoft.com/en-us/library/ms186862.aspx[^]
 
Share this answer
 
Comments
Wayne Gaylard 1-Jul-11 4:19am    
Good one!
i have obtained the sub string too. But problem is updating the alpha part only n preserving the numeric part.

while updating only the "book" part should be changed.

like
"book-0001","book-0002"
after
update
"bok1-0001", "bok1-0002"
and the length for alpha part is constant in my case.
and the numerical part is auto incremented.

this data is fetched from datagridview of previous form. N values need to updated in another form.
update query should be run for all item containing "book".

here is snippet of the code to fectch the data from datagrid
SqlConnection cin = new SqlConnection(str);
cin.Open();
SqlCommand cmd = new SqlCommand("select code from book where ISBN='" + selectedrow + "'", cin); //code is "book-0001, book-0002"
// string codebook = cmd.ExecuteScalar().ToString();
SqlDataReader dr = cmd.ExecuteReader();


string codebook = "";
string rate = "";
while (dr.Read() && dr.HasRows)
{
codebook += dr["code"].ToString() + "\n";
}

string alphaedit = codebook.Substring(0, 8);
this.textBox_Bookcode.Text = alphaedit;
string numedit = codebook.Substring(9, 4);
this.textBox_numcode.Text = numedit;

now i updated the code(ie book-0001) as bookcodefull

string bookcodefull = textBox_Bookcode.Text + "-" + numedit;
// textbox_bookcode is textbox= where user input new name for update for cook


while doing so
my result was

"cook-0001"
"cook-0001"

but required is
"cook-0001"
"cook-0002"
 
Share this answer
 
v2
thank you all for the replies.

Replace worked :)
 
Share this answer
 
get the string from right side 4 chars parse it to int and then cocatenate it with 4 chars and increment the parse int.


if u give some code then only we can give the required code or else we can just give u hint
 
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