Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a table with char datatype for a column. Need to fetch max number from that column.

some values in that column are 1,2,3,A,B

while fetching max(column) I only get the char - 'B', how do i get the max among numeric value?
SQL
select max(cast(floor as int)) from floormaster
Posted
Comments
[no name] 7-Mar-13 21:38pm    
It would appear that you have a basic misunderstanding about data types. Just because you store a 1 in a char column does not mean that it is a number. B is the correct answer. B is 66 and 3 is 51. If you want to do things like pick out the highest numeric value then store the values as numbers not as strings.

Just use this:


select MAX(floor) from floorMaster where ISNUMERIC(floor)=1
 
Share this answer
 
Comments
gvprabu 7-Mar-13 23:47pm    
The values are combination of Numbers and Characters(1,2,3,A,B) in single column, So ISNUMERIC() will return always return '0' in case if any Letters are there. So we need to do loop
Davidduraisamy 7-Mar-13 23:49pm    
If it is like that then have to split the values and use ISNUMERIC function..
gvprabu 7-Mar-13 23:53pm    
Check my Post :-)
Hi ,

Try this

SQL
-- Split Function
IF OBJECT_ID('fnSplit') IS NOT NULL DROP FUNCTION dbo.fnSplit
GO
CREATE FUNCTION dbo.fnSplit(
    @InputString VARCHAR(8000), -- List of delimited items
    @Delimiter CHAR(1) = ',') -- delimiter that separates items)
RETURNS @List TABLE (ColumnValues VARCHAR(8000))
AS
BEGIN
    DECLARE @NextString NVARCHAR(40), @Pos INT

    SET @InputString = @InputString + @Delimiter
    SET @Pos = CHARINDEX(@Delimiter,@InputString)

    WHILE (@pos <> 0)
    BEGIN
	   SET @NextString = SUBSTRING(@InputString,1,@Pos - 1)
	   INSERT INTO @List(ColumnValues) SELECT @NextString 
	   SET @InputString = SUBSTRING(@InputString,@pos+1,len(@InputString))
	   SET @pos = CHARINDEX(@Delimiter,@InputString)
    END 
    RETURN 
END
GO
-- Single Value 
DECLARE @Values VARCHAR(100)='1,2,3,A,B,34,40'
SELECT MAX(ColumnValues) FROM fnSplit(@Values,',') WHERE ISNUMERIC(ColumnValues)=1
-- For Tables
SELECT (SELECT MAX(ColumnValues) FROM fnSplit(Column_Name,',') WHERE ISNUMERIC(ColumnValues)=1)
FROM Table_Name
 
Share this answer
 
You can use cross apply and a custom split string function to achieve that.
SQL
CREATE FUNCTION  [dbo].[SplitString]
(
	@stringToSplit VARCHAR(MAX)
)
RETURNS @returnList TABLE ([Name] [nvarchar] (500))
AS
BEGIN
	DECLARE @name NVARCHAR(255)
	DECLARE @pos INT
	
	WHILE CHARINDEX(',', @stringToSplit) > 0
	BEGIN
	    SELECT @pos = CHARINDEX(',', @stringToSplit)  
	    SELECT @name = SUBSTRING(@stringToSplit, 1, @pos -1)
	    
	    INSERT INTO @returnList
	    SELECT @name
	    
	    SELECT @stringToSplit = SUBSTRING(@stringToSplit, @pos + 1, LEN(@stringToSplit) -@pos)
	END
	
	INSERT INTO @returnList
	SELECT @stringToSplit
	
	RETURN
END

SQL
create table floormaster(floor char(10));
insert floormaster(floor) values('1,2,3,A,B');
insert floormaster(floor) values('5,7,3,A,B'); 
select max(Name) from floormaster f cross apply dbo.SplitString(f.floor) where ISNUMERIC(Name) = 1;

The result will be 7
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900