Question: I have a table with a list of folders and I want to return only the top level folders.

For example, if the original table (tblFolders) has a single column (Fldrs) as below:

**Fldrs**

```
C:\Folder1
C:\Folder1\Subfolder1
C:\Folder1\Subfolder2
C:\Folder2\Subfolder1

```

I'd like to return:

```
C:\Folder1
C:\Folder2\Subfolder1

```

Thanks in advance

Answer: 
A standard SQL solution is to UNION all your dates

```
SELECT MyID, MAX(thedate ) AS maxdate, MIN(thedate ) AS mindate
FROM
(
SELECT MyID, Date1 AS thedate FROM table
UNION ALL
SELECT MyID, Date2 AS thedate FROM table
UNION ALL 
SELECT MyID, Date3 AS thedate FROM table
UNION ALL
SELECT MyID, Date4 AS thedate FROM table
) T
GROUP BY MyID

```

There might be better other solutions with window functions, depending on your RDBMS, which you haven't specified. But this one should work with any RDBMS.
Answering my own question here - after a fair amount of digging I don't think there is a good existing solution for this. So I ended up writing my own functions to handle this.

Here is the code for those functions:

```
CREATE FUNCTION dbo.maxDate(@date1 DATETIME, @date2 DATETIME) RETURNS DATETIME AS
BEGIN
    DECLARE @result DATETIME
    IF @date1 > @date2 SET @result = @date1
    ELSE SET @result = @date2
    RETURN @result
END
go
CREATE FUNCTION dbo.minDate(@date1 DATETIME, @date2 DATETIME) RETURNS DATETIME AS
BEGIN
    DECLARE @result DATETIME
    IF @date1 < @date2 SET @result = @date1
    ELSE SET @result = @date2
    RETURN @result
END
go

```

This then allows for a query similar to the second suggestion in my question:

```
SELECT
    MyID,
    dbo.minDate(Date1, dbo.minDate(Date2, dbo.minDate(Date3, Date4))),
    dbo.maxDate(Date1, dbo.maxDate(Date2, dbo.maxDate(Date3, Date4)))
FROM ...

```