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

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.

Related

Snowflake if String Pattern replace

I have column which should only contain numbers[0-9], But in some cases we started see alphanumeric[eg:p8eSadfghrc] values in that column.
I wanna write a condition where if the value is not completely numeric{0-9}, i wanna replace it with another value from another column.
Something like this?
update t
set col = <other value>
where regexp_like(col, '[^0-9]');
This updates the data. You could also just do this in a query:
select t.*,
(case when regexp_like(col, '[^0-9]') then <other value> else col end)
from t;
In Snowflake, I would recommend try_to_decimal(). It attempts to convert the number to a decimal value (you control the target precision and scale with arguments) and rturns null if that fails:
select t.*, case when try_to_decimal(mycol) is null then myothercol else mycol end newcol
from mytable
If you want an update statement:
update mytable
set mycol = myothercol
where try_to_decimal(mycol) is null
When given no second and third argument, the number of allowed digits is 38, with 0 decimals (which seems to be what you want).

SQL Strings - Filter by Hypen(x number)

I am trying to formulate a query that will allow me to find all records from a single column with 3 hyphens. An example of a record would be like XXXX-RP-XXXAS1-P.
I need to be able to sort through 1000s of records with either 2 or 3 hyphens.
You can REPLACE the hyphens in the string with an empty string and compute the difference of the length of original string and the replaced string to check for the number of hyphens.
select *
from yourtable
where len(column_name)-len(replace(column_name,'-',''))=3
and substring(column_name,9,1) not like '%[0-9]%'
If your records have 2 or 3 hyphens, then just do:
where col like '%-%-%-%'
This will get 3 or more hyphens. For exactly 3:
where col like '%-%-%-%' and col not like '%-%-%-%-%'
try this,
declare #t table(col1 varchar(50))
insert into #t values ('A-B'),('A-B-C-D-E'),('A-B-C-D')
select * from
(SELECT *
,(len(col1) - len(replace(col1, '-', ''))
/ len('-')) col2
FROM #T)t4
where col2=3

postgresql update row to fixed digit number

I am beginner in sql query and I am trying to update my rows like that:
1--->0001
15-->0015
254-->0254
1458-->1458
My column's type is text and there are lots of columns so I cannot handle with
update table1 set col1 = 0001 where col1 = 1;
and so on..
This seems easy question but after research,I could not find a solution. all I need is something like
foreach row in col1
if((int)row>0 and < 10)
then row = "000" + row;
All texts are infact integer value but I have to keep them as text. Whats sql query of above code?
Thanks
You can use the lpad() function:
update table1
set col1 = lpad(col1, 4, '0')
where length(col1) < 4;
But the real question is: why are you storing numbers as text values? That is almost always a bad choice.

SQL Update based on parts of another field

I need to update a field based on partial matches of another. "Field2" is a constant 4 characters in length. What I was trying to do was have it look at Field2 and if it started with an F then update Field1 to 340. Then if it starts with an F and the 3rd character is a 3 then update field1 to 344 and so on.
Basically, 340 would be the rule when field2 starts with F and F*3* would be the exception and needs to be called 344.
The problem of course is that using 'like F*3*' is not actually looking for a 3 in the third position but rather anywhere after the F.
Is there a way to do this? I appreciate any help.
Jim
DoCmd.RunSQL "UPDATE [Table] SET [Table].[Field1] = ""340"" WHERE (([Table]![Field2] Like ""F*""));", -1
DoCmd.RunSQL "UPDATE [Table] SET [Table].[Field1] = ""344"" WHERE (([Table]![Field2] Like ""F*3*""));", -1
DoCmd.RunSQL "UPDATE [Table] SET [Table].[Field1] = ""343"" WHERE (([Table]![Field2] Like ""F*4*""));", -1
Your conditions can be specific to what you want to match, so don't use the wildcard at all.
Consider that your first condition can be LEFT([Field2],1) = 'F'
Your second condition can use SUBSTRING()
LEFT([field2],1) = 'F' AND SUBSTRING([field2],3,1) = '3'
This way, you're matching where the first character is 'F' and the third character is '3'

How to quickly compare many strings?

In SQL Server, I have a string column that contains numbers. Each entry I need is only one number so no parsing is needed. I need some way to find all rows that contain numbers from 400 to 450. Instead of doing:
...where my stringcolumn like '%400%' or stringcolumn like '%401%' or stringcolumn like '%402%' or ...
is there a better that can save on some typing?
There are also other values in these rows such as: '5335154', test4559#me.com', '555-555-5555'. Filtering those out will need to be taken into account.
...where stringcolumn like '4[0-4][0-9]' OR stringcolumn = '450'
You don't need the wildcard if you want to restrict to 3 digits.
Use regex to accomplish this.
...where stringcolumn like '4[0-4][0-9]' OR stringcolumn like '450'
one way
WHERE Column like '%4[0-4][09]%'
OR Column LIKE '%500%'
keep in mind that this will pick anything with the number in it, so 5000 will be returned as well
I would do the following:
select t.*
from (select t.*,
(case when charindex('4', col) > 0
then substrint(col, charindex('4', col), charindex('4', col) + 2)
end) as col4xx
from t
) t
where (case when isnumeric(col4xx) = 1
then (case when cast(col4xx as int) between 400 and 450 then 'true'
end)
end) = 'true'
I'm not a fan of having case statements in WHERE clauses. However, to ensure conversion to a number, this is needed (or the conversion could become a column in another subquery). Note that the following is not equivalent:
where col4xx between '400' and '450'
Since the string '44A' would match.