How to concatenate multiple VARCHAR columns into single CLOB or LONG column in Oracle - sql

So I have several VARCHAR(4000) columns that are filled to the MAX in my Oracle SQL Table. I am trying to concatenate/combine them into a single column (LONG or CLOB). How do I do this? I've tried concatentate and obviously that doesn't work... I receive 'a result of string concatenation is too long' error.
I've tried the following:
select id, (comment_1 || comment_2)
from table
group by id;
Thanks in advance!

You can use select id, to_clob(comment1) || comment2 from table group by id. But generally better way is modify the database design and add clob column into the table.

Related

dynamically cast() values to string and unpivot in BigQuery

I have tables (of different schema) that consist of numerous rows (millions) with a unique id and at least 100-200 columns of various data types (INT64, String, Datetime, Float...etc). I need to unpivot the columns to rows dynamically and display pertaining values (including null values) in the next column. I need this only for data related to a selected id.
Here is an example of what I need.
An idea of how tables look and final result:
I wrote this code but I am getting the following error:
"Query error: The datatype of column does not match with other datatypes in the IN clause. Expected STRING, Found INT64 at [4:74]"
code I wrote:
declare myup string;
set myup=(
select concat('(',string_agg(column_name,','),')'),
from (select distinct column_name from `abc-def-
bigqueryghi.dataset_info.INFORMATION_SCHEMA.COLUMNS`
where table_name='table_1'
and column_name not in ("id")
)
);
execute immediate format("""
select*from `abc-def-bigquery-ghi.dataset_info.table_1`
unpivot
(values for column_name in %s)""",myup);
It is not possible to explicitly cast each column by name into string since some tables have up to 200 columns.
Null values also need to be displayed in final result since this needs to then be visualized on Google Data Studio.
Any ideas on how to solve this is highly appreciated.

Sum values of dictionary in SQL

I have a string column in a SQL database in the format below:
{string_one:1;string_two:2;string_three:3}
What is the best way to sum up the values in this column so that the output would be just the sum:
6
I am using postgresql
If that's a string field, then you could use a combination of regexp_replace and regexp_split_to_table.
select id, sum(number_values) from (
select id,
regexp_replace(regexp_split_to_table(col_name, ';'), '\D', '','g')::integer as number_values
from my_table
)z
group by id
The split-to-table will create virtual rows for each "delimited" value in the string, and then we replace non-numbers with nothing. Finally, that is casted to integer type.
DB-fiddle here.
This works as long as you don't have other numbers in the string that you're trying to remove. If this is a json column, then there are much better solutions. But you didn't mention json as of the time this answer was posted.

Filter unwanted characters from a column in Teradata

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)

Order by multiple columns with different types in Oracle?

I'm a newbie with Oracle. In SQLite, PostgreSQL or MSSQL I can do the following query:
SELECT * FROM users ORDER BY id, email
Here is the definition of USERS:
CREATE TABLE "USERS" (
"ID" NUMBER(38,0) NOT NULL,
"EMAIL" VARCHAR2(255)
)
Id is NUMBER type and email is VARCHAR type.
When I run the above SELECT query in Oracle it will raise the error:
ORA-00932: inconsistent datatypes: expected - got CLOB
Is there anyway to do that in Oracle?
Thank for your interest.
Seems like the email field is a CLOB and not a VARCHAR. You cannot ORDER BY a CLOB.
If your column is a CLOB then you can CAST() the field to order by:
select *
from users
order by id, cast(email as varchar2(50)) -- or whatever length you want.
See SQL Fiddle With Demo
try it with column positions also-
(this means you should not use * - which i would recommend anyway...)
select id, email from users order by 1,2
it should also be possible to order by the first portion of the CLOB by applying some string concatenation function in th eorder by clause.
Double check the real type of email column. It just seems to be CLOB which is not applicable for order by.
Obviously, there is not need to store emails that are longer than 4000 chars.
So CLOB should be replaced with VARCHAR2(255).
See What is the maximum length of a valid email address?.
There may be some problem with Oracle type mapping in your ORM.

Conversion failed when converting the varchar value '1,' to data type int

I have table categories (c) and an another table (x) with a column which can contain cat IDs separated with comma as varchar data type. I want to Select related categories but I'm having error "Conversion failed when converting the varchar value '5,' to data type int." when trying to select:
SELECT ID, Title FROM c WHERE ID IN (SELECT catIDs FROM x WHERE ID=any);
The subquery returns data like "1,3,4"
You need to split the 1,3,4 string returned by the subquery into separate int values. SQL Server does not have a built-in function to do it, but you can use this user-defined function.
Create the function dbo.Split in your database and then re-write your query as follows:
SELECT ID, Title
FROM c
WHERE ID IN
(
SELECT s
FROM dbo.Split(',', '1,3,4')
)
I replaced the subquery with example results 1,3,4 to shorten the query and make it easier to understand.
If I get it right, you actually have values like "1,3,4" in your column catIDs. So you should extract a substring in the select of your subquery.
By the way, I'm not an MS SQL Server expert, but it's probably a bad database design to do so. I'm not sure the RDBMS engine will use indexes in such a case...