Question : Convert to a SQL function

I need to convert a VBA function to SQL Server function. Please help me save 2 hours of work:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
Public Function Tick(Px As Double) As String
Dim intTicks As Integer
Dim strTicks As String
Dim intHandl As Integer
Dim intEights As Integer
Dim intSign As Integer
    If Px < 0 Then
      intSign = -1
    Else
      intSign = 1
    End If

    Px = intSign * Px
    
    intHandl = Int(Px)
    intTicks = Int((Px - intHandl) * 32)
    intEights = CInt(((Px - intHandl) * 32 - intTicks) * 8)
    If intSign < 0 Then
        intHandl = "-" & intHandl
    End If
    If intTicks < 10 Then
        strTicks = "0" & intTicks
    End If
    
    Tick = intHandl & "-" & strTicks & ":" & intEights
End Function

Answer : Convert to a SQL function

This does not work exactly as you have it, as it depends on whether you want the intEights to round up or round down

I have it rounding down, and the function above has it rounding up. It can be easily changed.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
CREATE FUNCTION Tick (
  @px MONEY
) RETURNS VARCHAR(20) AS BEGIN

  DECLARE @result    VARCHAR(20)
  DECLARE @intTicks  INTEGER
  DECLARE @strTicks  VARCHAR(10)
  DECLARE @intHandl  INTEGER
  DECLARE @intEights INTEGER
  DECLARE @intSign   INTEGER

  
  SET @intSign = CASE WHEN @px < 0 THEN -1 ELSE 1 END
  
  SET @px = ABS(@px)
  SET @intHandl = FLOOR(@px)
  SET @intTicks = (@Px - @intHandl) * 32
  SET @intEights = CAST((((@Px - @intHandl) * 32 - @intTicks) * 8) AS INTEGER)
  SET @intHandl = @intHandl * @intSign
  SET @strTicks = RIGHT('00' + CAST(@intTicks AS VARCHAR(10)), 2)
  SET @result = CAST(@intHandl AS VARCHAR(10)) + '-' + @strTicks + ':' + CAST(@intEights AS VARCHAR(10))
  RETURN CASE WHEN @intSign < 0 THEN '-' ELSE '' END + @result
END
Random Solutions  
 
programming4us programming4us