September 7, 2017 at 7:22 am
I want to replace all the values in a Column that starts with 0 to start with 27.
Please help.
September 7, 2017 at 7:32 am
hoseam - Thursday, September 7, 2017 7:22 AMI want to replace all the values in a Column that starts with 0 to start with 27.Please help.
Look for the STUFF() function.
September 7, 2017 at 7:32 am
hoseam - Thursday, September 7, 2017 7:22 AMI want to replace all the values in a Column that starts with 0 to start with 27.Please help.
CREATE TABLE #Test (Dsc VARCHAR(50));
INSERT #Test
(
Dsc
)
VALUES
('0lzdhksd')
,('abc');
SELECT *
FROM #Test t;
UPDATE #Test
SET Dsc = STUFF(Dsc, 1, 1, '27')
WHERE Dsc LIKE '0%';
SELECT *
FROM #Test t;
September 7, 2017 at 7:33 am
please give some sample data and expected result
My understanding is you want to change first character with 27th, below script should be useful
Declare @txt varchar(100) = 'abcdefghijklmnopqrstuvwxyz0123456789'
select @txt, stuff(@txt,1,1,substring(@txt,27,1))
September 7, 2017 at 10:38 am
hoseam - Thursday, September 7, 2017 7:22 AMI want to replace all the values in a Column that starts with 0 to start with 27.Please help.
select regexp_replace(column_name,0,27,1,1) from your_table
September 7, 2017 at 10:46 am
anand08sharma - Thursday, September 7, 2017 10:38 AM
select regexp_replace(column_name,0,27,1,1) from your_table
regexp_replace is not a T-SQL function.
September 7, 2017 at 10:50 am
anand08sharma - Thursday, September 7, 2017 10:38 AMhoseam - Thursday, September 7, 2017 7:22 AMI want to replace all the values in a Column that starts with 0 to start with 27.Please help.
select regexp_replace(column_name,0,27,1,1) from your_table
That function is not part of T-SQL (at least, not yet)
September 7, 2017 at 12:37 pm
Phil Parkin - Thursday, September 7, 2017 10:46 AManand08sharma - Thursday, September 7, 2017 10:38 AM
select regexp_replace(column_name,0,27,1,1) from your_table
regexp_replace is not a T-SQL function.
It could be a very poorly named scalar udf
-- Itzik Ben-Gan 2001
September 7, 2017 at 8:32 pm
Avi1 - Thursday, September 7, 2017 7:33 AMplease give some sample data and expected result
My understanding is you want to change first character with 27th, below script should be useful
Declare @txt varchar(100) = 'abcdefghijklmnopqrstuvwxyz0123456789'
select @txt, stuff(@txt,1,1,substring(@txt,27,1))
Not the 27th character. The OP want's to replace the first character with the number '27' if and only if the first character is "0".
Phil Parkin 's code in his first post above does that quite nicely.
--Jeff Moden
Change is inevitable... Change for the better is not.
September 8, 2017 at 11:02 am
Phil Parkin - Thursday, September 7, 2017 10:46 AManand08sharma - Thursday, September 7, 2017 10:38 AM
select regexp_replace(column_name,0,27,1,1) from your_table
regexp_replace is not a T-SQL function.
REPLACE is, though.
SELECT REPLACE(Columnname, 0, 27) FROM table WHERE columnname LIKE '0%'
Unfortunately, in SQL Server, REPLACE would replace ALL zeros in the number with a 27, not just the first one.
SELECT '011111010101010', STUFF('011111010101010',1,1,27)
The first argument is the string to act on.
The second argument tells us where to start - First char.
The third tells you how many characters to replace.
The fourth tells you what to replace with.
Update would look something like this:
UPDATE table set column = STUFF(column,1,1,27) WHERE column like '0%'
September 8, 2017 at 11:23 am
audrey.abbey - Friday, September 8, 2017 11:02 AM--
Update would look something like this:
UPDATE table set column = STUFF(column,1,1,27) WHERE column like '0%'
You seem to have regurgitated the query I provided in my first answer on this thread, except you have forgotten to enclose 27 in quotes, forcing the DB engine to perform an unnecessary implicit data conversion.
September 8, 2017 at 11:44 am
Phil Parkin - Friday, September 8, 2017 11:23 AMaudrey.abbey - Friday, September 8, 2017 11:02 AM--
Update would look something like this:
UPDATE table set column = STUFF(column,1,1,27) WHERE column like '0%'You seem to have regurgitated the query I provided in my first answer on this thread, except you have forgotten to enclose 27 in quotes, forcing the DB engine to perform an unnecessary implicit data conversion.
You made my whole day, lol. Thank you.
September 13, 2017 at 2:45 am
You can also use replace as below:
UPDATE #Test
SET Dsc = REPLACE(dsc,SUBSTRING(dsc,1,1),'27')
WHERE Dsc LIKE '0%';
September 13, 2017 at 7:56 am
crazy_new - Wednesday, September 13, 2017 2:45 AMYou can also use replace as below:
UPDATE #Test
SET Dsc = REPLACE(dsc,SUBSTRING(dsc,1,1),'27')
WHERE Dsc LIKE '0%';
That keeps the same problem that was described for replace while adding more work.
September 13, 2017 at 8:16 am
Luis Cazares - Wednesday, September 13, 2017 7:56 AMcrazy_new - Wednesday, September 13, 2017 2:45 AMYou can also use replace as below:
UPDATE #Test
SET Dsc = REPLACE(dsc,SUBSTRING(dsc,1,1),'27')
WHERE Dsc LIKE '0%';That keeps the same problem that was described for replace while adding more work.
Precisely. Why bother using SUBSTRING to extract character one of a string, when you know (from the WHERE clause) that this character MUST be zero.
Viewing 15 posts - 1 through 14 (of 14 total)
You must be logged in to reply to this topic. Login to reply
This website stores cookies on your computer.
These cookies are used to improve your website experience and provide more personalized services to you, both on this website and through other media.
To find out more about the cookies we use, see our Privacy Policy