Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Best way to concatenate two fields having integer datatype. Can you help please?
QtWS25 Last Chance

Best way to concatenate two fields having integer datatype. Can you help please?

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 5 Posters 1.9k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    CEO.
    wrote on 28 Jul 2021, 17:51 last edited by
    #1

    Hello, I am creating a table and I would want two particular fields to concatenate to form a 3rd field. Take for instance:

    class int not null,
    studentNo int unique IDENTITY(100000,1)
    StudentCode

    Class field will be 3-digits integer.

    StudentNo will be automatically generated, starting from 10,000

    StudentCode (I want it to contain the class 3-digits for each student and also the studentNo. It will start with the 3-digits of the class. i.e to concatenate the class value and the studentNo value)

    I try as much to explain because I want explicit suggestion.

    If you are proficient with database (MySql or Microsoft Sql), and you know about this, then kindly help.

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 28 Jul 2021, 17:57 last edited by
      #2

      Hi,

      See the CONCAT string function.

      See here for a simple tutorial on it.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      C 2 Replies Last reply 28 Jul 2021, 18:11
      2
      • S SGaist
        28 Jul 2021, 17:57

        Hi,

        See the CONCAT string function.

        See here for a simple tutorial on it.

        C Offline
        C Offline
        CEO.
        wrote on 28 Jul 2021, 18:11 last edited by
        #3

        @SGaist alright. Thanks.

        1 Reply Last reply
        0
        • S SGaist
          28 Jul 2021, 17:57

          Hi,

          See the CONCAT string function.

          See here for a simple tutorial on it.

          C Offline
          C Offline
          CEO.
          wrote on 28 Jul 2021, 18:43 last edited by
          #4

          @SGaist what I'm asking for is integer data types, not varcharc

          J 1 Reply Last reply 28 Jul 2021, 18:50
          0
          • C CEO.
            28 Jul 2021, 18:43

            @SGaist what I'm asking for is integer data types, not varcharc

            J Offline
            J Offline
            JonB
            wrote on 28 Jul 2021, 18:50 last edited by
            #5

            @CEO
            You can't just "concatenate" integer values, or at least you'd have to define what you mean.

            StudentCode (I want it to contain the class 3-digits for each student and also the studentNo. It will start with the 3-digits of the class. i.e to concatenate the class value and the studentNo value)

            This is presumably a string, made from concatenating the string digit representations. And that is what @SGaist is suggesting SQL CONCAT for. If you don't want a string as the concatenation then what do you want?

            1 Reply Last reply
            1
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 28 Jul 2021, 18:55 last edited by
              #6

              Use CAST to convert.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              2
              • M Offline
                M Offline
                mchinand
                wrote on 28 Jul 2021, 19:31 last edited by
                #7

                You could use a database trigger to automatically populate that 3rd column after an INSERT or UPDATE. Alternatively, since the third value can be generated from other columns you can generate it on the fly in the SELECT statement.

                e.g., SELECT class, studentNo, CONCATENATE(class, studentNo) AS StudentCode from studytable

                You'll probably have to cast the two integers to string/text before the concatenation.

                1 Reply Last reply
                2
                • S Offline
                  S Offline
                  SimonSchroeder
                  wrote on 30 Jul 2021, 08:48 last edited by
                  #8

                  I am not sure if what you are trying to do is a good approach.

                  Lets assume that studentNo is in the range of 10000 to 99999 and you want the combined field to be an integer as well. Then you can multiply the 3-digit classCode by 100000 and add the two: studentCode = classCode*100000+studentNo

                  However, you will run into trouble when studentNo exceeds 99999. You could shift it by one digit, but this will always fail eventually. And it is a design that will be hard to change later.

                  I think this statement is most problematic:

                  I would want two particular fields to concatenate to form a 3rd field.

                  It is not a good idea to put redundancy into your database. You can always cheaply create the concatenated studentCode. There is no reason to have it saved explicitly. Whenever you need the studenCode, just write a query that computes it from the other two fields. This makes it easier to change it in the future when you run out of digits. It is not so easy to keep your database consistent when you need to change that 3rd field in the future.

                  I believe that databases also have views to tables (I have never used them so far) where you could put your 3rd column. Future changes would then only have to change the definition of the view.

                  Short advice: Don't duplicate data in your database. Don't make consistency hard in your databases. Previous answers still apply in how you can write your queries to retrieve the studentCode.

                  C 1 Reply Last reply 1 Aug 2021, 10:37
                  2
                  • S SimonSchroeder
                    30 Jul 2021, 08:48

                    I am not sure if what you are trying to do is a good approach.

                    Lets assume that studentNo is in the range of 10000 to 99999 and you want the combined field to be an integer as well. Then you can multiply the 3-digit classCode by 100000 and add the two: studentCode = classCode*100000+studentNo

                    However, you will run into trouble when studentNo exceeds 99999. You could shift it by one digit, but this will always fail eventually. And it is a design that will be hard to change later.

                    I think this statement is most problematic:

                    I would want two particular fields to concatenate to form a 3rd field.

                    It is not a good idea to put redundancy into your database. You can always cheaply create the concatenated studentCode. There is no reason to have it saved explicitly. Whenever you need the studenCode, just write a query that computes it from the other two fields. This makes it easier to change it in the future when you run out of digits. It is not so easy to keep your database consistent when you need to change that 3rd field in the future.

                    I believe that databases also have views to tables (I have never used them so far) where you could put your 3rd column. Future changes would then only have to change the definition of the view.

                    Short advice: Don't duplicate data in your database. Don't make consistency hard in your databases. Previous answers still apply in how you can write your queries to retrieve the studentCode.

                    C Offline
                    C Offline
                    CEO.
                    wrote on 1 Aug 2021, 10:37 last edited by
                    #9

                    @SimonSchroeder you have a valid point.
                    I will appreciate you kindly write the code out to execute your suggestion. Let me see it plz

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      SimonSchroeder
                      wrote on 2 Aug 2021, 06:58 last edited by
                      #10

                      @CEO said in Best way to concatenate two fields having integer datatype. Can you help please?:

                      I will appreciate you kindly write the code out to execute your suggestion.

                      I can't do any better than what @mchinand already suggested:

                      SELECT class, studentNo, CONCATENATE(class, studentNo) AS StudentCode FROM studytable

                      You'll probably have to cast the two integers to string/text before the concatenation.

                      If you want to use views, I don't have any experience with these myself and don't know the syntax.

                      1 Reply Last reply
                      0

                      9/10

                      1 Aug 2021, 10:37

                      • Login

                      • Login or register to search.
                      9 out of 10
                      • First post
                        9/10
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved