How do I trim these fields of quotation marks in SQL Server? - sql-server

I have a table with 6 million + records, but the first field has a " at beginning and the last field has " at the end
i guess when doing a bulk insert they forgot to remove it.
I want to run an update query which will remove the " from those 2 fields
Sample data -
Field 1
"18157142
"18157152
"18157159
"18157177
"18157189
"18157191
"18157197
"18157208
"18157223
"18157224
Field 2 (Last field)
243901"
832218"
506356"
78301"
753201 - Automobile Body"
553111 - Automobile Parts & Supplies-Retail"
581203"
792903 - Music"
653118"
541105 - Grocers"
Also no place in this field does the " show up anywhere else so its just 1 " to remove from the field.
Any ideas?

Update myTable
SET
Field1 = Replace(Field1, '"', ''),
Field2 = Replace(Field2, '"', '')
This would work faster than other recommendations because you won't have to perform any extra string length function calls to determine how far over to count. This will just replace all occurrences of the quotes, which is what was originally asked for (since quotes won't show up anywhere where they might not want to be removed).

This is the update,
update table
set field1 = right(field1,length(field1)-1),
field2 = left(field1,length(field1)-1)
The problem is the enormous transaction that it will generate...

UPDATE TableName
SET Field1 = MID(Field1,2)
Field2 = MID(Field2,1,LEN(Field2)-1)

UPDATE MyTable
SET Field1 = RIGHT(Field1, LEN(Field1) - 1), Field2 = LEFT(Field2, LEN(Field2) - 1)

UPDATE [table]
SET [field1] = RIGHT( [field1], LEN([field1]) - 1),
[field2] = LEFT( [field2], LEN([field2]) - 1)

If you have the time to wait I would just:
update [tableName] set field1 = right(field1, len(field1) -1)
update [tableName] set field2 = left(field2, len(field2) -1)

Related

How can I CUT a specific string part to another column in SQL?

I have about 500 records in a table with an nvarchar column.
I want to cut a part of that data into another column. by "cut" I mean deleting it in the original column and add it to the target column.
All the data that has to be cut is contained within brackets. The bracketed text may occur anywhere in the string.
For example, ColumnA has: SomeTest Data [I want to cut this], and I want to move [I want to cut this] (but without the brackets) to ColumnB.
How do I achieve this?
UPDATE
Eventually found it out. The problem was that I didn't escaped my brackets.
What I have now (and works):
UPDATE TableA
SET TargetColumn = substring(SourceColumn,charindex('[',SourceColumn)+1,charindex(']',SourceColumn)-charindex('[',SourceColumn)-1),
SourceColumn = substring(SourceColumn, 0, charindex('[',SourceColumn))
where TableA.SourceColumn like '%\[%\]%' ESCAPE '\'
An UPDATE statement along these lines would do it:
CREATE TABLE #Test
(
StringToCut VARCHAR(100)
,CutValue VARCHAR(100)
)
INSERT #Test
VALUES
('SomeTest Data 1 [I want to cut this 1] More Testing',NULL),
('SomeTest Data 2 [I want to cut this 2]',NULL),
('SomeTest Data 3 [I want to cut this 3] Additional Test',NULL),
('[I want to cut this 4] last test',NULL)
SELECT * FROM #Test
--Populate CutValue column based on starting position of '[' and ending position of ']'
UPDATE #Test
SET CutValue = SUBSTRING(StringToCut,CHARINDEX('[',StringToCut),(CHARINDEX(']',StringToCut)-CHARINDEX('[',StringToCut)))
--Remove the '[' ']'
UPDATE #Test
SET CutValue = REPLACE(CutValue,'[','')
UPDATE #Test
SET CutValue = REPLACE(CutValue,']','')
--Remove everything after and including '[' from StringToCut
UPDATE #Test
SET StringToCut = LEFT(StringToCut,CHARINDEX('[',StringToCut)-1) + LTRIM(RIGHT(StringToCut,LEN(StringToCut)-CHARINDEX(']',StringToCut)))
SELECT * FROM #Test
DROP TABLE #Test
You left some questions unanswered.
How do you want to handle NULL values? I am leaving them NULL.
Where should the 'cut' string go? I am assuming "at the end"
What do you do if you find nested brackets? [[cut me]]
Do you need to remove any surrounding spaces? For example, does "The cat [blah] sleeps" become "The cat**sleeps" with two spaces before "sleeps"?
To make the operation atomic, you'll want to use a single UPDATE.
Here is a sample script to get you started.
--build a temp table with sample data
declare #t table(ikey int, sourcecolumn nvarchar(100), targetcolumn nvarchar(100));
insert into #t
select 0,'SomeTest Data [I want to cut this]','Existing Data For Row 1'
union select 1,'SomeTest [cut this too] Data2','Existing Data For Row 2'
union select 2,'[also cut this please] SomeTest Data3',null
union select 3,null,null
union select 4,null,''
union select 5,'Nested bracket example [[[within nested brackets]]] Other data',null
union select 6,'Example with no brackets',null
union select 7,'No brackets, and empty string in target',''
--show "before"
select * from #t order by ikey
--cut and paste
update #t
set
targetcolumn =
isnull(targetcolumn,'') +
case when 0 < isnull(charindex('[',sourcecolumn),0) and 0 < isnull(charindex(']',sourcecolumn),0)
then substring(sourcecolumn,charindex('[',sourcecolumn)+1,charindex(']',sourcecolumn)-charindex('[',sourcecolumn)-1)
else ''
end
,sourcecolumn =
case when sourcecolumn is null
then null
else substring(sourcecolumn,0,charindex('[',sourcecolumn)) + substring(sourcecolumn,charindex(']',sourcecolumn)+1,len(sourcecolumn))
end
where sourcecolumn like '%[%'
and sourcecolumn like '%]%'
--show "after"
select * from #t order by ikey
And another one in single update statement -
CREATE TABLE #Test
(
StringToCut VARCHAR(50)
,CutValue VARCHAR(50)
)
INSERT #Test
VALUES
('SomeTest Data 1 [I want to cut this 1]',NULL),
('SomeTest Data 2 [I want to cut this 2]',NULL),
('SomeTest Data 3 [I want to cut this 3]',NULL),
('SomeTest Data 4 [I want to cut this 4]',NULL)
UPDATE #Test
SET CutValue =
SUBSTRING(StringToCut, CHARINDEX('[', StringToCut)+1, CHARINDEX(']', StringToCut) - CHARINDEX('[', StringToCut) - 1)
SELECT * FROM #Test

How to Update a Column

I have SQL Express 2012 with a table that I added a column (integer) for a Word Count and the table already has a few hundred rows. I am not sure how I update the column to have the word count from the "entry" column.
I created a query that shows me the data, but how do I use this to update the table to store the Word Count for each entry?
SELECT
ID,
[UserName],
[DateCreated],
LEN([Entry]) - LEN(REPLACE([Entry], ' ', '')) + 1 AS 'Word Count'
FROM [dbo].[Notes]
The verb in the SQL language to update data in a table is not surprisingly UPDATE. The documentation has the full syntax.
If you want to update all rows and there are no NULL values in the Entry column (that would make the calculation fail) then this query will update a column named WordCount:
UPDATE Notes SET WordCount = LEN([Entry]) - LEN(REPLACE([Entry], ' ', '')) + 1
Try this piece of code.
UPDATE Notes SET
WordCount = LEN([Entry]) - LEN(REPLACE([Entry], ' ', '')) + 1
This should then update all rows in the table with the word count for that row.
Thanks,
Here is how you could do this so that your values are always current. Two big advantages here. First, you don't have to ever update your table. Second, the values will ALWAYS be current even if somebody updates your table with a query and doesn't update the WordCount.
create table #test
(
Entry varchar(100)
, WordCount as LEN(Entry) - LEN(REPLACE(Entry, ' ', ''))
)
insert #test
select 'two words' union all
select 'three words now'
select * from #test
drop table #test

Replace some characters in a column

I need to remove some characters and replace them from others.
Like in a column I need to replace
I need to replace column text from "data&data&sometext" to "data&sometext".
Something like
UPDATE TABLE
SET Column = "data&sometext"
WHERE Column = "data&data&sometext"
UPDATE TABLE
SET Column = REPLACE(Column , 'data&data&sometext', 'data&sometext')
WHERE Column LIKE '%data&data&sometext%'
Well, try this:
UPDATE TABLE
SET Column = REPLACE(Column, 'data&data&', 'data&')
WHERE Column LIKE 'data&data&%';
UPDATE:
So, based on your comment, try this:
UPDATE TABLE
SET Column = REPLACE([Column], LEFT([Column], CHARINDEX('&', [Column], CHARINDEX('&', [Column]) + 1)), LEFT([Column], CHARINDEX('&', [Column])))
WHERE Column LIKE LEFT([Column], CHARINDEX('&', [Column], CHARINDEX('&', [Column]) + 1)) + '%'

Need to remove data from a substring

I haven't the foggiest how to remove a substring from my column, I have been looking here for a few days and everyone seems to want to remove data from the end not the beginning.
Column data: /data/data/data.com --data=nameiwant2keep
Column name: column1
Table name: table1
Thank you for any help.
Assuming you wanted to keep just the nameiwant2keep part, and there won't be any other parameters in the column data, you can search for the index of the = sign and take the substring from the next character to the end of the string:
UPDATE
table1
SET
column1 = SUBSTRING(column1, CHARINDEX('=', column1, 0) + 1, LEN(column1))
Try this for proof:
DECLARE #column1 varchar(max)
SET #column1 = '/data/data/data.com --data=nameiwant2keep'
SELECT
SUBSTRING(#column1, CHARINDEX('=', #column1, 0) + 1, LEN(#column1))

remove spaces in the columns in ASE

I have columns that contain empty spaces with the data:
example:| fish |
how can I update the column so my result will be : |Fish| ?
in oracle I can trim the column:
update Example set column1 = trim(column1)
I google it and i notice that ASE doesnt supoort trim.
I found that str_replace(column1, ' ', '') does not actually replace the spaces.
Switching the '' for null works:
create table Example (column1 varchar(15))
insert into Example (column1) values ('| fish |')
select * from Example
-- produces "| fish |"
update Example set column1 = str_replace(column1, ' ', null)
select * from Example
-- produces "|fish|"
drop table Example
You can use combine of rtrim and ltrim
update Example set column1 = rtrim(ltrim(column1))
or str_replace
update Example set column1 = str_replace(column1,' ','')

Resources