# Combine/format two fields in query?

**URL:** <https://forum.kirupa.com/t/combine-format-two-fields-in-query/265104>\
**Category:** programming\
**Created:** [July 4, 2008, 12:07am UTC](https://forum.kirupa.com/t/combine-format-two-fields-in-query/265104 "2008-07-04T00:07:39Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Icy\_Penguin](https://avatars.discourse-cdn.com/v4/letter/i/f6c823/32.png) [@Icy\_Penguin](https://forum.kirupa.com/u/Icy_Penguin)\
**Post date:** [July 4, 2008, 12:07am UTC](https://forum.kirupa.com/t/combine-format-two-fields-in-query/265104/1 "2008-07-04T00:07:39Z")

</div>

I have two fields - author\_name\_first and author\_name\_last in my table.

What I’d like to do is combine the two in this format: author\_name\_last, author\_name\_first (to yield something like Hunt, Mike or Baker, Sheldon) - I would do this for sorting purposes (ORDER BY…etc).

Anyone point me in the right direction on this?

Thanks.

---

<div class="post-metadata">

**Author:** ![sekasi](https://avatars.discourse-cdn.com/v4/letter/s/5f8ce5/32.png) [@sekasi](https://forum.kirupa.com/u/sekasi)\
**Post date:** [July 4, 2008, 12:15am UTC](https://forum.kirupa.com/t/combine-format-two-fields-in-query/265104/2 "2008-07-04T00:15:10Z")

</div>

```auto

SELECT author_name_first, author_name_last FROM authors ORDER BY author_name_first DESC, author_name_last DESC

```

---

<div class="post-metadata">

**Author:** ![Icy\_Penguin](https://avatars.discourse-cdn.com/v4/letter/i/f6c823/32.png) [@Icy\_Penguin](https://forum.kirupa.com/u/Icy_Penguin)\
**Post date:** [July 4, 2008, 12:22am UTC](https://forum.kirupa.com/t/combine-format-two-fields-in-query/265104/3 "2008-07-04T00:22:38Z")

</div>

Ah - that works just fine.

Is there a way to combine those fields into a formatted string like I’ve described above anyways?

---

<div class="post-metadata">

**Author:** ![sekasi](https://avatars.discourse-cdn.com/v4/letter/s/5f8ce5/32.png) [@sekasi](https://forum.kirupa.com/u/sekasi)\
**Post date:** [July 4, 2008, 1:21am UTC](https://forum.kirupa.com/t/combine-format-two-fields-in-query/265104/4 "2008-07-04T01:21:19Z")

</div>

um, not sure. try UNION.

---

<div class="post-metadata">

**Author:** ![agnus](https://avatars.discourse-cdn.com/v4/letter/a/4bbf92/32.png) [@agnus](https://forum.kirupa.com/u/agnus)\
**Post date:** [July 4, 2008, 5:09am UTC](https://forum.kirupa.com/t/combine-format-two-fields-in-query/265104/5 "2008-07-04T05:09:32Z")

</div>

Try this:

Select CONCAT(author\_name\_last,’, ',author\_name\_first) AS full\_name FROM my\_table ORDER BY author\_name\_first ASC, author\_name\_last ASC

And after you do your query, the full name would be available in $row[‘full\_name’].

---

<div class="post-metadata">

**Author:** ![Icy\_Penguin](https://avatars.discourse-cdn.com/v4/letter/i/f6c823/32.png) [@Icy\_Penguin](https://forum.kirupa.com/u/Icy_Penguin)\
**Post date:** [July 4, 2008, 6:11am UTC](https://forum.kirupa.com/t/combine-format-two-fields-in-query/265104/6 "2008-07-04T06:11:23Z")

</div>

Perfect - thanks!

---

<div class="post-metadata">

**Author:** ![Charleh](https://avatars.discourse-cdn.com/v4/letter/c/a9a28c/32.png) [@Charleh](https://forum.kirupa.com/u/Charleh)\
**Post date:** [July 4, 2008, 8:50am UTC](https://forum.kirupa.com/t/combine-format-two-fields-in-query/265104/7 "2008-07-04T08:50:55Z")

</div>

Can you not just use a string expression in MySQL? I know you can do it in MSSQL - you can pretty much do any expression you want in a T-SQL statement as long as the syntax is valid

For instance - this works in SQL2000/2005

```auto

SELECT author_name_first + ', ' + author_name_last as full_name FROM authors ORDER BY author_name_first + ', ' + author_name_last

```
