I need to remove a prefix 0 from the data in specific column in BQ. How it should be done? Using substr function?
Example:
id
012345
0012345
00012345
and the output should be without zeros, so should be like:
id
12345
12345
12345
in all listed cases above.
I know how to add a prefix:
UPDATE
table.name
SET
id = CAST(CONCAT('99999', CAST(id AS STRING)) AS INTEGER)
WHERE
code = 'US' and cast(id as string)
Using LTRIM,
SELECT LTRIM(id, '0') id
FROM UNNEST(['012345', '0012345', '00012345']) id
Output will be:
If you cast your string to integer, you'll remove the zeroes. Then in order to get back your string, it's sufficient to cast back to string.
UPDATE table.name
SET id = CAST(CAST(id AS INTEGER) AS STRING)
WHERE code = 'US'
Does it work for you?
The below query should work
SELECT REPLACE(LTRIM(REPLACE('00012345', '0', ' ')),' ', '0');
SELECT REPLACE(LTRIM(REPLACE('0012345', '0', ' ')),' ', '0');
I've used regex:
set id, REGEXP_REPLACE(id, r"^[0]+(.*)", "\\1")
from table.name
where id like '0%'
Related
Here is my column :
column
abc1234
abc5678
abc4567
Now I need to remove the abc only from the column. Please help me write a query.
You might want to use REGEXP_REPLACE here:
UPDATE yourTable
SET col = REGEXP_REPLACE(col, '^abc', '')
WHERE col LIKE 'abc%';
If you don't care about the particular position of abc, and accept removing all occurrences of it anywhere, then we can do without regex:
UPDATE yourTable
SET col = OREPLACE(col, 'abc', '')
WHERE col LIKE 'abc%';
I have a table like:
ID NAME
----------------
35 File.png
What I want to do is update that record as:
ID NAME
----------------
35 File_35.png
I have this:
DECLARE arch_cursor CURSOR FOR SELECT Id from dbo.Archivos WHERE Nombre LIKE ('%' + #Id_Relacion_Articulo_Archivo + '%')
OPEN contact_cursor;
FETCH NEXT FROM arch_cursor
INTO #Id_cur
WHILE(##FETCH_STATUS = 0 )
UPDATE dbo.Archivos SET Nombre ...
CLOSE arch_cursor;
DEALLOCATE arch_cursor;
I know that STUFF statement can do something like put a string on a specific place of the string, but somebody know how to specify "insert string before "." without losing the left chars too?"
select id
, concat(left(name, charindex('.', name)-1), '_', id, RIGHT(name,(CHARINDEX('.', name))-1)) NAME
from test
Here is a demo
Also you can do it with a replace:
select id
, replace(name, '.', concat('_',id,'.'))
from test
And here is the one option you can use if you will look from the right (first occurrence of the character '.'):
select id
, concat(substring(name, 1, len(name)-charindex('.', (reverse(name))))
, '_'
, id
, substring(name, len(name)-charindex('.', (reverse(name)))+1, charindex('.', (reverse(name)))))
from test
Here is a demo where you can see all 3 of this examples in action with this two rows of data:
insert into test values (35, 'File.png')
insert into test values (55,
'File.File.png')
If no files can have a . in the name (apart from to denote the extension) I find STUFF easiest for this:
SELECT ID, [Name],
STUFF([Name], CHARINDEX('.',[Name]),0,CONCAT('_',ID)) AS NewName
FROM (VALUES(35,'File.png'))V(ID,[Name]);
If there could be multiple . characters and it needs to prior to the last one, you have to use the (expensive) function REVERSE:*
SELECT ID, [Name],
STUFF([Name], LEN([Name]) - CHARINDEX('.',REVERSE([Name]))+1,0,CONCAT('_',ID)) AS NewName
FROM (VALUES(35,'File.png'),(36,'File.2.png'))V(ID,[Name])
Also, don't loop. Just do this in a set based method. You're using a RDBMS, not a Programming Language:
UPDATE dbo.YourTable
SET File = STUFF([Name], CHARINDEX('.',[Name]),0,CONCAT('_',ID));
I need help to replace funny character in a column in SQL Server,
I have data like this:
id itemDesc
----------------------------------------------
1 Ball lock pins/ spring typeáááááá
2 Res 1.5k Ohm û R0805 1%
If itemDesc contains á, then replace it with " "; if it contains (û), replace it with -. I used charindex but not change at all especially with many funny characters like id = 1, so if i used charindex, "Ball lock pins/ spring typeáááááá" => "Ball lock pins/ spring type ááááá"
Any approach?
thanks for help
You can use REPLACE to replace the characters on the string:
SELECT REPLACE(REPLACE(itemDesc, 'á', ' '), 'û', '-') FROM table_name
In case you want to UPDATE the value on column itemDesc you can use the following:
UPDATE table_name SET itemDesc = REPLACE(REPLACE(itemDesc, 'á', ' '), 'û', '-')
The function CHARINDEX can't be used to replace the characters, but to find them. So you can UPDATE or SELECT only the rows with these characters using CHARINDEX:
SELECT REPLACE(REPLACE(itemDesc, 'á', ' '), 'û', '-')
FROM table_name
WHERE CHARINDEX('á', itemDesc) > 0 OR CHARINDEX('û', itemDesc) > 0
UPDATE table_name SET itemDesc = REPLACE(REPLACE(itemDesc, 'á', ' '), 'û', '-')
WHERE CHARINDEX('á', itemDesc) > 0 OR CHARINDEX('û', itemDesc) > 0
demo: http://www.sqlfiddle.com/#!18/6e241/1/0
If you're using SQL Server 2017, you can use TRANSLATE:
SELECT TRANSLATE(itemDesc, 'áû',' -') AS itemDescTidy
FROM table_name;
This is a little more succinct than a nested REPLACE (but is actually identical, as it's a "short-hand" function).
My query is the following:
SELECT id, category FROM table1
This returns the following rows:
ID|category
1 |{IN, SP}
2 |
3 |{VO}
Does anyone know how i can remove the first char and last char of the string in PostgreSQL, so it removes: {}?
Not sure, what you mean with "foreign column", but as the column is an array, the best way to deal with that is to use array_to_string()
SELECT id, array_to_string(category, ',') as category
FROM table1;
The curly braces are not part of the stored value. This is just the string representation of an array that is used to display it.
Either using multiple REPLACE functions.
SELECT id, REPLACE(REPLACE(category, '{', ''), '}', '')
FROM table1
Or using a combination of the SUBSTRING, LEFT & LENGTH functions
SELECT id, LEFT(SUBSTRING(category, 2, 999),LENGTH(SUBSTRING(category, 2, 999)) - 1)
FROM table1
Or just SUBSTRING and LENGTH
SELECT id, SUBSTRING(category, 2, LENGTH(category)-2)
FROM table1
You could replace the {} with an empty string
SELECT id, replace(replace(category, '{', ''), '}', '') FROM table1
select id,
substring(category,charindex('{',category,len(category))+2,len(category)-2)
from table1;
select id
,left(right(category,length(category)-1),length(category)-2) category
from boo
select id
,trim(both '{}' from category)
from boo
Trim():
Remove the longest string containing only the characters (a space by
default) from the start/end/both ends of the string
The syntax for the replace function in PostgreSQL is:
replace( string, from_substring, to_substring )
Parameters or Arguments
string
The source string.
from_substring
The substring to find. All occurrences of from_substring found within string are replaced with to_substring.
to_substring
The replacement substring. All occurrences of from_substring found within string are replaced with to_substring.
UPDATE dbo.table1
SET category = REPLACE(category, '{', '')
WHERE ID <=3
UPDATE dbo.table1
SET category = REPLACE(category, '}', '')
WHERE ID <=3
I'm sure this is super easy, but how would I go about converting LastName,FirstName to LastName,FirstInitial?
For example changing Smith,John to Smith,J or Johnson,John to Johnson,J etc.
Thank You!
In case of LastName and FirstName columns:
select LastName,substr(FirstName,1,1)
from mytable
;
In case of a fullname saved in a single column:
select substr(fullname,1,instr(fullname || ',',',')-1) || substr(fullname,instr(fullname || ',',','),2)
from mytable
;
or
select regexp_replace (fullname,'([^,]*,?)(.).*','\1\2')
from mytable
;
Here is one way, using just "standard" instr and substr. Assuming your input is a single string in the format 'Smith,John':
select substr(fullname, 1, instr(fullname, ',')+1) from yourtable;
yourtable is the name of the table, and fullname is the name of the column.
instr(fullname, ',') finds the position of the comma within the input string (it would be 6 in 'Smith,John'); thensubstrtakes the substring that begins at the first position (the1in the function call) and ends at the position calculated byinstr`, PLUS 1 (to get the first initial as well).