需要帮助才能创建一个返回TRUE或FALSE的函数。 TRUE - 如果输入1或3个单词(如'__ hello _','_ hello','_ hello my frend' - 空格应该被删除),如果条件未满足则为FALSE
CREATE FUNCTION dbo.nazvFac(@f nvarchar(30))
RETURNS bit
AS
BEGIN
DECLARE @l int = 1, @s nvarchar(30), @i int = 0, @b bit
WHILE LTRIM(RTRIM(LEN(@f))) >= @l --error here, but I do not know how to fix it
BEGIN
SET @s = SUBSTRING(@f, @l, 1)
IF @s BETWEEN 'А' AND 'я'
SET @l += 1
ELSE IF @s = ' '
BEGIN
SET @l -= 1
SET @s = SUBSTRING(@f, @l, 1)
SET @s = RTRIM(@s)
SET @l += 2
SET @i += 1
END
ELSE
BREAK
END
IF @i = 0 OR @i = 2
SET @b = 'TRUE'
ELSE
SET @b = 'FALSE'
RETURN @b
END
GO
答案 0 :(得分:2)
WHILE LTRIM(RTRIM(LEN(@f))) >= @l --error here, but I do not know how to fix it
LEN()
返回一个int,然后将其传递给字符串函数:RTRIM()
。
答案 1 :(得分:1)
你应修剪字符串,然后检查长度。
CREATE FUNCTION dbo.nazvFac(@f NVARCHAR(30))
RETURNS BIT
AS
BEGIN
DECLARE @l INT = 1, @s NVARCHAR(30), @i INT = 0, @b BIT
WHILE LEN(LTRIM(RTRIM(@f))) >= @l --I think youn need to trim the string and then check length
BEGIN
SET @s = SUBSTRING(@f, @l, 1)
IF @s BETWEEN 'А' AND 'я'
SET @l += 1
ELSE IF @s = ' '
BEGIN
SET @l -= 1
SET @s = SUBSTRING(@f, @l, 1)
SET @s = RTRIM(@s)
SET @l += 2
SET @i += 1
END
ELSE
BREAK
END
IF @i = 0 OR @i = 2
SET @b = 'TRUE'
ELSE
SET @b = 'FALSE'
RETURN @b
END
GO
答案 2 :(得分:1)
只有在有一个或三个单词时才想返回TRUE?这应该这样做:
CREATE FUNCTION dbo.nazvFac(@f NVARCHAR(30))
RETURNS BIT
AS
BEGIN
DECLARE @l INT, @b BIT
SET @l = LEN(@f) - LEN(REPLACE(@f, ' ', '')) + 1
IF @l == 1 OR @l == 3
SET @b = 'TRUE'
ELSE
SET @b = 'FALSE'
RETURN @b
END
另外,JC。关于len()错误是正确的。