usage of concat function - sql

Write a query to display address details by concatenating address and city of students . Give an alias as Address and sort the result based on the concatenated column in descending order.

You can Concatenate using pipe symbol || in oracle or use + symbol in sql server.Once you concatenate give alias name (address1 || address 2)as address, sorting is done by order by clause .
provide some sample to help you

Related

Parse string into elements, where each element will get column with unique name, Bigquery sql

I'm not getting anywhere with chatgpt :)
Big query sql syntax.
Let's say I have a string of IPs separated by commas. Strings can have different lengths.
These are the strings:
First example:
'1.1.1.1, 12.12.12.12'
Second example:
'1.1.1.1, 12.12.12.12, 3.3.3.3'
Using the comma, I want to parse the string.
As a result, I would like each element to have a column name: ip_ + its position in the original string.
First example:
ip_1, ip_2,
1.1.1.1, 12.12.12.12'
Second example:
ip_1, ip_2, ip_3
1.1.1.1, 12.12.12.12, 3.3.3.3
Could you please assist me with this query?
Thanks!
Consider below approach
select * from (
select list, offset, ip
from your_table, unnest(split(list)) ip with offset
)
pivot (any_value(trim(ip)) as ip for offset + 1 in (1,2,3))
if applied to sample data in your question - output is
You can refactor above into dynamic pivot - there are plenty posts here on SO showing the technique

I need query to exclude a particular alphanumeric pattern in Oracle query

I have a column with value
String = 'Select Id,name,model_1,model_2,model_30 from employee'
I need the output to exclude ',model%'
i.e o/p should be
'Select Id,name from employee'
I used regexp_replace(string,[',model'+\d]), but this is returning numbers as well.
Assuming that your string is exactly in the form you showed, you have only some syntax issues; you need:
regexp_replace(yourString, ',model_\d+', '')
You need to replace ,model_[numbers]. You can use regexp_replace as follows:
regexp_replace(your_string, '(,model_[0-9]+)','')
Db<>fiddle

how to read & seprated data in single column

Field Description:
User_id Unique identifier of every user following these creators
Creator_id List of creator ids separated by ‘&’
User_id,Creator_IDs
U100,A300&A301&A302
U101,A301&A302
U102,A302
U103,A303&A301&A302
U104,A304&A301
U105,A305&A301&A302
U106,A301&A302
U107,A302
Note: I have to remove U and A before the values, I though I could use substring for U but what can I do for A since it is varying.
Moreover going forward I have to use this data to have distinct creator_id and subsequent user following them.
You could try using regexp_replace eg:
select regexp_replace(User_id, "^U", "")
, regexp_replace(regexp_replace(Creator_IDs, "A", ""), '&', ',')
You can use REPLACE function to remove A from the string. The function would be something like this -
SELECT REPLACE('A300&A301&A302', 'A','') AS NewString;
For the entire query -
select concat (REPLACE('U100', 'U',''),',',REPLACE('A300&A301&A302', 'A',''));
You can use this to see how it works. For your query of course you have to use the column names -
select concat (REPLACE(user_id, 'U',''),',',REPLACE(Creator_Id, 'A',''));

how to replace all numbers from a string using sql

i'm trying to return a column with every instance of a number replaced by '!'.
the following code only replaces the first instance from each row:
select project, commits, contributors, regexp_replace(address, '[0-9]', '!') as address
from repositories
1BcJBCAYqW9 should return as so !BcJBCAYqW!
but the output I get is !BcJBCAYqW9,
where the second digit does not change.
The code works as expected in MySQL and Oracle. So, I am going to guess you are using Postgres or a Postgres-derived database.
Postgres requires an additional argument to specify that all occurrences should be replaced:
select regexp_replace(address, '[0-9]', '!', 'g') as address
from (select '1BcJBCAYqW9' as address ) x

SQL Aliases ',' and + function

What is the function of ',' and + on the given following SQL script?
SELECT CustomerName, Address+', '+City+', '+PostalCode+', '+Country AS Address
FROM Customers;
' is the delimiter for a string literal. ', ' is a string containing a comma and a blank.
So obviously this query selects CustomerName plus a concatenation of address and a comma and a blank and city and a comma and a blank and country, calling this new string Address.
The DBMS in question accepts a + for string concatenation. Either additionally to or instead of the standard operator || .
This query will give you two columns: CustomerName and Address.
The Address will be composed of: Address, City, PostalCode and Country seperated by comma (,)
The + sign is used for concatenating strings in this specific SQL query.
Some DBMS like (MySql and Sybase) use the + for concatenation of strings.