sql-server - Need to ensure a hyphen between string
问题描述
I need to check the length of character in b/w Procurement and WW: -it should be greater than 1 and if space is present (Procurement ww), replace with hyphen(like Procurement-ww). -if no string is present between (like Procurementww), then add a hyphen in between (like Procurement-ww)
Input:
-------------------------
Procurement-ww13'18
--------------------
Procurement ww13'18
--------------------
Procurementww12'18
--------------------
Procurement ww12'18 abc-as
--------------------
Procurement ww11'18 NON-VMI (copy 5551)
--------------------
I have tried it using Replace and Stuff functions. But i am not able to get the result.
First Sol
declare @versionid int = 123, @versionname varchar(50) = 'Procurement ww12''18'
If (@versionname not like '%-%')
SELECT STUFF(@versionname,12,1,'-')
print @versionname
--Second sol
declare @versionid int = 123, @versionname varchar(50) = 'Procurement ww12''18'
If (@versionname not like '%-%')
SELECT replace(@versionname,'procurement ','Procurement-')
print @versionname
Output:(Hyphen in between 'Procurement' and 'ww')
-------------------------------
Procurement-ww13'18
--------------------
Procurement-ww13'18
--------------------
Procurement-ww12'18
--------------------
Procurement-ww12'18 abc-as
--------------------
Procurement-ww11'18 NON-VMI (copy 5551)
--------------------
解决方案
您可以使用 aSUBSTRING
从ww
以后获取值并将其附加到硬编码Procurement-
,仅当该值尚不存在Procurement-ww
且至少ww
存在 a 时。
DECLARE @Input TABLE (
Input VARCHAR(100),
Fixed VARCHAR(100))
INSERT INTO @Input (Input)
VALUES
('Procurement-ww13''18'),
('Procurement ww13''18'),
('Procurementww12''18'),
('Procurement ww12''18 abc-as'),
('Procurement ww11''18 NON-VMI (copy 5551)'),
('Procurement ww11''18 NON-VMI (copy 5551)'),
('Procurement NON-VMI (copy 5551)')
UPDATE I SET
Fixed = 'Procurement-'
+ SUBSTRING(
I.Input,
CHARINDEX('ww', I.Input),
100)
FROM
@Input AS I
WHERE
I.Input NOT LIKE 'Procurement-ww%' AND
I.Input LIKE 'Procurement%ww%'
SELECT
I.Input,
I.Fixed
FROM
@Input AS I
结果:
Input Fixed
Procurement-ww13'18 NULL
Procurement ww13'18 Procurement-ww13'18
Procurementww12'18 Procurement-ww12'18
Procurement ww12'18 abc-as Procurement-ww12'18 abc-as
Procurement ww11'18 NON-VMI (copy 5551) Procurement-ww11'18 NON-VMI (copy 5551)
Procurement ww11'18 NON-VMI (copy 5551) Procurement-ww11'18 NON-VMI (copy 5551)
Procurement NON-VMI (copy 5551) NULL
如果您在变量检查中需要它:
DECLARE @Input VARCHAR(100) = 'Procurement ww11''18 NON-VMI (copy 5551)'
IF @Input NOT LIKE 'Procurement-ww%' AND @Input LIKE 'Procurement%ww%'
SET @Input = 'Procurement-'
+ SUBSTRING(
@Input,
CHARINDEX('ww', @Input),
100)
SELECT @Input
推荐阅读
- openssl - 是否可以在消息传输过程中截获和剥离消息中的数字签名?
- c# - linq where 语句受复选框值的约束
- php - 删除卡片中的项目后,查看卡片仍然显示
- firebase - 从 Flutter 应用程序发送 FCM 时出现 403,登录用户不是 firebase 帐户所有者
- django - Django:获取 JSONField 的大小?
- android - Android Dagger2错误:包android.app不存在
- javascript - Javascript中的电子商店hackerRank挑战
- firebase - 如何设置强大的 Firebase 规则
- google-cloud-platform - 如何使用 Cloud Billing Budget API 创建启用通知渠道的预算
- c# - 如何从 ASP.NET MVC 中的表中获取每个值?