Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors
podcast
Filter by Categories
ArcGIS Pro
GDAL
GeoJson
Map
Python
QGIS
Uncategorized

Concatenate fields in QGIS

How to concatenate fields in QGIS

To concatenate fields in QGIS, you can use the Field Calculator to create a new field or update an existing one by combining the values from multiple fields.

Working in QGIS? Listen to our podcast!

Here’s a step-by-step guide on how to do it:

  1. Open the Attribute Table: Open the layer’s attribute table where you want to concatenate fields.
  2. Open the Field Calculator: Click on the “Field Calculator” button. This opens a dialog box where you can define the calculation for the new or existing field.
  3. Create or Update a Field:
  • To create a new field, check the “Create a new field” option.
  • To update an existing field, select the field from the “Field name” drop-down menu.
  1. Define the Field Name and Type (if creating a new field):
  • Enter a name for the new field.
  • Choose the output field type (e.g., String, Integer). For concatenation, you usually want to select “String”.
  • Specify the field length (e.g., 255).
  1. Enter the Expression for Concatenation:
  • Use the concat() function to join fields. For example, if you want to concatenate fields named FirstName and LastName with a space in between, your expression would be:
    concat("FirstName", ' ', "LastName")
  • You can also use the || operator for concatenation, like:
    "FirstName" || ' ' || "LastName"
  • If you want to handle possible NULL values gracefully, you might consider using the concat_ws() function, which skips NULLs:
    concat_ws(' ', "FirstName", "LastName")
  1. Click OK: Once your expression is set, click OK. QGIS will execute the calculation and update your layer’s data accordingly.
  2. Save Changes (if needed): If your layer is editable and changes are not auto-saved, make sure to save the changes.

This approach allows you to flexibly combine data from multiple fields into a single field, which can be useful for labeling, exporting, or further analysis.

Here are more examples of how to concatenate fields in QGIS using different scenarios:

Concatenate Date and Time Fields:
If you have separate date and time fields and want to combine them into a single datetime string, you can use:

       concat(to_string("DateField"), ' ', "TimeField")

    This expression assumes your date is already in a string format that can be concatenated directly with the time.

    Concatenate Address Components:
    To create a full address from separate fields like street, city, state, and postal code, you might use:

         concat_ws(', ', "Street", "City", "State", "PostalCode")

      The concat_ws() function (with a comma and space as separators) ensures that missing components (null values) do not disrupt the formatting.

      Concatenate with a Conditional:
      If you want to concatenate fields but include certain elements only if they are not null, you can use an IF statement:

           concat("FirstName", ' ', IF("MiddleName" IS NOT NULL, concat("MiddleName", ' '), ''), "LastName")

        This adds the middle name with a preceding space if it exists.

        Concatenate and Convert Non-String Fields:
        To concatenate fields that are not strings, you must first convert them. For example, combining an ID number with a name field:

             concat("FirstName", ' - ID: ', to_string("IDNumber"))

          This uses to_string() to convert the numeric ID to a string so it can be concatenated with text.

          Concatenate with a Line Break:
          If you are preparing data for display or output where a line break is needed:

               concat("Name", '\n', "Address")

            In this example, \n inserts a newline between the name and address, which can be useful for labeling in maps.

            Frequently asked questions about concatenating fields in QGIS:

            How do I concatenate fields with a delimiter but avoid delimiter with NULL values?
            To concatenate fields with a delimiter while avoiding the delimiter when encountering NULL values, use the concat_ws() function. This function stands for “concatenate with separator” and automatically skips NULL fields. For example:

                 concat_ws(', ', "Field1", "Field2", "Field3")

              In this case, if any of the fields (“Field1”, “Field2”, “Field3”) are NULL, they are omitted, and the delimiter (comma) is only inserted between non-NULL fields.

              Can I concatenate fields of different data types?
              Yes, you can concatenate fields of different data types by converting all fields to strings. Use the to_string() function to convert numeric or date fields to string type. For instance:

                   concat(to_string("NumericField"), ' ', "StringField")

                This is necessary because concatenation requires that all fields be of the same type, specifically strings.

                How can I handle NULL values when concatenating fields?
                Use the coalesce() function to handle NULL values effectively. This function returns the first non-NULL value in the list of its arguments. For example:

                     concat(coalesce("Field1", 'Default1'), ' ', coalesce("Field2", 'Default2'))

                  Here, if “Field1” or “Field2” is NULL, ‘Default1’ or ‘Default2’ will be used respectively.

                  Is it possible to concatenate fields conditionally based on other field values?
                  Yes, you can use conditional expressions such as IF or CASE to concatenate fields based on conditions. For example:

                       concat(IF("Field1" > 10, "Field1", 'Not applicable'), ' ', "Field2")

                    This expression will concatenate “Field1” with “Field2” only if “Field1” is greater than 10; otherwise, it concatenates ‘Not applicable’ with “Field2”.

                    How do I update an existing field with concatenated values?
                    To update an existing field, open the Field Calculator, ensure the “Create a new field” option is unchecked, and select the field you want to update from the “Field name” dropdown. Enter your concatenation expression and click OK. This will overwrite the existing field with the new concatenated values.

                    What if I need to concatenate a large number of fields? Is there a more efficient way?
                    For concatenating many fields, especially in a dynamic or repetitive scenario, consider using a scripting approach with PyQGIS. This Python interface allows you to automate tasks in QGIS, including concatenation across multiple fields and layers, by writing scripts that perform the concatenation programmatically.

                    How can I concatenate fields and format the result, like adding line breaks or tabs?
                    You can use special characters like \n for a newline or \t for a tab within your concatenation expressions to format the output:

                         concat("Field1", '\n', "Field2")

                      This adds a line break between the values from “Field1” and “Field2”.

                      Can I use the concatenated fields as labels directly on the map?
                      Yes, concatenated fields can be directly used for labeling on the map. In the Layer Properties under the Labels section, set the label expression to your concatenated field. This allows for dynamic labeling based on your concatenated data.

                      How to ensure the concatenated string fits within a specified field length?
                      When creating a new field to store concatenated values, be mindful of the maximum field length. If necessary, use the left() function to truncate fields:

                           concat(left("Field1", 10), ' ', left("Field2", 10))

                        This ensures that each field contributes no more than 10 characters, preventing overflow.

                        Are there performance considerations when concatenating fields on large datasets?
                        While concatenation isn’t typically resource-intensive, performance can degrade with large datasets, especially if using complex expressions or doing the operation on many records. To improve performance, consider performing concatenations during less busy times, or preprocess data to simplify operations.

                          About the Author
                          I'm Daniel O'Donohue, the voice and creator behind The MapScaping Podcast ( A podcast for the geospatial community ). With a professional background as a geospatial specialist, I've spent years harnessing the power of spatial to unravel the complexities of our world, one layer at a time.