I have created following query in SQL Server 

```
declare @x XML
set @x='<Dataset_Extent>
      <EXTENT_TYPE>Bounding_Polygon</EXTENT_TYPE>
      <Vertex>
        <LON>66.91292909247741</LON>
        <LAT>30.27001012181008</LAT>
        <X>299232</X>
        <Y>3350549</Y>
        <COL>1</COL>
        <ROW>1</ROW>
      </Vertex>
      <Vertex>
        <LON>66.99456841960638</LON>
        <LAT>30.27128639618252</LAT>
        <X>307089.5</X>
        <Y>3350549</Y>
        <COL>15715</COL>
        <ROW>1</ROW>
      </Vertex>
      <Vertex>
        <LON>66.99700791329992</LON>
        <LAT>30.1509623521339</LAT>
        <X>307089.5</X>
        <Y>3337207.5</Y>
        <COL>15715</COL>
        <ROW>26683</ROW>
      </Vertex>
      <Vertex>
        <LON>66.91546772378466</LON>
        <LAT>30.14969219541345</LAT>
        <X>299232</X>
        <Y>3337207.5</Y>
        <COL>1</COL>
        <ROW>26683</ROW>
      </Vertex>
      <Center>
        <LON>66.9549932872921</LON>
        <LAT>30.21048776638499</LAT>
        <X>303160.75</X>
        <Y>3343878.25</Y>
        <COL>7858</COL>
        <ROW>13342</ROW>
      </Center>
    </Dataset_Extent>';
declare @wkt varchar(8000);
select @wkt=CONVERT(varchar(7000),@x.query('distinct-values(
for $lon in /Dataset_Extent/Vertex/LON/text()
for $lat in /Dataset_Extent/Vertex/LAT/text()
return ($lon cast as xs:string?,$lat cast as xs:string?,","))
'));

--concatenate the word POLYGON
set @wkt='POLYGON(('+@wkt +'))';
print @wkt

```

This gives me the following output:

```
POLYGON((66.91292909247741 66.99456841960638 30.27128639618252 66.99700791329992 66.91546772378466 30.27001012181008,30.1509623521339 30.14969219541345))

```

Each Lon/lat pair is not in proper order, I required output in following format:

```
POLYGON((66.91292909247741 30.27001012181008,66.99456841960638 30.27128639618252,66.99700791329992 30.1509623521339,66.91546772378466 30.14969219541345,66.91292909247741 30.27001012181008))

```

The starting vertex Lon/Lat should also repeat at end to make polygon close.

What do I need to do this to fix the problems?