Removing leading zeroes in a select* SQL - sql

I want to strip the leading zeroes in certain columns of my select statement. I know how to do this if the column names are listed:
For example, in
SELECT
a,b,c.... if I want to trim column b, I simply do
SELECT a, TRIM(LEADING '0' FROM b) new name, c....
Now I also want to do the same for a SELECT* statement..
Suppose I have SELECT *, and I want to trim the leading zeroes for column b only. Is there an alternative to go as to convert the SELECT * to a normal select by listing out all columns? It becomes tedious this way.

This has been answered here: Removing leading zeros from varchar sql developer
select ltrim('000012345', '0') from dual;
LTRIM
-----
12345

Related

Finding numeric values in varchar field

sorry if this is a duplicate, I wasn't able to find what I was looking for in the answered questions.
I'm looking to query for only records with a field formatted like this numbers (0-9), hyphen (-), number (0-9), hyphen (-), numbers (0-9). This is what I have tried:
SELECT *
FROM TABLE_1
WHERE LTRIM(RTRIM(LOC_NAME)) LIKE '[0-9]-[0-9]-[0-9]'
The result set I'm looking for would be 123456-123-1234.
I thought at first they may have spaces so I trimmed the field but still no results are showing with the ABOVE query. The BELOW query returns them, but with other results:
SELECT *
FROM TABLE_1
WHERE LOC_NAME LIKE '%[0-9]-[0-9]%'
But I would get results like 1-2-3 Place...
I think this does what you want:
SELECT *
FROM TABLE_1
WHERE LTRIM(RTRIM(LOC_NAME)) NOT LIKE '%[^-0-9]%'
This checks that the field has no non-hyphens or non-digits.
If you specifically want two hyphens, separated by digits, then:
WHERE LTRIM(RTRIM(LOC_NAME)) NOT LIKE '%[^-0-9]%' AND
LTRIM(RTRIM(LOC_NAME)) LIKE '[0-9]%-[0-9]%-[0-9]%' AND
LTRIM(RTRIM(LOC_NAME)) NOT LIKE '%-%-%-%'
The second pattern requires at least two hyphens and a digit in all three parts of the name. The third forbids three hyphens.
I would do this way
select *
from table_1
where isnumeric(replace(LOC_NAME, '-','')) = 1;
Update (2018-Jun-12)
After reading the comments of #EzLo, I realized that the OP may just need two hyphens (no more, no less), so I am updating my answer with the following demo code
create table #t (LOC_NAME varchar(100));
go
insert into #t (loc_name)
values ('a12-b12-123'), ('123456-123-11'), ('123-123-123-123')
go
select *
from #t --table_1
where isnumeric(replace(LOC_NAME, '-','')) = 1
and len(loc_name)-len(replace(LOC_NAME, '-',''))=2
The result is:

how to remove spaces, special characters and numbers in one oracle sql statement

how to remove spaces, special characters and numbers in one oracle sql statement
EDIT
tried so far:
SELECT REGEXP_REPLACE(NAME ,'[^[:alnum:]'' '']', NULL) FROM table
and
SELECT translate(NAME,'[0-9]-/^%#-$&&!_. ','[0-9]') last_names FROM table
Assuming that you want to delete all the characters different from a letter, you can try the following:
select regexp_replace('admin1 xxx', '[^[:alpha:]]', null)
from dual;

Extract characters to the right of a delimited value in a SELECT statement

I need to extract all the characters to the right of a hyphen as part of a select statement. There will be other columns in the select. In the below query, the right three characters are selected from the second column. How would I extract an indefinite number of characters to the right of a delimiter – in my case a hyphen? Can I use the right function? Do I need to use another function?
Select column1, right(column2,3) as extracted, column3
From myTable
I am using SQL Server 2008.
This question has a database specific answer.
If using SQL Server:
SELECT column1
, RIGHT(column2,CHARINDEX('-',REVERSE(column2))-1) as extracted
, column3
FROM myTable
You can add a CASE statement or use NULLIF() in case the hyphen isn't always present:
SELECT column1
, CASE WHEN column2 LIKE '%-%' THEN RIGHT(column2,CHARINDEX('-',REVERSE(column2))-1)
END as extracted
, column3
FROM myTable
Or:
SELECT column1
, RIGHT(column2,NULLIF(CHARINDEX('-',REVERSE(column2)),0)-1) as extracted
, column3
FROM myTable
If using MySQL just change CHARINDEX() to LOCATE(). I believe Oracle it's INSTR() and the first two parameters are switched, first it's the string you're searching in, then the string you're searching for.
How about:
SUBSTRING(column2, CHARINDEX('-',column2)+1, 8000)
(Replace 8000 with the definition of the column.)
Of course if the column might not always contain a hyphen, then you can say:
SUBSTRING(column2, COALESCE(NULLIF(CHARINDEX('-',column2)+1,1),1),8000)
If the column does not contain a hyphen, you'll get the whole column. If you want to exclude those from the result, you can add a WHERE clause:
WHERE CHARINDEX('-', column2) > 0
Or you can use a CASE expression like Goat CO's answer.

Select number of comma delimited list items

I have a column that has comma seperated values and I need to select all rows that have 13 commas. They seperate numbers so I don't need to worry about any strings that contain commas. How would I do this?
alternative to like (I do not like the like, and the above will fail if contains 14 commas or more)
select * from table
where length(replace(your_column, ',', ''))=length(your_column)-13;
for better utilize the index, you should seek to normalize your table
If you're using PostgreSQL, you could also use regular expressions.
However, a better question might be why you have a single column with comma-separated values instead of multiple columns.
If you count a string with 14 commas as having 13 commas, then this will work:
SELECT * FROM table WHERE column LIKE '%,%,%,%,%,%,%,%,%,%,%,%,%,%'
% matches any string (including zero length).
In PostgreSQL you can do:
select col from table where length(regexp_replace(col, '[^,]', '', 'g')) = 13;

Oracle -- finding values with leading or trailing spaces

I am trying to find if a certain column requires TRIM function on it.
How can I find out if this column in a table has records that have white space either before or after the actual data.
You can check it using the TRIM function itself, not the most efficient but accurate:
Select *
From TableA
Where MyColumn <> TRIM(MyColumn)
Though if you're checking then turning around to trim anyway, you probably want to just do it in the first place, like this:
Select TRIM(MyColumn) as TrimmedMyColumn
From TableA
A quick and dirty way
WHERE LENGTH(TRIM(COL1)) <> LENGTH(COL1)
So why can't you use the following to find the leading spaces? I've been able to identify the records with leading spaces this way and using '% ' to find the trailing spaces.
SELECT mycolumn
FROM my_table
WHERE mycolumn LIKE ' %'
I've also used the following to remove both the leading and trailing spaces
Update My_table set Mycolumn = TRIM(Mycolumn)
which seems to work just fine.
You could use regular expressions in Oracle.
Example:
select * from your_table
where regexp_like(your_column, '^[ ]+.*')
or regexp_like(your_column, '.*[ ]+$')
select data1, length(data1)-length(replace(data1,' ','')) from t;
Following query will retrieve rows when one of Table fields T$DSCA has trailing spaces at the end:
SELECT * from TABLE_NAME A WHERE RAWTOHEX(SUBSTR(A.T$DSCA, LENGTH(T$DSCA),1)) ='A0' AND TRIM(T$DSCA) is not null;