我是使用CREATE FUNCTION的新手。所以,我试图在一个工作查询上使用它来将结果更改为UPPER /小写。这是原始的工作查询。
SELECT
CASE WHEN CHARINDEX('MCDONALD', cust_name) = 0
THEN REPLACE(REPLACE([cust_name],'.',''),'--','-')
ELSE 'MCDONALDS'
END cust_name
FROM job,dbo.cust cust_name
WHERE job.cust_id_bill_to = cust_name.cust_code AND
job.job_id = '44239' AND
job.sub_job_id = ''
我尝试在此处添加CREATE FUNCTION并且出现错误,这是使用该功能的正确方法吗?我错了吗?或者我是否接受了语法上的一些调整?请帮忙。这是我现有的查询和函数,有错误。
CREATE FUNCTION dbo.InitCap (@inStr VARCHAR(8000))
RETURNS VARCHAR(8000)
AS
BEGIN
DECLARE @outStr VARCHAR(8000) = LOWER(@inStr),
@char CHAR(1),
@alphanum BIT = 0,
@len INT = LEN(@inStr),
@pos INT = 1;
-- Iterate through all characters in the input string
WHILE @pos <= @len BEGIN
-- Get the next character
SET @char = SUBSTRING(@inStr, @pos, 1);
-- If the position is first, or the previous characater is not alphanumeric
-- convert the current character to upper case
IF @pos = 1 OR @alphanum = 0
SET @outStr = STUFF(@outStr, @pos, 1, UPPER(@char));
SET @pos = @pos + 1;
-- Define if the current character is non-alphanumeric
IF ASCII(@char) <= 47 OR (ASCII(@char) BETWEEN 58 AND 64) OR
(ASCII(@char) BETWEEN 91 AND 96) OR (ASCII(@char) BETWEEN 123 AND 126)
SET @alphanum = 0;
ELSE
SET @alphanum = 1;
END
RETURN @outStr;
END
SELECT
dbo.InitCap (CASE WHEN CHARINDEX('MCDONALD', cust_name) = 0
THEN REPLACE(REPLACE([cust_name],'.',''),'--','-')
ELSE 'MCDONALDS'
END) cust_name
FROM job,dbo.cust cust_name
WHERE job.cust_id_bill_to = cust_name.cust_code AND
job.job_id = '44321' AND
job.sub_job_id = ''
RESULT = ERROR:关键字“SELECT”附近的语法不正确。 错误代码:156
我做错了什么?