Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8208657
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T09:26:57+00:00 2026-06-07T09:26:57+00:00

I want to update some values within a column based on different situations….. the

  • 0

I want to update some values within a column based on different situations…..
the table has the following details like…..

                    Date               date
                    period             int
                    subcode            varchar(3)
                    status_bits        varchar(100)
                    // status bits resemble an information based code based...the data usually stored are  2343211 where each value in digits represent an information.....

Now i’ve got to update these values based on different dates and periods….
Now considering the java program……

i have stored the details in corresponding variables in java like..

                   java.util.Date date[];
                   int period[];
                   String subcode[];

                   // Here for an index i , they share the same values within a row.....

If i wanna update it in such a way that that i want to change the 5th letter in a varchar for different dates,periods and subcodes(combined)…..
now..i’ve currently performed like this…

     Connection con;
     preparedStatement ps;
     String bitstatus;



     for(i=0; i < noofupdates; i++)
     {
           ps = con.prepareStatement("select status_bits from tablename where Date = ? AND period = ? AND subcode = ? limit 0,1");
           ps.setDate(1,date[i]);
           ps.setInt(2,period[i])'
           ps.setString(3,subcode[i]);

           rs=ps.executeQuery();
           while(rs.next())
           {

                 bitstatus = rs.getString(1);
                 // performed operation to update the bit.....
                 ps=con.prepareStatment("update status_bits correspondind to the same date,field and subcode");

                 ps.executeUpdate();
           }

     }

Now i presume you understand from the program that i want to update status_bits in the table based on different dates and periods where the bit operation is common……Now i really know that these method is hammering lots of queries and seriously affecting mysql performance…So plz help me by providing a much alternate idea to this…….there are around 1000 records to update………

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T09:27:01+00:00Added an answer on June 7, 2026 at 9:27 am

    If you know you want the 5th position of the status_bits string set to a particular value for a given (date,period,subcode), you can avoid the SELECT and perform just an update.

    UPDATED: to properly handle cases where the existing value in status_bits column is less than 4 characters, we want to ensure we are replacing the 5th character, and not the 2nd character when the existing string is only 1 character long. We can use a CASE expression to do the test. When we have at least 4 characters, we can append our (desired) 5th character after the first four characters. Otherwise, we need to “pad out” the existing string to 4 characters, before we append our (desired) 5th character. (Previously, the statement only included the expression that worked for existing strings at least 4 characters in length.)

    UPDATE tablename
       SET status_bits = 
           CASE WHEN CHAR_LENGTH(status_bits >= 4
             THEN CONCAT(LEFT(status_bits,4), ?, SUBSTR(status_bits,6))
             ELSE CONCAT(RPAD(status_bits,4), ? )
             END
     WHERE `Date` = ? AND period = ? AND subcode = ? 
    

    (You can add the LIMIT clause if you don’t want to update all matching rows, but this isn’t needed if you want to update all matching rows. If (date,period,subcode) is unique, you will match at most one row.)

    It’s also possible to set the 5th position of status_bits dependent on other conditions, for example, by adding predicates to the where clause, or a CASE expression (or IF function) in the SET.

    UPDATE tablename
       SET status_bits = CONCAT(LEFT(status_bits,4)
                         , IF(SUBSTR(status_bits,1,1 = '1', ?, SUBSTR(status_bits,5,1))
                         , SUBSTR(status_bits,6))
     WHERE `Date` = ? AND period = ? AND subcode = ? 
    

    is equivalent to:

    UPDATE tablename
       SET status_bits = CONCAT(LEFT(status_bits,4), ?, SUBSTR(status_bits,6))
     WHERE `Date` = ? AND period = ? AND subcode = ? 
       AND SUBSTR(status_bits,1,1) = '1'
    

    This approach will cut in half the number of queries you are running (reduces the number of roundtrips to the database) and avoids you having to return the entire status_bits column.

    (Unfortunately, the SQL text to replace the 5th character of a varchar column is a bit unwieldy, since MySQL doesn’t have the more compact STUFF function that is provided by SQL Server.)


    Update: more complex expression required to handle existing status_bits strings that are less than 4 characters in length…)

    UPDATE tablename
       SET status_bits = 
           CASE WHEN CHAR_LENGTH(status_bits) >= 4
                THEN CONCAT(LEFT(status_bits,4), ? , SUBSTR(status_bits,6))
                ELSE CONCAT(RPAD(status_bits,4,' '), ? )
           END
     WHERE `Date` = ? AND period = ? AND subcode = ?
    

    NOTE: In that SQL statement, the value of the character to be placed in the 5th position will need to be supplied in two separate positional parameters.

    NOTE:

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to update a table to indicate that some rows are parents of
I want to update a column in a table making a join on other
I want to update some file in a server, with an automated script and
Guess that I have a TextView that I want to update it in some
I would appreciate some help with an UPDATE statement. I want to update tblOrderHead
I have a dataset with some 30 records in it. I want to update
I want to interrupt some specific grails domain class events(read,write,delete,update).Is there any hibernate eventlistner
What I want: Update all new commits from server with my local repository in
I have developed a windows forms c# application, i just want update items in
i want to update my sqlite database but i cannot find the way to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.