Here is my code:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE OR ALTER FUNCTION [dbo].[FormatExtractNumber]
(@RawValue sql_variant)
RETURNS nvarchar(50)
AS
BEGIN
DECLARE @FormatExtractNumber nvarchar(50)
IF (SQL_VARIANT_PROPERTY(@RawValue,'scale') = 0 or @RawValue > 100000000000000000000000000000)
BEGIN
SET @FormatExtractNumber = CONVERT(nvarchar(50),@RawValue)
END
ELSE
BEGIN
DECLARE @Raw_dec dec(38,7)
SET @FormatExtractNumber = CONVERT(nvarchar(50), @Raw_dec)
SET @FormatExtractNumber = CASE WHEN @Raw_dec = 0 THEN N'0' ELSE CASE WHEN CHARINDEX('.',@FormatExtractNumber) > 0 THEN TRIM(TRAILING '0' FROM @FormatExtractNumber) ELSE @FormatExtractNumber END END
END
IF CHARINDEX(N'.',REVERSE(@FormatExtractNumber)) = 1
BEGIN
SET @FormatExtractNumber = REPLACE(@FormatExtractNumber, '.', '')
END
RETURN @FormatExtractNumber
END
I am getting the following error and I don't know why:
Msg 102, Level 15, State 1, Procedure FormatExtractNumber, Line 72 [Batch Start Line 9]
Incorrect syntax near '0'.
Msg 156, Level 15, State 1, Procedure FormatExtractNumber, Line 80 [Batch Start Line 9]
Incorrect syntax near the keyword 'END'.
Msg 102, Level 15, State 1, Procedure FormatExtractNumber, Line 83 [Batch Start Line 9]
Incorrect syntax near 'END'
I have isolated the problem to the following line as, when I comment this part, I can get the function installed:
SET @FormatExtractNumber = CASE WHEN @Raw_dec = 0 THEN N'0' ELSE CASE WHEN CHARINDEX('.',@FormatExtractNumber) > 0 THEN TRIM(TRAILING '0' FROM @FormatExtractNumber) ELSE @FormatExtractNumber END END
2条答案
按热度按时间x759pob21#
compatibility level for the database had to be changed to SQL 2022. My partner had made changes to his db after he upgraded SQL Server there. He then ran the script there with no issues. I then upgraded SQL server from 2019 to 2022 here but had not changed compatibility level on the DB yet here.
dtcbnfnu2#
Remove ELSE CASE after N'0'. Check syntax the CASE statement.