I have a column type in my Postgresql db with a lot of values like Content::News or Content::Video.
I need to strip Content:: for every value in the column
Can I use?
select ltrim(type, 'Content::')
You could use replace:
select replace(type, 'Content::', '')
Related
I have a Phone number column in my table with values only being numbers and no special characters. for one of the column I got a value coming in as ":1212121212".
I will need to filter this record and any records coming in with any special characters in teradata. Can anyone help on this.
I have tried the below solutions but it is not working
where (REGEXP_SUBSTR(column_name, '[0-9]+')<>1 or column_name is null )
In MS SQL Server DB's, you can use TRYCAST to find those entries having non numeric characters:
SELECT column_name
FROM yourtable
WHERE TRY_CAST(column_name AS INT) IS NULL;
In Teradata DB's, you can use TO_NUMBER:
SELECT column_name
FROM yourtable
WHERE TO_NUMBER(column_name) IS NULL;
If you want to stay close to your attempt, can use LIKE to find not numeric entries:
SELECT column_name
FROM yourtable
WHERE column_name LIKE '%[^0-9]%';
Note this could get slow when your table has very many rows.
Thanks Jonas. Since I need only numeric values and the length should be 10, I tried the below and it worked. This would ignore all the additional special characters.
(regexp_similar(Column,'[0-9]{10}')=1)
I have a column named population which is a text type because it cannot convert those values with a comma to a numeric as seen below. I used the following query to select those: select population from total_table where population like '%,%'. Is there an efficient way in SQL to remove this comma so I can cast it to a numeric type without using an update query for each value?
You can use replace function to remove comma as shown below:
select replace(population,',','') from total_table;
If you want to update the value you would use:
update total_table
set population = replace(population, ',', '')
where population like '%,%';
Then you can alter the type of the table. In most databases, this looks something like:
alter total_table alter population int;
Or numeric or whatever type you want.
My data contains numbers like 100,000.89 and so on. What function should I use in Redshift to remove the comma and keep it like 100000.89? Do we write the function while creating a table since it is at column level or after its creation and then post process the table?
To remove commas from text columns, use replace():
select replace(col, ',', '')
from t
EDIT : In case of null data use coalesce() :
select coalesce(replace(col,',', ''), '')
from t
I just added all the columns in my insert query with coalesce since all of them somewhere had null values and it worked like a charm. The redshift error for missing data in not null fields is misleading here as mentioned : https://forums.aws.amazon.com/thread.jspa?threadID=119640
I changed my copy command too, and added BLANKSASNULL. This worked! Thanks for all your help. Below is my command:
insert into test.t_final
(select
coalesce(project_number) as project_number,
coalesce(contract_po) as contract_po,
coalesce(tracking_date) as tracking_date,
(coalesce(replace(amount,',',''))) as amount,
(coalesce(replace(tax,',',''))) as tax,
(coalesce(replace(contract_value,',',''))) as contract_value,
coalesce(comments) from test.t)
I am trying to update a table coulmn, the table has thousands of records.
Currently I am updating the table by running the following query manually for some of the records.
UPDATE MyTable
SET column = REPLACE(column, 'ABC', 'ABC9')
WHERE where column like ‘ABC%’
Now I am trying to generate a generic query to update the table by adding a letter '9' after the alphabets. Thanks for your help
Use PATINDEX and STUFF
Patindex - Helps you to identify the first occurrence of numeric character in the string
Stuff - Helps you to insert 9 before the first occurrence of numeric character in the string
UPDATE MyTable
SET column = stuff(column,patindex('%[0-9]%',column),0,'9')
I want to put a condition in my query where I have a column that should contain second position as an alphabet.
How to achieve this?
I've tried with _[A-Z]% in where clause but is not working. I've also tried [A-Z]%.
Any inputs please?
I think you want mysql query. like this
SELECT * FROM table WHERE column REGEXP '^.[A-Za-z]+$'
or sql server
select * from table where column like '_[a-zA-Z]%'
You can use regular expression matching in your query. For example:
SELECT * FROM `test` WHERE `name` REGEXP '^.[a-zA-Z].*';
That would match the name column from the test table against a regex that verifies if the second character is either a lowercase or uppercase alphabet letter.
Also see this SQL Fiddle for an example of data it does and doesn't match.
agree with #Gordon Linoff, your ('_[A-Z]%') should work.
if not work, kindly add some sample data with your question.
Declare #Table Table
(
TextCol Varchar(20)
)
Insert Into #Table(TextCol) Values
('23423cvxc43f')
,('2eD97S9')
,('sAgsdsf')
,('3Ss08008')
Select *
From #Table As t
Where t.TextCol Like '_[A-Z]%'
The use of '%[A-Z]%' suggests that you are using SQL Server. If so, you can do this using LIKE:
where col like '_[A-Z]%'
For LIKE patterns, _ represents any character. If the first character needs to be a digit:
where col like '[0-9][A-Z]%'
EDIT:
The above doesn't work in DB2. Instead:
where substr(col, 2, 1) between 'A' and 'Z'