I am running a SQL query on some customer data. Within my Select statement I ideally need to remove all commas.(I am not looking to update my source tables.)
Not certain how to do this on all fields and all columns, so any help or insight would be much appreciated.
If you need any additional information, please let me know.
Thank you,
Allison
I assume that you need to remove the commas in any of your data (in columns)
and replacing the comma by a space
Select Replace(ColA,',',' ') as ColA, Replace(ColB,',',' ') as ColB ...
From
Related
I am very new to SQL and had a query with which I think you might be able to help me
I am trying to make a list of studies whose official_title (in the studies table) includes the keyword 'fibrillation'
I have been trying to write a code in SQL to get such information but have failed to do so, I used the following code -
SELECT * FROM studies
WHERE official_title ILIKE 'fibrillation'
I would appreciate it if you could spare a few minutes to help me out!
Thank you very much!
you can use SQL "like" to check if it contains the string you're looking for
SELECT column1, column2, ...
FROM table_name
WHERE columnN LIKE pattern;
For more information check this
https://www.w3schools.com/sql/sql_like.asp
SELECT * FROM studies
WHERE official_title LIKE '%fibrillation%'
This will work for you. If this is not what you want, let me know.
I'm cleansing a database table so that I can build a data warehouse for my coursework, however first I need to make sure that the data is of a quality.
There are a lot of name entries have only a single letter. I want to delete those rows with a single script.
You can get the number of chars in the column name with the function length()
delete from tablename
where length(trim(name)) < 2
The function trim() could also be useful in this case.
Delete from #your_table
Where length(#your_name_column)=1
This will remove all the rows with a 1 character length name
This has not been tested but REGEXP_LIKE can be very usefull for things like this.
delete from your_table
where regexp_like (column, '[A-Z]|[a-z]')
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 have a decent size table with 20+ columns and almost 3 million rows, and I want to select all the unique values from a single column and enter them into a newly created table. After research, I have attempted this using both the DISTINCT and GROUP BY approaches, but both are producing duplicate values. Furthermore, I've set the new column in the new table as a Primary Key, which I don't believe should allow duplicate values.
I'm definitely a beginner here, so perhaps there is something simple I'm doing wrong. Here's some sample code:
Using GROUP BY
INSERT INTO ResourceGroups(ResourceGroup)
SELECT ResourceGroup
FROM dbo.UsageData
WHERE ResourceGroup IS NOT NULL
GROUP BY ResourceGroup
Using DISTINCT
INSERT INTO ResourceGroups(ResourceGroup)
SELECT DISTINCT ResourceGroup
FROM dbo.UsageData
WHERE ResourceGroup IS NOT NULL
The results of both of these seem to be the same. Here's a sample of the first few rows:
ResourceGroup
aiiInnovationTime
Api-Default-Central-US
Api-Default-Central-US
applicationinsights
applicationinsights
azurefunctions-southeastasia
azurefunctions-southeastasia
The query resulted in 532 rows, and it clearly eliminated some duplicates after consolidating down from 3 million. However, there are obviously still duplicates here, and they also successfully inserted into a primary key column which shouldn't allow duplicate. Furthermore there's a blank row despite my attempt to filter out NULLs (though maybe there's a space or something there?). Needless to say, I'm a bit confused about what I'm doing wrong, and would greatly appreciate any help that this community can provide!
Both the queries you mentioned should give you unique results, the anomaly however, is due to may be leading or trailing white-spaces.
Depending on the DB you can modify the query for e.g.
For Oracle DB: You can use TRIM function which removes both leading and trailing white-spaces.
SQL Server Don't have single function you have to use LTRIM and RTRIM to remove spaces.
Assuming there are spaces in your data
SELECT DISTINCT
REPLACE(REPLACE(REPLACE(REPLACE(ResourceGroup, CHAR(13) + CHAR(10), ' ... '),
CHAR(10) + CHAR(13), ' ... '), CHAR(13), ' '), CHAR(10), ' ... ')
FROM dbo.UsageData
WHERE LTRIM(RTRIM(ResourceGroup)) IS NOT NULL
LTRIM trims leading spaces and RTRIM trims trailing spaces. Try this out and see if it works!
As Chetan Ranpariya mentioned, checked leading and trailing spaces. The way you do it depends on the SQL engine. For instance, in MySQL you can use https://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_trim.
I want to look for values in variable/column which start with 'S' and has 'gg' in between.
For instance Staggered is a word which starts with alphabet S and has gg in between the word.
so what sql query to write to get the result.
Due to the fact that you did not provide much meta information (which database?), I'll just show the following:
SELECT * FROM <table>
WHERE <columnname> LIKE 'S%gg%';
Good luck :)
As the target database is not mentioned, I will answer with Oracle syntax:
select *
from TABLE_NAME
where COL_NAME like 'S%gg%'