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 |
rsmohankumar
Starting Member
11 Posts |
Posted - 2015-04-13 : 05:20:15
|
Hi, I want the minimum of two values to be display like below without using the case MIN(3.00,4.00) output:3.00Can anyone let me is there any function available to do it. thanks |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2015-04-13 : 06:19:47
|
Without using CASE?If you are in version 2012, you could useIIF(col1<col2,col1,col2)MadhivananFailing to plan is Planning to fail |
|
|
Maithil
Starting Member
29 Posts |
Posted - 2015-04-13 : 07:33:58
|
HiYou need to 2 functions One Function will separates your all values and another will find the Minimum Value among it...Create below two Functions.-----------------------------------------------CREATE FUNCTION [dbo].[NvarcharListToTable] (@list NVARCHAR(MAX), @seperator NVARCHAR(10) = ',')RETURNS @tbl TABLE (number NVARCHAR(MAX))AS BEGIN DECLARE @position int DECLARE @NewLine char(2) DECLARE @no NVARCHAR(MAX) declare @startIndex int set @NewLine = char(13)+char(10) SET @position = 1 SET @list = @list + @seperator set @startIndex = charindex(@seperator,@list,@position) WHILE @startIndex <> 0 BEGIN SELECT @no = substring(@list, @position, @startIndex - @position) IF @no <> '' INSERT into @tbl values(@no) SET @position = @startIndex + len(replace(@seperator,' ','C')) set @startIndex = charindex(@seperator,@list,@position) END update @tbl set number = Replace(number , @NewLine,'') WHERE number like '%' + @NewLine +'%' RETURNENDGO-----------------------------------------------CREATE FUNCTION GetMinValue( @Val nvarchar(max))RETURNS floatASBEGIN DECLARE @Result float DECLARE @Table TAble(Value float) INSERT INTO @Table select number from dbo.NvarcharListToTable(@val,',') SET @Result=(select Min(Value) from @table) RETURN @resultENDGO-----------------------------------------------After Creation above two FunctionsRun select dbo.GetMinValue( '13.0,4.0,5.0,1.0,2.0')-----------------------------------------------Hope you will get what you want exactly. |
|
|
|
|
|
|
|