Nvarchar sql query issue

Hi guys

I hope this is someting you can help me with

Basically, I’m looking to query my sql server database using the following query


SELECT MIN(mileage) AS minmileage, MAX(mileage) AS maxmileage
FROM mytable
where make = 'audi'
and mileage > 10000"

So I simply want to display the lowest mileage that’s higher than 1000 in my database. However, the problem I have is that the “mileage” column in my table has a nvarchar format (don’t ask) and I’m not able to change the format of the field.

However, I understand I can do this via the query (CAST or Convert???).

I have looked at so many articles but I just can’t get anything to work. I get results but not as I would expect. For example, I’m getting strange results where the minmileage value for an Audi is 100000 when I know there are Audis with mileages around 25000.

Any help would be fully appreciated

Best regards

Rod from the UK

Maybe like this? (I don’t use sql server)

SELECT 
    MIN(CAST(mileage AS INT)) AS minmileage
  , MAX(CAST(mileage AS INT)) AS maxmileage
FROM mytable
where make = 'audi'
and CAST(mileage AS INT) > 10000

Thanks DarthGuido

This worked perfectly!

Thanks again!

Best regards

Rod from the UK

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.