DB2: Extract string between 2 specific delimiters - db2

I am writing a DB2 query for a Crystal Report and am running into an issue. I have a column in one of my tables that looks like this:
VISITOR=123456|ID=789
I need to extract the "123456" between the first "=" and the "|" that comes after, for all rows in that column. I was able to figure out the formula in Crystal, and I think I could accomplish it in SQL, but translating that over to DB2 is proving difficult. For instance, not being able to use CHARINDEX is a show stopper for me. Can anyone offer any assistance? Thanks.

Try REGEXP_SUBSTR scalar function.
SELECT REGEXP_SUBSTR(T.STR, '[^=]*=([^|]*)|', 1, 1, '', 1)
FROM (
VALUES
'VISITOR=123456|ID=789'
, 'str1 = 123456 | str2'
--, 'whatever string'
) T (STR);

Related

Filemaker Execute SQL with column name

I think this is a very dumb question but I just can't find how to do it.
I'm using ExecuteSQL in filemaker but I noticed the result text doesn't contain the column name, any idea how I can make the result show column name?
Thanks in advance!
You could simply add a text line to your calculation, before calling the ExecuteSQL() function, e.g.:
"SomeField,AnotherField¶"
&
Execute SQL ( "SELECT SomeField, AnotherField FROM YourTable" ; "" ; "" )
Note that you can use the GetFieldName() function to protect your calculation against fields being renamed.
You can also use a query like:
SELECT * FROM FileMaker_Fields WHERE TableName='YourTable'
to retrieve ALL field names of a table.

Postgresql select from where

I would like to look up records based on column value.
I know how to do this using the select from where command.
However I am facing an issue with the value of the column I would like to lookup values for.
The problem is that the value has a / in it.
So here is the command:
SELECT COLA FROM TABLENAME WHERE COLB = cotton/candy;
This is giving me an issue as column "cotton" does not exist.
Any help would be greatly appreciated.
Thanks in advance and best regards,
If you wrap the value in quotes this should work since it will look for an exact string match:
SELECT COLA FROM TABLENAME WHERE COLB = 'cotton/candy';
The content of the value should not matter at all.
Your syntax is invalid either with, or without, the /.
COLB = cotton/candy;
is just as wrong as
COLB = cotton;
since in neither case do you use 'quotes for literals'.
There's a broader problem though. Say you write:
COLB = '$1'
where $1 is some program variable substitution. Then someone sends the value ');DROP TABLE COLA;--. Whoops! They ran a command on your DB and dropped your table. This is called SQL injection and to avoid it, you should make sure to use bind parameters ("parameterised statements", "prepared statements") for all user-supplied or interpolated values in your queries.

SPLIT_PART() not returning results in Redshift

There are strings in a Redshift database that I'm trying to parse. The strings look like this in the object field:
yada \n foobar\n thisthing: xyz\nvegetable: amazing
The value I'm trying to get at is xyz.
I'm trying:
SELECT split_part(v.object::varchar,'\n',3) as first_parse
FROM table_name as v
Believing that will return thisthing: xyz which I can then split again on ': '.
The Redshift documentation makes me think that's valid Redshift SQL:
http://docs.aws.amazon.com/redshift/latest/dg/SPLIT_PART.html
This answer on StackOverflow also makes me believe this is valid Redshift SQL:
https://stackoverflow.com/a/20811724/1807668
However the results of that query are results that are blank in the first_parse field (not NULL, actually blank).
How should I go about getting to the xyz part of my sample string above using Redshift SQL? Any help would be appreciated.

syb_describe in DBD::Sybase

I am looking to extract Sybase datatype for all the columns in a table. When I try to achieve this using $sth->{TYPE}, I get a numeric version of the datatype (i.e. instead of sybase datatype varchar, I get 0).
From the DBD::Sybase documentation, I noticed that SYBTYPE attribute of syb_describe function might be able to produce what I am looking for. But it seems that my understanding is not proper. SYBTYPE also prints datatype in numeric form only.
Is there any way to fetch the textual representation of actual Sybase datatype (instead of the number)?
It sounds like you wish to reverse engineer the create table definition. Here is an SQL script you can use for Sybase or SQL Server tables.
select c.name,
"type(size)"=case
when t.name in ("char", "varchar") then
t.name + "(" + rtrim(convert(char(3), c.length)) + ")"
else t.name
end,
"null"=case
when convert(bit, (c.status & 8)) = 0 then "NOT NULL"
else "NULL"
end
from syscolumns c, systypes t
where c.id = object_id("my_table_name")
and c.usertype *= t.usertype
order by c.colid
go
Note: This could still be edited with a nawk script to create a real SQL schema file.
The nawk script would strip the header, add "create table my_table_name", add commas, strip the footer and add a "go".
Good SQL, good night!
I found a workaround (Note: This does not answer the question though):
What I did was simply joined the sysobjects, systypes and syscolumns system tables.

How can I map numbers to their string equivalents in Perl?

I am writing a script using DBI to execute a select query to an Oracle db. I have successfully able to capture the data but I need help to change the output.
Below is the sample output.
Type
2
6
I want to display 2=>Good and 6=>Bad
Can anyone please suggest me the Perl code to map the output?
# Create a hash of the values you want to output
my %human_text = (2 => 'Good', 6 => 'Bad');
# and then access the hash values like this:
print $human_text{2}; #will output 'Good'
print $human_text{6}; #will output 'Bad'
Usually the easiest way is to change directly the values outputted by the SQL query. With Oracle you can use DECODE.
SELECT DECODE(MY_TYPE, 2, 'TWO', 6, 'SIX', 'DEFAULT_VALUE') FROM MY_TABLE
The standard SQL way is to use a CASE conditional expression. It is a little more verbose, but more powerful and more portable. It works for example in Oracle, PostgreSQL and MS-SQL.
SELECT
CASE
WHEN MY_TYPE = 2 THEN 'TWO'
WHEN MY_TYPE = 6 THEN 'SIX'
ELSE 'DEFAULT_VALUE'
END CASE
FROM MY_TABLE
If you still want to do it in Perl, you might create a Hash. The code sample is quite trivial, and well documented in the link I provided.
Create a lookup table in your RDBMS which maps 2 to Good and 6 to Bad. Create an INNER JOIN (or LEFT JOIN if you anticipate having values that will not match the lookup) with your SQL statement (or create a VIEW which returns the JOINed tables). Trying to use Perl or SQL SELECT statements to replace database design is probably a bad idea.
$inWords = ("","very good","good","satisfactory","sufficient","poor","bad")[$number];