Trimming a part of string in sql needed for datatrieve - sql

I have a column called notation in the correspondence table in our database. The values are as such for this column:
notation
SSQSUPER405575681 || 032-797 || 034828 1141289
SGQSUPER405575091 || 032-797 || 034828 1140802
SGQSUPER395729693 || 032-797 || 034828 1061943
I want to have this value only SSQSUPER405575681 or SGQSUPER405575091 for notation column and trim the rest part of the string such as || 032-797 || 034828 1141289 in the 1st row and || 032-797 || 034828 1140802 in 2nd row.
Please tell me how can i do that.

Well, Sybase functions are often pretty similar to T-SQL for SQL Server, so to extract the desired substrings out the notation column, try:
select
substring([notation], 1, patindex ('%||%',[notation]) - 1) as f1,
substring([notation],
patindex ('%||%',[notation]),
len([notation]) - patindex ('%||%',[notation]) ) as f2
from [correspondence]
If that doesn't work exactly, you might see if there is some variation in the way the Sybase functions work.

Related

What's the best way to concat two integers for SQL Redshift?

I'm trying to create a simple key value that will look like 'subid_pid' where subid and pid are integers.
I tried doing concat(subid+'_'+pid) and I'm trying to figure out what I did wrong
+ is string arithmetic. To concatenate strings, use the || operator. Redshift should convert numbers automatically. So, you only need:
(subid || '_' || pid)
If you really want an explicit conversion, then text is convenient:
(subid::text || '_' || pid::text)
You can convert the integers to strings and concatenate them with ||:
subid::varchar(20) || '_' || pid::varchar(20)

How to use a column named as keyword in a DB2 concat function

So I got this table FOO which has a column of the type VARCHAR and is named COMMENT (which happens to be a reserved keyword).
When I am trying to use it in a CONCAT function in my select the result is NULL.
How can I fix this?
SELECT
CONCAT(CONCAT(CONCAT(CONCAT('{"NAME":"', NAME), '","COMMENT":"'), COMMENT),'"}')
FROM
SOMESCHEMA.FOO
I also tried to use " or ' around COMMENT, but then it is interpreted as a VARCHAR...
2nd I used ` but that happens to print me the following error.
[Code: -104, SQL State: 42601] ILLEGAL SYMBOL "`". SOME SYMBOLS THAT MIGHT BE LEGAL ARE:.
I also tried to add the SCHEMA and the TABLE name in front of the column like:
CONCAT(CONCAT(CONCAT(CONCAT('{"NAME":"', NAME), '","COMMENT":"'), SOMESCHEMA.FOO.COMMENT),'"}')
But no luck.
Did you try this?
SELECT CONCAT(CONCAT(CONCAT(CONCAT('{"NAME":"', NAME
), '","COMMENT":"'
), "COMMENT"
),
'"}')
FROM SOMESCHEMA.FOO
That is, double quotes only around the column name.
I would find this simpler to read using the infix operator:
SELECT '{"NAME":"' CONCAT NAME CONCAT '","COMMENT":"' CONCAT "COMMENT" CONCAT '"}'
FROM SOMESCHEMA.FOO
or:
SELECT '{"NAME":"' || NAME || '","COMMENT":"' || "COMMENT" || '"}'
FROM SOMESCHEMA.FOO
It seems like DB2 also accept the ANSI/ISO SQL || concatenation:
SELECT
'{"NAME":"' || NAME || '","COMMENT":"' || COMMENT || '"}'
FROM
SOMESCHEMA.FOO

What is the meaning of || '|' || in oracle?

I see a query with this "COUNT(DISTINCT(code || '|' description)":
SELECT ...,..., ...,NULL, COUNT(DISTINCT(code || '|' description)
FROM....
But Im not understanding what this "COUNT(DISTINCT(code || '|' description)" means? Do you know what it is? Thanks
In Oracle, the double pipe (||) stands for string concatenation. This:
select 'a' || 'b' from dual
Yields:
ab
When it comes to this expression: COUNT(DISTINCT(code || '|' description):
it is invalid sql code: parentheses are not balanced, and there is a missing concatenation operator; I suppose you meant COUNT(DISTINCT code || '|' || description)
the latter concatenates code and description with a | separator, and counts distinct resulting values
|| is the string concatenation operator
This code has a slight syntax error but looks like it was supposed to concat the code column with the description column values separated by a pipe
code, description
A123, code for blah blah
Becomes:
A123|code for blahblah
(If the sql is amended to code || '|' || description
Presumably you intend:
COUNT(DISTINCT code || '|' || description)
The purpose of this code is to count the unique combinations of code and description. COUNT(DISTINCT) in Oracle takes only one argument. So, if you want to count distinct combinations, you need to resort to tricks such as concatenating the values together.
This is putting the values together with a vertical bar in-between:
'NY' || '|' || 'New York' --> 'NY|New York'
The vertical bar is a separator that presumably does not occur (or only rarely occurs) in either code or description.

Return numerical values at the end of string - Presto SQL

I am trying to return the numerical values at the end of a string, in their own column.
Example - adfjdakfadkf || adfjadkfajd || adjfkad || dajlaj (123456789)
The numbers change in length throughout the table, so it would need to be dynamic.
I was using split to map with '(' and ')' as delimiters, but am having no luck.
Thanks in advance!
You can use regexp_extract() to achieve this. For example:
WITH data(c) AS (
VALUES 'adfjdakfadkf || adfjadkfajd || adjfkad || dajlaj (123456789)'
)
SELECT regexp_extract(c, '\((\d+)\)', 1)
FROM data
produces:
_col0
-----------
123456789
(1 row)

Concatenate string in Oracle SQL? (wm-concat)

I've got some SQL that I'd like to format correctly for a mailout (generated directly from SQL - don't ask!). The code is as follows:
SELECT wm_concat('<br>• ' || FIELD1 || ' ' || FIELD2 || ' : ' || FIELD 3 || ' text') AS "Team"
Okay, so this kinda works - but it places a comma at the end of each line. Silly question, and possibly quite trivial, but is there anyway at all to remove the comma please? I think it's being added by the wm_concat function
Thanks
Yes the WM_CONCAT function puts a comma between each value it concatenates.
If there are no commas in your data you could do this:
SELECT replace (wm_concat('<br>• ' || FIELD1 || ' ' || FIELD2 || ' : '
|| FIELD 3 || ' text'),
',', null) AS "Team"
If you are on 11G you can use the new LISTAGG function instead:
SELECT LISTAGG ('<br>• ' || FIELD1 || ' ' || FIELD2 || ' : '
|| FIELD 3 || ' text')
WITHIN GROUP (ORDER BY <something>) AS "Team"
That will produce a result without commas.
Just trim the string for trailing commas:
RTRIM( wm_concat(...), ',' )
Oracle 10g provides a very convenient function wm_concat used to solve line reclassified demand, very easy to use this function, but the function provides only ',' this kind of delimiter.
In fact, as long as some simple conversion you can use other delimiters separated, the first thought is replace function
with t as( select 'a' x from dual union select 'b' from dual )
select replace(wm_concat(x),',','-') from t;
But taking into account the string itself may contain ',' character, use the above SQL will lead to erroneous results, but also made some changes to the above SQL.
with t as( select 'a' x from dual union select 'b' y from dual)
select substr(replace(wm_concat('%'||x),',%','-'),2) from t;
In the above SQL by a '%' as a separator, and then replace the '%' to remove the error. The program assumes that the string does not exist within the '%' string to replace the '%' in the SQL can also use other special characters.
Source: http://www.databaseskill.com/3400944/
You can create your own aggregate functions in Oracle and use those to aggregate strings.
Or use the StrAgg function written by Tom Kyte: http://www.sqlsnippets.com/en/topic-11591.html
SELECT StrAgg('<br>• ' || FIELD1 || ' ' || FIELD2 || ' : ' || FIELD 3 || ' text') AS "Team"
FROM Abc