[英]SQL Replace Function and NULLS
This should be simple but I can figure it out for the life of me. 这应该很简单,但我可以为自己的一生解决。 Basically I have a stored procedure that updates a database.
基本上,我有一个更新数据库的存储过程。 In the database I have forename and surname columns and the stored procedure is doing the business logic.
在数据库中,我有“姓氏”和“姓氏”列,并且存储过程正在执行业务逻辑。 What I want to be able to do is if someone tries to enter multiple names it identifies that based on a series of replaces statements and returns NULL if it finds any, for example like in the code below it is looking for a "/" (as if it was looking for someone doing something like John / Jill) just like in the sample code below:
我想要做的是,如果有人尝试输入多个名称,它会根据一系列的replaces语句来标识该名称,如果找到任何名称,则返回NULL,例如,在下面的代码中,它正在寻找“ /”(就像在寻找某人做类似John / Jill的事情一样,就像下面的示例代码中一样:
Declare @Forename Varchar(50)
Set @Forename = 'John'
Select Replace(@Forename,'/',NULL)
However instead of retuning 'John' it returns NULL even though there is no / present. 但是,即使没有/存在,也不会返回“ John”,而是返回NULL。 I've had a google about but I can't find anything.
我有一个有关的谷歌,但我找不到任何东西。 Of course in my real code there are many nested replace statements but this is just to show you what the issue is i am up against.
当然,在我的实际代码中,有许多嵌套的replace语句,但这只是向您显示我所面对的问题。 Any help would be great.
任何帮助都会很棒。
Thanks in advance 提前致谢
NULL
in almost any operation returns NULL
, so the result is not surprising. NULL
在几乎任何操作返回NULL
,所以结果并不奇怪。 Probably the simplest way to do what you want in SQL Server is: 在SQL Server中执行所需操作的最简单方法可能是:
Select left(@Forename, charindex('/', @Forename + '/') - 1)
The charindex()
finds the /
if any -- the + '/'
ensures that it finds a slash. charindex()
找到/
如果有)- + '/'
确保找到斜杠。 The left()
then chooses all characters up to the one before. 然后,
left()
选择所有字符,直到之前的一个。
As per documentation: 根据文档:
Returns NULL if any one of the arguments is NULL.
如果任何一个参数为NULL,则返回NULL。
https://msdn.microsoft.com/en-us/library/ms186862.aspx https://msdn.microsoft.com/en-us/library/ms186862.aspx
EDIT: 编辑:
If I understand your original requirement correctly, you need something like this: 如果我正确理解了您的原始要求,则需要以下内容:
select case when @forename like '%[/]%' then null else @forename end
Here you can extend the '%[/]%'
with additional "separator" characters. 在这里,您可以使用其他“分隔符”字符扩展
'%[/]%'
。
You have to create custom Split method in SQL, which will split your name string by '/' delimiter. 您必须在SQL中创建自定义的Split方法,该方法将使用'/'分隔符来分隔名称字符串。 If your @Forename string has '/' in it.
如果您的@Forename字符串中包含“ /”。
Below is the sample code for Split method : 下面是Split方法的示例代码:
CREATE FUNCTION [dbo].[SplitString]
(
@string NVARCHAR(MAX),
@delimiter CHAR(1)
)
RETURNS @output TABLE(splitdata NVARCHAR(MAX)
)
BEGIN
DECLARE @start INT, @end INT
SELECT @start = 1, @end = CHARINDEX(@delimiter, @string)
WHILE @start < LEN(@string) + 1 BEGIN
IF @end = 0
SET @end = LEN(@string) + 1
INSERT INTO @output (splitdata)
VALUES(SUBSTRING(@string, @start, @end - @start))
SET @start = @end + 1
SET @end = CHARINDEX(@delimiter, @string, @start)
END
RETURN
END
GO
Usage : 用法:
Select * from dbo.SplitString(@Forename,'/')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.