Blog Sections Open
Using @SELECT TVs While Keeping Empty Values at the Top
A small SQL pattern that keeps an empty first option in an Evolution CMS @SELECT TV while still sorting the rest of the list properly.
An @SELECT TV is a simple and powerful way to feed option lists from the database, but it becomes awkward when editors also need an empty first value such as “choose one” or a blank placeholder.
The original query was straightforward:
@SELECT pagetitle, id
FROM [+PREFIX+]site_content
WHERE parent=0 AND published=1 AND deleted=0
ORDER BY pagetitle ASC
Then an empty row was added through UNION:
@SELECT '','' UNION
SELECT pagetitle, id
FROM [+PREFIX+]site_content
WHERE parent=0 AND published=1 AND deleted=0
ORDER BY pagetitle ASC
Why the result can look wrong
As soon as the artificial empty row is added, the final ordering may no longer behave the way you expect. Depending on the SQL engine and how the query is parsed, the empty row and the sorted rows compete in one combined result set.
A cleaner pattern
Wrap the sorted part in a subquery or use a synthetic sort column so the blank option is always first and the real values remain alphabetized after it.
@SELECT title, value FROM (
SELECT '' AS title, '' AS value, 0 AS sort_order
UNION ALL
SELECT pagetitle AS title, id AS value, 1 AS sort_order
FROM [+PREFIX+]site_content
WHERE parent=0 AND published=1 AND deleted=0
) AS t
ORDER BY sort_order ASC, title ASC
Why this is safer
The intent becomes explicit: one blank option first, then real records in alphabetical order. That is easier to maintain than relying on how a bare UNION happens to sort the final result.
For Evo projects with many editor-facing TVs, small details like this make forms much less frustrating to use.
Using Snippet Output as a TV Parameter in Evolution CMS
How to feed the result of a snippet into a TV or listing parameter when a plain stored value is not enough for the final frontend output.
Building Category Select Filters with autoFilter
How to output category options in an autoFilter select, submit them cleanly, and feed the resulting resource IDs into a Ditto listing.