October 25, 2016 at 6:44 am
Hello,
I want a sql function that will allow to make one update but with several replace on the same column.
For exemple
Replace the space by vacuum('')
The slash by vacuum
The point by vacuum
The dash by vacuum
The comma with the empty
The "+33 " by 0
thanks for your help
October 25, 2016 at 6:55 am
ninamahezi (10/25/2016)
Hello,I want a sql function that will allow to make one update but with several replace on the same column.
For exemple
Replace the space by vacuum('')
The slash by vacuum
The point by vacuum
The dash by vacuum
The comma with the empty
The "+33 " by 0
thanks for your help
Here's a sample of what you can do to get your result.
DECLARE @Sample TABLE(
phone varchar(20)
)
INSERT INTO @Sample
VALUES ('+33-123-861-7321'),
('+52.168.945.5465'),
('+84 (854)933,4186')
SELECT *
FROM @Sample
CROSS APPLY(SELECT REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(phone, ' ', ''), '/', ''), '.', ''), '-', ''), ',', ''), '+33', '0'))x(cleanphone)
October 25, 2016 at 7:50 am
thanks very much for your help
I need a function with a return statement and the parameter will be a phonenumber
Thanks
October 25, 2016 at 7:56 am
ninamahezi (10/25/2016)
thanks very much for your helpI need a function with a return statement and the parameter will be a phonenumber
Thanks
Then create it. Just remember that scalar user defined functions are bad for performance. You shouldn't store formatted phone numbers, the format should be a display option and not part of the data.
Here's another option for you if you really want a function:
October 25, 2016 at 9:13 am
thanks,
I have never writen sql function. So I do not know what is.
What we want to do is make an update on the phonenumber taking into account all these replace.
the goal was to create a function that will perform this task.
Thanks for your help
October 25, 2016 at 9:22 am
Functions in SQL don't perform tasks. Functions return values, that's their main purpose in any language.
To perform a process, you need a stored procedure. To update the values in a table, you need an UPDATE statement.
If you're not sure on how to do that, be sure to have someone that can help you or you might lose your data. The syntax for UPDATE can be found here: https://msdn.microsoft.com/en-us/library/ms177523.aspx or you could just press F1 when selecting the keyword on Management Studio (SSMS).
You can also find a good start on writing T-SQL code in here: http://www.sqlservercentral.com/stairway/75773/
October 25, 2016 at 9:28 am
I know how to use UPDATE but I was talking about functions in SQL.
Thanks very much.
October 25, 2016 at 10:18 am
ninamahezi (10/25/2016)
I know how to use UPDATE but I was talking about functions in SQL.Thanks very much.
You have your answer. Functions do not perform updates in T-SQL.
The absence of evidence is not evidence of absence
- Martin Rees
The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
- Phil Parkin
October 25, 2016 at 11:10 pm
Luis Cazares (10/25/2016)
ninamahezi (10/25/2016)
Hello,I want a sql function that will allow to make one update but with several replace on the same column.
For exemple
Replace the space by vacuum('')
The slash by vacuum
The point by vacuum
The dash by vacuum
The comma with the empty
The "+33 " by 0
thanks for your help
Here's a sample of what you can do to get your result.
DECLARE @Sample TABLE(
phone varchar(20)
)
INSERT INTO @Sample
VALUES ('+33-123-861-7321'),
('+52.168.945.5465'),
('+84 (854)933,4186')
SELECT *
FROM @Sample
CROSS APPLY(SELECT REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(phone, ' ', ''), '/', ''), '.', ''), '-', ''), ',', ''), '+33', '0'))x(cleanphone)
That works nice and fast thanks to the nested REPLACEs but I'd bet the OP wants to replace the "+" number (country code?) no matter what the number may be. Country Codes can be 1 to 3 digits.
--Jeff Moden
Change is inevitable... Change for the better is not.
October 25, 2016 at 11:15 pm
ninamahezi (10/25/2016)
thanks,I have never writen sql function. So I do not know what is.
What we want to do is make an update on the phonenumber taking into account all these replace.
the goal was to create a function that will perform this task.
Thanks for your help
Functions can't do an update but they can be used as a source in an update.
--Jeff Moden
Change is inevitable... Change for the better is not.
October 26, 2016 at 12:37 pm
>> I want a SQL function that will allow to make one update but with several replace on the same column. <<
UPDATE Foobar -- we have no DDL or even a table name!
SET phone_nbr
= REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(phone_nbr, ' ', ''),
'/', ''),
'.', ''),
'-', ''),
',', ''),
'+33', '');
SQL is a declarative language. Nesting functions inside each other can be quite fast and easy for the optimizer. If you hang out with LISP programmers, this will seem quite natural 🙂
Books in Celko Series for Morgan-Kaufmann Publishing
Analytics and OLAP in SQL
Data and Databases: Concepts in Practice
Data, Measurements and Standards in SQL
SQL for Smarties
SQL Programming Style
SQL Puzzles and Answers
Thinking in Sets
Trees and Hierarchies in SQL
October 27, 2016 at 12:02 pm
CELKO (10/26/2016)
>> I want a SQL function that will allow to make one update but with several replace on the same column. <<UPDATE Foobar -- we have no DDL or even a table name!
SET phone_nbr
= REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(phone_nbr, ' ', ''),
'/', ''),
'.', ''),
'-', ''),
',', ''),
'+33', '');
SQL is a declarative language. Nesting functions inside each other can be quite fast and easy for the optimizer. If you hang out with LISP programmers, this will seem quite natural 🙂
Plus we can use PROGN with SETQ's and do punched card programming!
edit: heh I don't really know lisp
Viewing 12 posts - 1 through 11 (of 11 total)
You must be logged in to reply to this topic. Login to reply