Problem in selecting max of a varchar datatype value

Hi,

I have a table in sqlserver 2008 with a auto generated column as varchar. these are the sample values
max1
max2
max3
.
.
.
.
max98
max99
max100

when i try to select the max value of the above column, it give only as max99 but it should give us max100.

please let me know an solution to find out max of a varchar datatype value

thanks,
Rajesh
rajeshmarappanAsked:
Who is Participating?
 
HainKurtConnect With a Mentor Sr. System AnalystCommented:
you can use one of these (not tested)

col.Remove(0,3) * 1

col.Replace('max','') * 1

col.Substring(3) * 1

so it removes first 3 letters 'max', then multiplies it with 1 makes it (or should make number) then sort it...

check here for string functions: http://msdn.microsoft.com/en-us/vbasic/bb737939.aspx#strsubst
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
that column should really be splitted into 2 columns ...

apart from that:
select max( cast( substring(yourfield, 4, 100) as bigint) )
  from yourtable

Open in new window

0
 
HainKurtSr. System AnalystCommented:
or if they all start with 'max'

select max(cast(replace(col,'max','') as int)) from yourTable
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
Rajkumar GsSoftware EngineerCommented:
You need one more column to indicate the number.

For current data, if 'max' is hardcoded, you can try this query as well

select 'max' + cast( max( cast( right(yourcolumn, len(yourcolumn) - 3) as int)) as varchar(25)) from yourtable

Open in new window


Regards
Raj
0
 
Patrick MatthewsCommented:
Rajesh,

>>please let me know an solution to find out max of a varchar datatype value

Actually, in a text comparison, 'max99' > 'max100'.

Just as 'max2' > 'max100'

:)

Patrick
0
 
rajeshmarappanAuthor Commented:
Hi,

Above solutions are working fine. but we need the same solution for linq query where we cant use CAST. please let me know.

Regards,
Rajesh
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
not sure if that can be done in LINQ ...

so basically, I come back to my initial post:

that column should really be splitted into 2 columns ...
0
 
Rajkumar GsSoftware EngineerCommented:
Right.
Things will be easy, if you have one more integer column so that you can find the maximum value

Raj
0
 
HainKurtConnect With a Mentor Sr. System AnalystCommented:
or do this

('0000000' + col.Replace('max','000')).Substring(10-col.Length)

the idea is convert

max1     : 0000000001
max23   : 0000000023
max100 : 0000000100

so sorting will be correct without casting ;)

0
 
rajeshmarappanAuthor Commented:
Partially Solved my problem
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.