I am trying to retrieve the largest number from a varchar column that includes both numbers and strings. An example of the data I\'m working with:
BoxNumber
The selected answer worked for me until I added this value to the temp table along with the others in the sample:
insert #bla values('1234')
I expected my max() result to now be 1234, but it remained at 789. Perhaps this is due to some collation setting, but I was able to reproduce on multiple databases. I found this query below worked for me, but I'd certainly be interested to hear if there is a more efficient way of doing this. Also, I did not want to include any decimal values, so I excluded anything with a period as well.
SELECT MAX(CAST(Value AS Int)) FROM #bla
WHERE ISNUMERIC(Value)=1
AND Value NOT LIKE '%[a-z]%'
AND Value NOT LIKE '%.%'