How to code a random letter generator in cloudbot? - variables

(Original subject line: I don't know much about coding, however I am trying to learn the script for cloudbot (SLOBS Bot for twitch). How do i code a random letter generator?)
I am trying to set a command that generates a random number and letter, i have found that {randnum.1-100} works to pick a random number 1-100, however I am struggling to find a way to have it generate a random letter.
(This is not the direct command i am adding, however it is an example to get the idea of it)
Example:
Mod: !addcommand !gpa {touser.name} has a GPA of {randnum.1-4}. They recieved all {RANDOMLETTERVARIABLE}'s this year!
JohnDoe: !gpa
Streamlabs: JohnDoe has a GPA of 3. They recieved all A's this year!
I attempted a few different suggestions, some of which included:
Attempt:
making a pastebin of every letter and typing {readapi.PASTEBINLINKHERE}
Bad Result:
It read every letter in the pastebin, not a random one.
Attempts:
{randlet.A-Z}
{random.pick "ABCDEFGHIJKLMNOPQRSTUVWXYZ"}
{random.pick "A" 'B' 'C' 'D' 'E' 'F' 'G' 'H' 'I' 'J' 'L' 'K' 'M' 'N' 'O' 'P' 'Q' 'R' 'S' 'T' 'U' 'V' 'W' 'X' 'Y' 'Z'}
Result:
Nothing in place of the variable.
Thank you in advance for any help! I personally use nightbot, and its super simple to find ways to work around it's script, but ive been struggling in learning how to work around the variables in cloudbot :/

Related

SQL computed column based on a special character on another column

I am not good with SQL at all, barely have an idea on how to do basic scripts suck as delete, drop, add.
I have this data with about 12 columns, I want to add a calculated column which will change depending if a special character shows up in another column.
lets say
A C
Money$ YES
Money NO
that is the idea, I want to create a column C where it says yes if there is a $ sign on the column A. Is this possible? I am assuming you can use something similar to an if condition but I have no experience with SQL scripting.
You would use a case expression and like:
select t.*,
(case when a like '%$%' then 'YES' else 'NO' end) as c
from t;
The following is just commentary.
This is very basic syntax for SQL. I would recommend that you spend some time to learn the basics. Learning-as-you-go is an okay approach -- assuming you have some fundamentals to build on. Otherwise, you are likely to spend a lot of time to learn a few things, and you may not learn the best way to do things.
yes, this is possible. you'll have to replace the parts in braces ({}) with the appropriate object names. I also use a bit rather than 'Yes'/'No'; as that seems better suited:
ALTER TABLE {YourTable} ADD {New Column Name} AS CONVERT(bit, CASE WHEN {Column} LIKE '%$%' THEN 1 ELSE 0 END) PERSISTED;
Note that this will return 0 if the column ({Column}) has a value of NULL, not NULL; unsure if this is the correct logic however, this should be more than enough to get the ball rolling. If not, read up on the CASE expression and NULL logic.
Regexp match can help you find out if there is a character you consider as special char in the strings:
SELECT
ColumnA
, SUBSTRING(ColumnA, PATINDEX('%[^ a-zA-Z0-9]%', ColumnA), 1) AS FirstSpecialChar
WHERE
ColumnA LIKE '%[^ a-zA-Z0-9]%'
;
The pattern [^ a-zA-Z0-9] will match on any character which is not a number, a space or an alphabetic character (note the ^ at the beginning of the character group - that mean NOT)
You can use regex to check any special character in column EX:
SQL SERVER
SELECT CASE WHEN 'ABCD$' Like '%[^a-zA-Z0-9]%' 1 THEN 'YES' ELSE 'NO' END as result
MYSQL
SELECT CASE WHEN 'ABCD$' REGEXP '[^a-zA-Z0-9]' = 1 THEN 'YES' ELSE 'NO' END as result
Regex can be changed as per the requirement
REGEXP '[^[:alnum:]]'

How to retrieve specific chars after decimal using REGEX in Oracle SQL?

I'm using RegEx in a View in Oracle 11g and I need to display certain codes that have an 'S' in the 8th position.
Using https://regexr.com/2v41h,
I was able to display these results with
REGEXP_SUBSTR(code, '\S{8}')
Y38.9X2S
Y38.9X2D
Y38.9X2A
Y38.9X1S
My issue is that I need to return only the values that have an 'S' in the last position which is the 8th position counting the decimal. What expression should I use?
Example:
Y38.9X2S
Y38.9X1S
I have tried:
REGEXP_SUBSTR(code, '\b[S]*[8]\b') AS CODE
Thank you in advance for your help.
I am thinking:
select substr(code, -8)
from t
where code like '%_______S'
If the code is always long enough, just use '%S'.
Or, as a case expression:
select (case when code like '%_______S' then substr(code, -8) end)
You will need regular expressions if code has other characters but they may not be necessary.

PostgreSQL - Converting a string with quotes to different string using case?

So hopefully this isn't a super difficult question. I've looked around but haven't been able to find an answer. Basically I have a table in a READ-ONLY db that I'm trying to use a case statement to convert a column that stores JSON values (I think?) to something more visually pleasing.
The table shows Emails, and the Status of whether or not they're subscribed to the mailing list.
EXAMPLE HERE
Basically my query looks something like this
select
f.data as "Email",
(
case f.status
when '{"value":true}' then 'Yes'
when '{"value":false}' then 'No'
else NULL
end
) as "Subscribed"
from fields f
When I run this in my example page it works just fine when set to POSTGRES 11 but when I run it on Metabase, I get an error "ERROR: operator does not exist: json = unknown" and I'm stumped on how to proceed.
Any help here would be greatly appreciated.
That error means f.status is of type json and you're trying to compare it to a string, which doesn't work.
You can try the following instead (related documentation):
case f.status->>'value'
when 'true' then 'Yes'
when 'false' then 'No'
end
as "Subscribed"

How do I extract a substring from a random position in a string using built-in functions?

I have a series of data stored in the following fashion:
Word of various kinds (ANT\username1) and even more words
This is another row, the words are random (ANT\username2)
Thankfully the username only ever shows once (ANT\username1)
Above represents three seperate rows.
The general flow of this data is:
Parenthesis can appear anywhere in the text
The username portion of each string (ANT\usernamex) will only ever appear once
The text preceeding and proceeding the username portion is always different lengths.
The username text may not always be present
As you probably already guessed what I need to do is take the username from each row and where it isn't present return null. Unfortunately I have no idea how to approach this - I've played around with left() and right() functions but don't really know how else to tackle this. Would appreciate if any answers that use a number of functions to accomplish the task have a quick blurb explaining the flow of logic (so I can then read the documentation for the functions to learn).
Note the specific results when the data is not as expected. This works for exactly the format '(ANT\....)'.
-- sample table
create table t(s varchar(max));
insert t select
'Word of various kinds (ANT\) blank' union all select
'Word of various kinds (ANT) blank' union all select
'Word of various kinds (ANT\ no closing' union all select
'Word of various kinds (ANT\(ANT\me) double up' union all select
'' union all select
'(ANT\' union all select
null union all select
'Word of various kinds (ANT\username1) and even more words' union all select
'This is another row, the words are random (ANT\username2)' union all select
'Thankfully the username only ever shows once (ANT\username1)';
-- Query
select Original = s,
Extracted = nullif(STUFF(LEFT(s, CharIndex(')',s+')',
PatIndex('%(ANT\%', s)) -1), 1,
PatIndex('%(ANT\%', s + '(ANT\')+4,''),'')
from t;

SQL Server BETWEEN problem

I have a table mapping postal code ranges to contacts. Postal codes are saved in multiple formats for different countries.
Table postcode-range:
FROM-CODE TO-CODE CONTACT-ID COUNTRY-CODE
12000 12999 18 fr
BT000000 BT9ZZZZZ 34 uk
...
To find a contact in a specific range , e.g. range starting with 123, I use the following query:
select * from postcode-range
where '123' between from-code and to-code
and country-code = 'fr'
This will return the first entry, contact-id 18.
The problem is if I search for '120' or '12' I get no results.
Any idea what's wrong here? Anybody got an alternative SQL query?
'12' < '120' < '12000'
If you change your from and to codes to '12' and '12999' for 'fr' your query as written will work, although it would include patterns such as '121AB' which presumably isn't a valid French post code. You could include other validation such as testing that the string contains only numbers, or its length.
e.g. do
like '12[0-9][0-9][0-9]'
I found this solution:
select * from postcode-range
where country-code = 'fr'
and left('12' + '00000000000000', len(from-code)) between from-code and to-code
If the query is shorter than the required length of the postal codes, then the missing characters are filled by zeros. I simply assume that no postal code is longer than 14 characters.
The country-code and query (here '12') are just place-holders.
You could use LIKE '12%'
or use lpad to get the 1st 3 characters.