October 23, 2017 at 12:03 am
Hello Experts,
I have a data in a table as shown in the first column (A) and would like to update as shown in column B.
Please note there is a space on both sides of the operator (+, -, /, *).
Only these four arithmetic operators would be used. Except, operators and numerals, other words should be enclosed by [ ].
Please do help me to achieve this either using query or using user defined function or stored procedure.
A | B |
Total Deduction * 10 | [Total Deduction] * 10 |
Remium + 0.01 | [Remium] + 0.01 |
100 / Final Results | 100 / [Final Results] |
Pre Results + 10 - New Results | [Pre Results] + 10 - [New Results] |
Thanks,
Naveen J V
October 23, 2017 at 4:38 am
Naveen J V - Monday, October 23, 2017 12:03 AMHello Experts,
I have a data in a table as shown in the first column (A) and would like to update as shown in column B.
Please note there is a space on both sides of the operator (+, -, /, *).
Only these four arithmetic operators would be used. Except, operators and numerals, other words should be enclosed by [ ].
Please do help me to achieve this either using query or using user defined function or stored procedure.
A B Total Deduction * 10 [Total Deduction] * 10 Remium + 0.01 [Remium] + 0.01 100 / Final Results 100 / [Final Results] Pre Results + 10 - New Results [Pre Results] + 10 - [New Results] Thanks,
Naveen J V
anybody attempted this questions?
October 23, 2017 at 7:18 am
You seem to be relatively new to the forums, but you should take into account that it's a good courtesy to post your sample data in a consumable format. I'll post so you can do it like that the next time.
Second, you'll need to get some additional information and a function from this article: Splitting Strings Based on Patterns - SQLServerCentral
You might also need to learn a bit about patterns in SQL Server, I wrote an article some time ago: http://www.sqlservercentral.com/articles/T-SQL/130558/
And finally, here's the code:
CREATE TABLE #SampleData(
AFormula varchar(1000));
INSERT INTO #SampleData
VALUES
('Total Deduction * 10'),
('Remium + 0.01'),
('100 / Final Results'),
('Pre Results + 10 - New Results');
SELECT sd.AFormula,
STUFF(( SELECT ' ' + CASE WHEN Item NOT LIKE '%[^0-9. +/*-]%' THEN LTRIM(RTRIM(Item)) ELSE QUOTENAME(LTRIM(RTRIM(Item))) END
FROM dbo.PatternSplitCM( sd.AFormula, '%[-*/+]%') ps
ORDER BY ps.ItemNumber
FOR XML PATH(''), TYPE).value('./text()[1]', 'varchar(max)'), 1, 1, '')
FROM #SampleData sd;
GO
DROP TABLE #SampleData;
October 23, 2017 at 9:11 am
T-SQL is horrible at string manipulation. Your best option is to use SQLCLR.
You also need to better define your options, Specifically what do you want to happen when you have alphanumeric values like 'FY2017Q1' or 'FY2017Q2'? I suspect you want '[FY2017Q1]', but your description could be interpreted to mean '[FY]2017
1'.
Finally, I hope that you're not planning to use these fields for dynamic SQL. If you are set on doing that, please read up on SQL injection.
Drew
J. Drew Allen
Business Intelligence Analyst
Philadelphia, PA
October 23, 2017 at 3:16 pm
Naveen J V - Monday, October 23, 2017 12:03 AMHello Experts,
I have a data in a table as shown in the first column (A) and would like to update as shown in column B.
Please note there is a space on both sides of the operator (+, -, /, *).
Only these four arithmetic operators would be used. Except, operators and numerals, other words should be enclosed by [ ].
Please do help me to achieve this either using query or using user defined function or stored procedure.
A B Total Deduction * 10 [Total Deduction] * 10 Remium + 0.01 [Remium] + 0.01 100 / Final Results 100 / [Final Results] Pre Results + 10 - New Results [Pre Results] + 10 - [New Results] Thanks,
Naveen J V
How many rows are we talking about in this table??? Is it just the 4 rows you have posted?
--Jeff Moden
Change is inevitable... Change for the better is not.
October 23, 2017 at 3:17 pm
drew.allen - Monday, October 23, 2017 9:11 AMT-SQL is horrible at string manipulation. Your best option is to use SQLCLR.You also need to better define your options, Specifically what do you want to happen when you have alphanumeric values like 'FY2017Q1' or 'FY2017Q2'? I suspect you want '[FY2017Q1]', but your description could be interpreted to mean '[FY]2017
1'.
Finally, I hope that you're not planning to use these fields for dynamic SQL. If you are set on doing that, please read up on SQL injection.
Drew
How would an SQLCLR be any better at this? Are you suggesting the use of RegEx?
--Jeff Moden
Change is inevitable... Change for the better is not.
October 23, 2017 at 10:19 pm
Luis Cazares - Monday, October 23, 2017 7:18 AMYou seem to be relatively new to the forums, but you should take into account that it's a good courtesy to post your sample data in a consumable format. I'll post so you can do it like that the next time.
Second, you'll need to get some additional information and a function from this article: Splitting Strings Based on Patterns - SQLServerCentral
You might also need to learn a bit about patterns in SQL Server, I wrote an article some time ago: http://www.sqlservercentral.com/articles/T-SQL/130558/And finally, here's the code:
CREATE TABLE #SampleData(
AFormula varchar(1000));
INSERT INTO #SampleData
VALUES
('Total Deduction * 10'),
('Remium + 0.01'),
('100 / Final Results'),
('Pre Results + 10 - New Results');SELECT sd.AFormula,
STUFF(( SELECT ' ' + CASE WHEN Item NOT LIKE '%[^0-9. +/*-]%' THEN LTRIM(RTRIM(Item)) ELSE QUOTENAME(LTRIM(RTRIM(Item))) END
FROM dbo.PatternSplitCM( sd.AFormula, '%[-*/+]%') ps
ORDER BY ps.ItemNumber
FOR XML PATH(''), TYPE).value('./text()[1]', 'varchar(max)'), 1, 1, '')
FROM #SampleData sd;GO
DROP TABLE #SampleData;
Thank you very much Luis. This works as I expected.
Yes Luis, I am very new to the forum, I would take care of posting sample data in a table or in a consumable format in future.
October 23, 2017 at 10:26 pm
Naveen J V - Monday, October 23, 2017 10:19 PMLuis Cazares - Monday, October 23, 2017 7:18 AMYou seem to be relatively new to the forums, but you should take into account that it's a good courtesy to post your sample data in a consumable format. I'll post so you can do it like that the next time.
Second, you'll need to get some additional information and a function from this article: Splitting Strings Based on Patterns - SQLServerCentral
You might also need to learn a bit about patterns in SQL Server, I wrote an article some time ago: http://www.sqlservercentral.com/articles/T-SQL/130558/And finally, here's the code:
CREATE TABLE #SampleData(
AFormula varchar(1000));
INSERT INTO #SampleData
VALUES
('Total Deduction * 10'),
('Remium + 0.01'),
('100 / Final Results'),
('Pre Results + 10 - New Results');SELECT sd.AFormula,
STUFF(( SELECT ' ' + CASE WHEN Item NOT LIKE '%[^0-9. +/*-]%' THEN LTRIM(RTRIM(Item)) ELSE QUOTENAME(LTRIM(RTRIM(Item))) END
FROM dbo.PatternSplitCM( sd.AFormula, '%[-*/+]%') ps
ORDER BY ps.ItemNumber
FOR XML PATH(''), TYPE).value('./text()[1]', 'varchar(max)'), 1, 1, '')
FROM #SampleData sd;GO
DROP TABLE #SampleData;Thank you very much Luis. This works as I expected.
Yes Luis, I am very new to the forum, I would take care of posting sample data in a table or in a consumable format in future.
Still, the question I have is how many rows do you actually need to do this for? Also, is this a one-off or something that will need to be repeated in the future?
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply