UPDATE multiple values in table t-sql - sql

I have multiple columns where one of them has varchar values as follow.
1
2
4
02
05
6
03
06
123
32
87
I want to find any record that has values starting with 0 > remove that 0 (so 02 > 2) and update that field so there is no value starting with 0.
How can I go about doing that instead of manually updating each record that has 0 in the front? Is that even possible?
Thank you

The following code removes the leading zeros by casting the value to an integer and back to a character string:
update t
set col = cast(cast(col as int) as varchar(255))
where t.col like '0%'

You can use the following:
update yourtable
set col = substring(col, 2, len(col)-1)
where left(col, 1) = '0'
See SQL Fiddle with Demo

Run this query until everything is filtered out I guess...
UPDATE Table SET Column=RIGHT(Column, LEN(Column)-1) WHERE Column LIKE '0%';
[Edit] Gordon's approach is probably better.

Related

Leading zero using sql server

I need to add leading zero in my records, but it doesn't work then I have no idea to solve this. the data as follow
textmess
convert
8
08
8
008
14
014
this is the query i have run
Select right('000' + rtrim(ltrim(cast(TEXTMESS as varchar(3)))), 3) convert, TEXTMESS from mytable
I thought it would work but it didn't work for first row, it should be 008 right? why the result is different with second row? . please help to solve this query
You can go for FORMAT and apply preceding 0.
DECLARE #table table(textmess int)
insert into #table
values (8),(8),(14)
SELECT textmess, FORMAT(textmess,'000') as convertedTExt from #table
textmess
convertedTExt
8
008
8
008
14
014
It seems that you have a special non-visible character in that row. First check that varchar value in hex to check which character code is it:
Select
right('000' + rtrim(ltrim(cast(TEXTMESS as varchar(3)))), 3) AS Converted,
TEXTMESS,
CONVERT(VARBINARY, TEXTMESS) AS HexValues
from
mytable
Seems that for a row you have 0x38 and for the other 0x381C. Assuming that the values are in ASCII, 38 is the 8 and 1C is the hex for symbol FS (file separator).
If you check any ASCII table online, you can see that the code for FS is 28, so just use REPLACE with CHAR(28). Check the result first:
Select
right('000' + rtrim(ltrim(cast(TEXTMESS as varchar(3)))), 3) AS Converted,
TEXTMESS,
REPLACE(TEXTMESS, CHAR(28), '') AS Replaced,
right('000' + rtrim(ltrim(cast(REPLACE(TEXTMESS, CHAR(28), '') as varchar(3)))), 3) AS ReplacedConverted
from
mytable
Then remove the character with UPDATE for all rows that have any FS:
UPDATE T SET
TEXTMESS = REPLACE(TEXTMESS, CHAR(28), '')
FROM
mytable AS T
WHERE
CHARINDEX(CHAR(28), TEXTMESS) > 0
try this
SELECT RIGHT('000'+CAST(textmess AS VARCHAR(3)),3)convertedText, textmess from mytable

How do I update a SQL table to strip away the left 3 characters in a 9 character string?

I have a table with values like this in columnA:
ColumnA
800
800602041
800602044
800602050
800602057
800602093
800602099
800602131
800602132
800602133
I need to strip away the leading 800 but leave the rest of the numbers intact. I tried doing an update with wildcards but it failed. I have several tables with thousands of records that need this correction.
The result would look like this:
ColumnA
800
602041
602044
602050
602057
602093
602099
602131
602132
602133
I cannot lose the single 800 record which is why I was trying an update query with wildcards. I only want to update the rows with a 9 digit value that begins with 800.
Kindly try using below-mentioned query.
Consider table name as #a and column name as colA.
Update #a set colA = case when len(ltrim(rtrim(cola)))<=3 then colA else right(colA, len(ltrim(rtrim(colA)))-3) end
Select * from #a
A typical method is:
(case when columnA like '800%' then substring(columnA, 4, 6) else columnA) as new_columnA
Note: This preserves values when the value does not start with 800. And substring() might be spelled substr() in your database.
Since the data values are stored as numbers, I ended up treating it like a math problem.
BEGIN TRAN
SET XACT_ABORT ON
UPDATE sometable
SET ColumnA = ColumnA - 800000000
WHERE ColumnA LIKE '8006%'
It is not sophisticated but it worked.
UPDATE YourTable
SET ColumnA = SUBSTRING(ColumnA, 4, LEN(ColumnA))
WHERE LEN(ColumnA) = 9 AND ColumnA LIKE '800%'
A mass update would look like this with the information you have provided. Update ColumnA and take the entire value starting at the 4th character. Only do this when the length of ColumnA is equal to 9 and is prefixed with 800.
Filter the records by checking the string length which are exactly 9 digits and which starts with characters '800'. Same can be achieved with help of LEFT/RIGHT function.
UPDATE data_table
SET columnA = right(columnA,6)
WHERE LEN(columnA) = 9 AND left(columnA,3) = '800'
Note: Consider "data_table" as your actual table and "columnA" as the column you want to update.

SQL - Changing data type of an alphanumeric column

I'm on Teradata. I have an ID column that looks like this:
23
34
W7
007
021
90
GS8
I want to convert the numbers to numeric so the 007 should be 7 and 021 be 21. When a number is stored as a string, I usually do column * 1 to convert to numeric but in this case it gives me a bad character error since there are letters in there.
How would I do this in a select statement within a query?
Assuming that numeric values always start with a number, then something like this should work:
update t
set col = (case when substr(col, 1, 1) between '0' and '9'
then cast(cast(col as int) as varchar(255))
else col
end);
Or, you can forget the conversion and do:
update t
set col = trim(leading '0' from col);
Note: both of these assume that if the first character is a digit then the whole string comprises digits. The second assumes that the values are not all zeroes (or, more specifically, that returns the empty string).
Simply use TO_NUMBER(col) which returns NULL when the cast fails.

Select Part of Column

I was wondering if anyone could help with a query to select part of a column.
The column 'criteriadata' contains data that would look like this:
CriteriaData
14 27 15 C
14 30 15 DD
14 38 15 Pass
14 33 15 Pass
How can I select just the data that appears after the number 15.
Many thanks.
SELECT RIGHT(CriteriaData,
LEN(CriteriaData) - CHARINDEX('15', CriteriaData, 1) - 2)
FROM TableName
WHERE CriteriaData LIKE '%15%';
SQL Fiddle Demo
declare #T table
(
CriteriaData varchar(20)
)
insert into #T values
('14 27 15 C'),
('14 30 15 DD'),
('14 38 15 Pass'),
('14 33 15 Pass')
select stuff(CriteriaData, 1, 3+charindex(' 15 ', CriteriaData), '')
from #T
Result:
---------
C
DD
Pass
Pass
If CriteriaCData always contains a pattern of 3 numbers of 2 numerics separated by a space then you always want to retrieve from 10th chars:
select SUBSTR(CriteriaCData, 10) from xxx
If you are under oracle min 10.g then use REGEXP_SUBSTR to retrieve the alpha pattern
SELECT upper(REGEXP_SUBSTR(CriteriaCData, '[a-zA-Z]*$')) FROM xxx
Since you seem to want everything from the ninth character onwards, you could use RIGHT and LEN
SELECT right([CriteriaData], len([CriteriaData]) - 9)
However, you'd be better off normalizing your data so it was already in a seperate column.
On oracle use LENGTH instead of LEN
SELECT substr(CriteriaData, 8, LENGTH(CriteriaData) - 9) from table
You should use substring with left functions
Have a look at this: How to extract this specific substring in SQL Server?
And this: http://msdn.microsoft.com/en-us/library/aa259342(v=sql.80).aspx
SELECT substring(criteriadata, 9, LEN(criteriadata)-8) from table
This assumes that the position of 15 is fixed.
Declare #x nvarchar(100) = '14 30 15 DD';
Select substring(#x, (select charindex('15',#x,1) + 2) ,len(#x));
I created a SQL function to split the criteria by the spaces and used the last remaining value after the last space.
create function dbo.getCriteria
(
#criteria varchar(500)
)
returns varchar(500)
begin
declare #space as int
select #space=charindex(' ', data) from mydata
while #space > 0
begin
set #criteria=substring(#criteria, #space + 1, len(#criteria))
select #space=charindex(' ', #criteria)
end
return #criteria
end
select dbo.getCriteria(data) from mydata
SELECT
RIGHT(CriteriaData, LEN(CriteriaData) - (CHARINDEX('15', CriteriaData, 1) - 2))
FROM
MyTable;
As I had trouble making prior answers work, I had to find my own and figure for future reference I'd leave it on Stack Overflow. My field has XML but it's an NVarchar field and should generalise just fine - if you have a clear criteria for left AND right surrounding strings.
It's not a complete match to this question but I hope it helps someone else who has huge strings in their columns and needs to snip out a string that varies in between two others!
WITH r
AS (
SELECT TOP 100 RIGHT(XMLData, LEN(XMLData)-CHARINDEX('<INVOICE_NO>', XMLData)-11) AS xmldata
FROM IncomingPartsInvoiceXML)
SELECT LEFT(xmldata, CHARINDEX('<\/INVOICE_NO>', XMLData)-1)
FROM r;

copy part of string from one column into another

I've got a column with some string data that somewhere has 'T##' (## being a two digit number) I want to copy this into another column, how do I do that?
something like this:
abc-T03-def -> 03
For Microsoft SQL Server:
update YourTable
set NewColumn = substring(OldColumn, patindex('%T[0-9][0-9]%', OldColumn) + 1, 2)
where patindex('%T[0-9][0-9]%', OldColumn) <> 0