Solved

Problem in selecting max of a varchar datatype value

Posted on 2010-11-17
10
321 Views
Last Modified: 2012-05-10
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
0
Comment
Question by:rajeshmarappan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +2
10 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 34155845
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
 
LVL 56

Expert Comment

by:HainKurt
ID: 34155916
or if they all start with 'max'

select max(cast(replace(col,'max','') as int)) from yourTable
0
 
LVL 23

Expert Comment

by:Rajkumar Gs
ID: 34155941
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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
LVL 93

Expert Comment

by:Patrick Matthews
ID: 34156257
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
 

Author Comment

by:rajeshmarappan
ID: 34156314
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
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 34156354
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
 
LVL 23

Expert Comment

by:Rajkumar Gs
ID: 34156451
Right.
Things will be easy, if you have one more integer column so that you can find the maximum value

Raj
0
 
LVL 56

Accepted Solution

by:
HainKurt earned 250 total points
ID: 34156591
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
 
LVL 56

Assisted Solution

by:HainKurt
HainKurt earned 250 total points
ID: 34156648
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
 

Author Closing Comment

by:rajeshmarappan
ID: 34164725
Partially Solved my problem
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question