[英]pattern matching in sql , how to do it?
so far i have looked up for all relevant info regarding pattern matching in sql but nothing helped me, i am receiving a Code in sms like "yyyy/mm-digit" (digit increments with every message). 到目前为止,我一直在查找有关sql中模式匹配的所有相关信息,但没有任何帮助,我收到了诸如“ yyyy / mm-digit”(每条消息的数字递增)之类的短信代码。 so i want to ensure that the code sent by user is exactly in that format otherwise another action. 因此,我想确保用户发送的代码完全采用该格式,否则将采取其他措施。 So how ? 又怎样 ?
Here is my old code to check for just numeric code but it want it for eg "2014/08-digit" kinda pattern , how ? 这是我的旧代码,仅用于检查数字代码,但它希望用于例如“ 2014/08位数”的样板模式,怎么办? (it changes with respect to year and month) (根据年月改变)
ALTER TRIGGER [dbo].[T_InsertAfterRcvdMessage]
ON
[dbo].[ReceivedMessages]
AFTER INSERT
AS
Declare @msg as varchar(20)
Declare @inmbl as varchar(20)
Declare @cid as int
Declare @ComplainantId as int
BEGIN TRY
SELECT @msg = [Message], @inmbl = FromMobileNo FROM inserted
Set @cid = ISNUMERIC(@msg)
if (@cid = 1) -- it means @msg is numeric value
BEGIN
Declare @sts as varchar(50)
Declare @mbl as varchar(20)
(SELECT @sts = Status, @mbl = ContactNo, @ComplainantId = ComplainantID FROM CPOCMS.dbo.View_ComplaintStatusInfo WHERE ComplaintID = @msg)
Exec CmsSMSDb.dbo.ADD_SMS_InQueue @mbl,@sts,0,0,2,1,0,null,@ComplainantId
END
--else
-- BEGIN
-- Exec CmsSMSDb.dbo.ADD_SMS_InQueue @inmbl,'Please Provide Valid Complaint No'
-- END
END TRY
BEGIN CATCH
END CATCH
If you are trying to match "2014/08-", you can use regular expressions or SQL Server built in patterns. 如果尝试匹配“ 2014 / 08-”,则可以使用正则表达式或SQL Server内置模式。 With the built in patterns, you would look for: 使用内置模式,您将寻找:
where code like '2014/08-[0-9]%' and
code not like '2014/08-%[^0-9]%'
That is, the code starts with 2014/08-
and at least one digit. 也就是说,代码以2014/08-
开头,并且至少包含一位数字。 And, it has no non-digit after the hyphen. 并且,连字符后没有非数字。
EDIT: 编辑:
In the context of the code: 在代码的上下文中:
SELECT @msg = [Message], @inmbl = FromMobileNo FROM inserted;
IF (@msg like '2014/08-[0-9]%' and
@msg not like '2014/08-%[^0-9]%'
) BEGIN
. . .
END;
EDIT II: 编辑二:
You don't mention that 2014/08
changes, but this is easy enough to handle, sort of (SQL Server could have simpler string and date functions). 您没有提到2014/08
更改,但这很容易处理(SQL Server可能具有更简单的字符串和日期函数)。 Try this: 尝试这个:
SELECT @msg = [Message], @inmbl = FromMobileNo FROM inserted;
DECLARE @yyyymm varchar(255) = (cast(year(getdate()) as varchar(255)) + '/' +
right('00' + cast(month(getdate()) as varchar(255)), 2)
);
IF (@msg like @yyyymm + '-[0-9]%' and
@msg not like @yyyymm + '-%[^0-9]%'
) BEGIN
. . .
END;
This uses the current date for the comparison. 这会将当前日期用于比较。 You might have another date you want to use. 您可能还有另一个要使用的日期。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.