Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Keep order of "field in (list)" 1

Status
Not open for further replies.

jlsmithhartfiel

Programmer
Jan 7, 2002
336
US
Hi!

My query is:
select l.label
from listbox l
where l.fieldname = 'Municipality'
and l.value in ('rams','ak','cret')

I'm looking for the easiest way to display the records in the same order as 'rams','ak','cret'. Currently it sorts them in alphabetical order of l.label.

TIA!


Jessica [ponytails2]
 
and l.value in ('rams','ak','cret')

can u give me what this does (i have not used this sql command), maybe if i know what u want i can help u.

Known is handfull, Unknown is worldfull
 
If you are using SQL Server then this will work:

Code:
SELECT label
FRO listbox
WHERE fieldname = 'Municipality'
  AND value in ('rams','ak','cret')
ORDER BY CASE value
  WHEN 'rams' THEN 1
  WHEN 'ak' THEN 2
  WHEN 'cret' THEN 3
  END

--James
 
Thanks for the example James! I think that will work for me. It's actually in a asp page, so I dynamically build the sql query (probably should have included that info :) ). The "in" list comes from another page, but I should be able to do this:

Code:
if sMunicipalities <> &quot;&quot; then
  dim tmpArray, iCtr
  tmpArray = Split(sMunicipalities, &quot;,&quot;)
  sql = &quot;select l.label &quot; _
      & &quot;from listbox l &quot; _
      & &quot;where l.fieldname = 'Municipality' &quot; _
      & &quot;and l.value in (&quot; & sMunicipalities & &quot;) &quot; _
      & &quot;order by case l.value &quot;
  for iCtr = 0 to ubound(tmpArray)
    sql = sql _
        & &quot; when &quot; & tmpArray(iCtr) & &quot; then &quot; & iCtr
  next
  sql = sql & &quot; end&quot;
  objData.Open sql
  Do While Not objData.EOF and Len(sMList) < 25
    if sMList <> &quot;&quot; then
      sMList = sMList + &quot;,&quot;
    end if
    sMList = sMList + objData(&quot;label&quot;)
    objData.MoveNext
  Loop
  objData.Close
  sMList = mid(sMList,1,25)
end if

Jessica [ponytails2]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top