Hi,

I am using the MS SQL SERVER 2000 with php. I have a table region
which fields are

  1. id char (2),
  2. description varchar(100);

The input of tables are as follow:
Id Description
3 London
4 abc
13 xyz
12 tyx

I need the max value from id which field type is char.

I am using this query

select max(id) as maxValue from region;

but its give me wrong result. Can anybody help me?

    I don't think the "max value" makes any sense here, since we're talking about characters. I'd instead suggest doing an "ORDER BY id DESC" clause and adding a "LIMIT 1" if you only want the first result.

    EDIT: Looks like I wasn't looking at your sample data carefully enough. Is there a reason why the 'id' field is a char field and not a numeric type?

      I agree with bradgrafelman, why have the id as char?
      But as long as it is, using CAST or CONVERT should make your query work, as long as it isn't alpha-numeric. Ain't that familiar with ms sql (yet).

      SELECT MAX(CAST(id AS INT)) maxValue FROM region
      // or
      SELECT MAX(CONVERT(INT,id)) maxValue FROM region
        Write a Reply...