Doing a group by on a column gives this result:
select column, count(column) from table group by column
column | count(column)
P | 3123
P | 145
I figured I must have a 'P' with trailing spaces. However, after using the function TRIM(column) and OREPLACE(column,' ',''), like so:
select TRIM(column), count(TRIM(column)) from table group by TRIM(column)
.. I still get the same results.
Is column a CHAR? If yes, try to CAST your column as VARCHAR.
It can also be an unbreakable space (these are invisible and not trimmed), or a different P character.
Related
I will need to extract the year from a column name , , it is returning null value and the same number of character. i would want to only extract the date however there is a few column with the same number of character .
sample data in table
10020020
1172053041
597246141
3339110821
26590621
192133643
20190203
20180109
20170204
20190904
I have tried this,
select regexp_extract((colname), '([0-9]{8})', 1) from tablename
however it is returning the result that has the same number of characters with null values. i wish to only extract only the date which is 20170109,20190204 etc etc . what is the best approach and what did i go wrong ?
10020020
26590621
20190203
20180109
20170204
20190904
i have tried using wildcard select regexp_extract((maxvalue), '([0-9]{8})', 1) like '%2019%' from profilingoverviewreport but it returning boolean instead
If you want to match values which have exactly 8 digits, and those 8 digit values correspond to dates, then I suggest the following pattern:
^(20|19)[0-9]{6}$
Your updated SQL code:
SELECT *
FROM tablename
WHERE colname IREGEXP '^(20|19)[0-9]{6}$';
Check the demo to see the regex pattern correctly identifying the dates in your column:
Demo
I have 10 columns and their values could be either null, or a name of a fruit.
I would like to add another column with all the fruits that every row has. I have used Concat(column1 , column2,..., column10) as name.
Issue : There are no commas coming on the result and if I add the comma before concatenating, we are having them together, the last word is also a comma.
Any ideas?
Thanks!
You can use the standard concatenation (||) in conjunciton with COALESCE function, which returns the value of the first non-null argument.
Example:
select coalesce(column1||',', '')||coalesce(column2||',', '')|| ... ||coalesce(column10||, '');
I want to add leading spaces in one of the column of the table. This ID column has data type Char(6).
Example: Table1
ID
1234
5678
when I do select * from Table1. and save file into .csv with pipeline delimited.
It show spaces at the end of number.
Current output:
|1234 |
|5678 |
desired output
| 1234|
| 5678|
You'd need to trim the value to remove the trailing spaces and then lpad it to add the leading spaces
select lpad(trim(id),6)
from your_table
Here is a sqlfiddle example that shows the steps
Try:
select LPAD(trim(id), 2) from table
I'm trying to substring the last 3 digits from a column. Problem is, some of them have a random space at the end, which then only returns 2 of the 3 needed values from the column. Right now I have:
SELECT substr(infovalue, -3)
FROM TABLE
WHERE INFOCODE = 555
How can I get my statement to ignore the space at the end?
thanks!
I guess you are in Oracle. Apply an rtrim to remove all trailing spaces
SELECT substr(rtrim(infovalue), -3)
FROM TABLE
WHERE INFOCODE = 555
And please specify the vendor & product version for all your questions.
I am trying to find and compare the length of a field value in table.
Table1
Id Name
1 abcd
2 xyz
3 Y
I am trying to find the name with length more than 3. The result should have only record with id 1
I tried LEN. Its not in db2 . i tried length . Its giving the field length ,not the actaul length of the value in that field.
select id,name,LENGTH(name) as namelength from table1
group by id,name having LENGTH(name)>3
Your field is probably defined as CHAR, which pads the remaining length with spaces. You can TRIM this off, and you'll get what you're looking for:
SELECT id,name,LENGTH(TRIM(name)) AS namelength
FROM table1
GROUP BY id,name
HAVING LENGTH(TRIM(name)) > 3
This assumes Linux/Unix/Windows DB2. If you're on the Mainframe z/OS DB2, then you'll use RTRIM instead of TRIM.
You can check the condition in where clause itself,
select id,name,LENGTH(name) as namelength from table1 where LENGTH(name)>3