Hi @MohammedMuntazim-1386
the output i'm getting is 102.30 but expected output is 102+(30/60.0)=102.5
Don't you find there is an IF clause IF CHARINDEX(':', @P_Value) > 0
in your function?
This means that when the data you apply does not contain ':' (say '120.30'), the original data will be returned.
If you want this function to work for both 'HH:MM' and 'HH. MM', you can use PATINDEX, instead of CHARINDEX, like this:
ALTER FUNCTION [dbo].[ConvertHHMMToDecimal]
(
@P_Value NVARCHAR(50)
)
RETURNS NVARCHAR(50)
BEGIN
DECLARE @V_Results NVARCHAR(50)
IF PATINDEX('%[:.]%', @P_Value) > 0
BEGIN
SET @V_Results=(SELECT CAST(LEFT(@P_Value,PATINDEX('%[:.]%',@P_Value)-1) AS INT)+CAST(RIGHT(@P_Value,PATINDEX('%[:.]%',REVERSE(@P_Value))-1)AS INT)/60.0);
RETURN @V_Results
END
RETURN @P_Value
END
GO
select [dbo].[ConvertHHMMToDecimal] ('102.30')
Best regards,
LiHong
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.