SQL中的字符串操作 [英] String Operation in SQL

查看:86
本文介绍了SQL中的字符串操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我想在数学表达式中替换string + operator + digit的组合。例如,如果我有一个字符串'当前日期是:@ CurrentDate + 2。谢谢',我希望它像这样被替换 - '当前日期是:'2013年8月31日'。谢谢'当前日期是8月29日。任何人都可以帮助我找到实现它的最佳方法吗?谢谢



注:

1.要添加的数字(此处为2)可以是任意数字。

2. @CurrentDate之后可能有空格即:@CurrentDate + 5

3.字符串中可能有多个@CurrentDate,其中包含不同的数字。例如:

'当前日期是:@ CurrentDate + 2。请再次访问@CurrentDate + 11'

Hi All,
I want to replace a combination of string + operator + digit in a mathematical expression. For example, if I have a string 'Hi, The Current Date is: @CurrentDate+2. Thanks', i want it to be replaced like this- 'Hi, The Current Date is: '31 Aug 2013'. Thanks' when current date is 29 Aug. Can anyone help me to find the best approach to accomplish it? Thanks

Note:
1. The digit to add (which is 2 here) can be any number.
2. There could be space after @CurrentDate i.e: @CurrentDate + 5
3. There could be multiple @CurrentDate in the string with different digits. For Example:
'Hi, The Current Date is: @CurrentDate+2. Please visit again on @CurrentDate + 11'

推荐答案

查看此链接 [ ^ ]






你可以像下面的sql代码一样...



声明@ str1 varchar(100)

设置@ str1 ='当前日期是:@CurrentDate。谢谢'

声明@num int = null

--SET @ num = 2

如果@num为空

BEGIN

SET @ num = 0

END

声明@CurrentDate varchar(最大值)

设置@ CurrentDate = CONVERT(VARCHAR(11),GETDATE()+ @ num,106)

SELECT REPLACE(@ str1,'@ CurrentDate',@ CurrentDate);
Hi,

You can do like bellow sql code...

Declare @str1 varchar(100)
Set @str1='Hi, The Current Date is: @CurrentDate. Thanks'
Declare @num int= null
--SET @num=2
if @num is null
BEGIN
SET @num=0
END
Declare @CurrentDate varchar(max)
Set @CurrentDate=CONVERT(VARCHAR(11),GETDATE()+@num,106)
SELECT REPLACE(@str1,'@CurrentDate',@CurrentDate);


这篇关于SQL中的字符串操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆