Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
real_pearl
Posting Yak Master
106 Posts |
Posted - 2004-07-23 : 01:43:18
|
| if I have a column, Column_Name and it has valuesColumn_NameT1T2T3....T99....T864HOw I can get the MAX column value i.e., T864. When I run the query select MAX(column_name) from Ait returns T99, while MAX value is T864. How can I get the MAX value? |
|
|
ditch
Master Smack Fu Yak Hacker
1466 Posts |
Posted - 2004-07-23 : 02:05:21
|
| SELECT Column_NameFROM (SELECT TOP 1 Column_Name, LEFT(Column_Name, 1) + RIGHT(REPLICATE('0', 10) + RIGHT(Column_Name, LEN(Column_Name) - 1), 10) as Col2 FROM Table_Name ORDER By Col2 DESC) as aDuane. |
 |
|
|
eyechart
Master Smack Fu Yak Hacker
3575 Posts |
Posted - 2004-07-23 : 02:06:26
|
| I didn't think MAX worked with non-numeric datatypes.Try clipping off the T with some of the SQL string functions and then wrap that with the MAX function. I would code it for you here, but I am too tired to even think about it. Lookup string functions in BOL and check the examples.-ec |
 |
|
|
eyechart
Master Smack Fu Yak Hacker
3575 Posts |
Posted - 2004-07-23 : 02:08:15
|
quote: Originally posted by ditch SELECT Column_NameFROM (SELECT TOP 1 Column_Name, LEFT(Column_Name, 1) + RIGHT(REPLICATE('0', 10) + RIGHT(Column_Name, LEN(Column_Name) - 1), 10) as Col2 FROM Table_Name ORDER By Col2 DESC) as a
Looks like Duane did it for you. -ec |
 |
|
|
real_pearl
Posting Yak Master
106 Posts |
Posted - 2004-07-23 : 03:07:24
|
| Thanks DuaneI did it this waySELECT TOP 1 MAX(column_name) FROM table_name GROUP BY column_name HAVING LEN(column_name) = (SELECT MAX(LEN(column_name)) from table_name) ORDER BY column_name DESC |
 |
|
|
kselvia
Aged Yak Warrior
526 Posts |
Posted - 2004-07-23 : 03:27:43
|
| You shouldn't need the group by or having clause.SELECT TOP 1 column_name FROM table_nameWHERE len(column_name) = (SELECT max(Len(Column_name)) FROM table_name )ORDER BY column_name DESC--KenYour Kung-Fu is not strong. -- 'The Core' |
 |
|
|
|
|
|
|
|