Solved

Problem in selecting max of a varchar datatype value

Posted on 2010-11-17
10
319 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 53

Expert Comment

by:Huseyin KAHRAMAN
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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
LVL 92

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 53

Accepted Solution

by:
Huseyin KAHRAMAN 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 53

Assisted Solution

by:Huseyin KAHRAMAN
Huseyin KAHRAMAN 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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
(sql serv16)ssis 2016 question/check 1 127
Can I skip a node in XML? 9 46
SQL Select Query help 1 49
How to create and use encrypted columns in SQL 2005? 15 63
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 SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

734 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