Is there an equivalent to "tr" within SQL? - mysql

To remind you, tr
reads a byte stream from its standard input and writes the result to
the standard output. As arguments, it takes two sets of characters
(generally of the same length), and replaces occurrences of the
characters in the first set with the corresponding elements from the
second set. -- Wikipedia
I need to this within the context of an UPDATE statement and, ideally, without doing 26 nested REPLACE function calls.
EDIT:
For you nosy-Parkers who just have to know what I'm doing: I want to make the in-database equivalent of upsidedowntext.com. Right now, I do this:
SELECT
REVERSE(
REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(UPPER(value),
'A', 'ɐ'),
'B', 'q'),
'C', 'ɔ'),
'D', 'p'),
'E', 'ǝ'),
'F', 'ɟ'),
'G', 'b'),
'H', 'ɥ'),
'I', 'ı'),
'J', 'ظ'),
'K', 'ʞ'),
'L', 'ן'),
'M', 'ɯ'),
'N', 'u'),
'O', 'o'),
'P', 'd'),
'Q', 'b'),
'R', 'ɹ'),
'S', 's'),
'T', 'ʇ'),
'U', 'n'),
'V', 'ʌ'),
'W', 'ʍ'),
'X', 'x'),
'Y', 'ʎ'),
'Z', 'z')) from table;

Related

Flask render select based on a list of tuples

I have a data that looks like this that I send from flask render_template
a list of tuples called product_list
[(22, '1', 'false'), (94, '2', 'true'), (95, '3', 'false'), (100, '4', 'false'), (101, '5', 'false'), (102, '6', 'false'), (103, '7', 'false'), (104, '8', 'false'), (105, '9', 'false'), (106, '10', 'false'), (120, '11', 'false'), (121, '12', 'false')]
Essentially
first item in tuple is "product_id", second item is "product_version", third item is "is_product_active"
I am trying to render these tuples in a select tag on my html.
The list is ordered in a way such that first item in the list is suppose to be selected in the dropdown so I tried this
<select size="2" data-width="fit" style="line-height:2px">
<option value="{{product_list[0][0]}}" selected >Version: {{product_list[0][1]}}</option>
{% for product in product_list|slice:"1:" %}
<option value="{{product[0]}}">Version {{product[1]}}</option>
{% endfor %}
</select>
This doesn't work, flask jinja throws error saying jinja2.exceptions.TemplateSyntaxError: expected token 'end of statement block', got 'string'
Mostly coming from product_list|slice:"1:"
I essentially set the first item of the array as selected, then I am attempting to skip the first item in my array and simply add other items in the dropdown.
What is the cleaner way of doing this such that it also helps in readability of the html code.
You can use bracket notation.
{% for product in product_list[1:] %}

Transfer mySQL data structure into new relation database (comma separated to relation table)

I have a several mySQL tables where I have saved the relation ID of the child table comma separated. Now I have to transfer this entries into a new table where for each relation is one entry.
Is there an easy way to transfer import query into the correct format?
Here the data example, my old table (cat_projects) has the following entries I want convert:
-- export of table cat_projects
INSERT INTO `cat_projects` (`id`, `authors`) VALUES
(2, '4,1'),
(3, '0'),
(4, '8,4,1'),
(5, '13,12'),
(10, '19,4,1'),
(13, ''),
(14, ''),
(15, '28,27,25,12,9,1');
This entries I want just to write into the new relation table (cat_project_relation). The att_id links to the another table where I have save the settings of the old authors column:
-- att_id = 58
-- item_id = id
-- value_sorting = counting from 0 for each item_id
-- value_id = for each relation one entry value
INSERT INTO `cat_project_relation` (`att_id`, `item_id`, `value_sorting`, `value_id`) VALUES
(58, 2, 0, '4'),
(58, 2, 1, '1'),
(58, 3, 0, '0'),
(58, 4, 0, '8'),
(58, 4, 1, '4'),
(58, 4, 2, '1'),
(58, 5, 0, '13'),
(58, 5, 1, '12'),
(58, 10, 0, '19'),
(58, 10, 1, '4'),
(58, 10, 2, '1'),
(58, 13, 0, ''),
(58, 14, 0, ''),
(58, 15, 0, '28'),
(58, 15, 1, '27'),
(58, 15, 2, '25'),
(58, 15, 3, '12'),
(58, 15, 4, '9'),
(58, 15, 5, '1');
I hope it is clear what I try to achieve. Is it possible to do that directly in SQL or do I have to apply an external bash script?
Actually it wasn't as difficult as I thought to write a little bash script. And I guess its the easier solution then program something in SQL.
#!/bin/bash
# input table
table="(2, '4,1'),
(3, '0'),
(4, '8,4,1'),
(5, '13,12'),
(10, '19,4,1'),
(13, ''),
(14, ''),
(15, '28,27,25,12,9,1');"
# fixed attribute id
att_id=58
# read each line into an array
readarray -t y <<<"$table"
# for each array item (each line)
for (( i=0; i<${#y[*]}; i=i+1 ))
do
z=${y[$i]}
# split by comma into array
IFS=', ' read -r -a array <<< "$z"
# loop through each value
for (( j=0; j<${#array[*]}; j=j+1 ))
do
# remove all other characters then number
nr=$(echo ${array[$j]} | sed 's/[^0-9]*//g')
# each first value is the item_id
if [ $j -eq 0 ]
then
item_id=$nr
else
k=$(expr $j - 1)
value_id=$nr
# print output line by line
echo "($att_id, $item_id, $k, '$value_id')," >> output.txt
fi
done
done
The result will be as the on asked in the question.

Print list to csv file in Python3

I have a list like this.
all_chords = [['C', 'C', 'E', 'G'],
['CM7', 'C', 'E', 'G', 'B'],
['C7', 'C', 'E', 'G', 'Bb'],
['Cm7', 'C', 'Eb', 'G', 'Bb'],
['Cm7b5', 'C', 'Eb', 'Gb', 'Bb'],
['Cdim7', 'C', 'Eb', 'Gb', 'Bbb(A)'],
['Caug7', 'C', 'E', 'G#', 'Bb'],
['C6', 'C', 'E', 'G', 'A'],
['Cm6', 'C', 'Eb', 'G', 'A'],
]
I want to print out to a csv file, something like this.
C_chords.csv
C;C,E,G
CM7;C,E,G,B
C7;C,E,G,Bb
Cm7;C,Eb,G,Bb
Cm7b5;C,Eb,Gb,Bb
Cdim7;C,Eb,Gb,Bbb(A)
Caug7;C,E,G#,Bb
C6;C,E,G,A
Cm6;C,Eb,G,A
It has two fileds which are separted by semicolon. (not by a comma)
I used csv module, like this.
myfile = open('C_chords.csv','w')
wr = csv.writer(myfile, quotechar=None)
wr.writerows(all_chords)
myfile.close()
The result is..
C,C,E,G
CM7,C,E,G,B
C7,C,E,G,Bb
Cm7,C,Eb,G,Bb
Cm7b5,C,Eb,Gb,Bb
Cdim7,C,Eb,Gb,Bbb(A)
Caug7,C,E,G#,Bb
C6,C,E,G,A
Cm6,C,Eb,G,A
Should I modify the list? Somewhat like this?
[['C',';', 'C', 'E', 'G'],.......]
or any other brilliant ideas do you guys have?
Thanks in advance.
You're writing four columns, not two, if you want the last list elements be one single column, you need to join them first manually.
And you need to change the delimiter if you want the csv semicolon separated, not the quote character:
import csv
all_chords = [['C', 'C', 'E', 'G'],
['CM7', 'C', 'E', 'G', 'B'],
['C7', 'C', 'E', 'G', 'Bb'],
['Cm7', 'C', 'Eb', 'G', 'Bb'],
['Cm7b5', 'C', 'Eb', 'Gb', 'Bb'],
['Cdim7', 'C', 'Eb', 'Gb', 'Bbb(A)'],
['Caug7', 'C', 'E', 'G#', 'Bb'],
['C6', 'C', 'E', 'G', 'A'],
['Cm6', 'C', 'Eb', 'G', 'A'],
]
myfile = open('C_chords.csv','w')
wr = csv.writer(myfile, delimiter=';')
wr.writerows([c[0], ','.join(c[1:])] for c in all_chords)
myfile.close()
I think it's easier to do it without the csv module:
with open('C_chords.csv','w') as out_file:
for row in all_chords:
print('{};{}'.format(row[0], ','.join(row[1:])), file=out_file)

Compare two strings according to defined values in a query

I have to compare two string fields containing letters but not alphabetically.
I want to compare them according to this order :
"J" "L" "M" "N" "P" "Q" "R" "S" "T" "H" "V" "W" "Y" "Z"
So if I compare H with T, H will be greater than T (unlike alphabetically)
And if I test if a value is greater than 'H' (> 'H') I will get all the entries containing the values ("V" "W" "Y" "Z") (again, unlike alphabetical order)
How can I achieve this in one SQL query?
Thanks
SELECT *
FROM yourtable
WHERE
FIELD(col, 'J', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'H', 'V', 'W', 'Y', 'Z') >
FIELD('H', 'J', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'H', 'V', 'W', 'Y', 'Z')
^ your value
Or also:
SELECT *
FROM yourtable
WHERE
LOCATE(col, 'JLMNPQRSTHVWYZ')>
LOCATE('H', 'JLMNPQRSTHVWYZ')
Please see fiddle here.
You can do
SELECT ... FROM ... ORDER BY yourletterfield='J' DESC, yourletterfield='L' DESC, yourletterfield='M' DESC, ...
The equality operator will evaluate to "1" when it's true, "0" when false, so this should give you the desired order.
There's actually a FIELD() function that will make this a bit less verbose. See this article for details.
SELECT ... FROM ... ORDER BY FIELD(yourletterfield, 'J', 'L', 'M', ...)

SQL Server : how does the Replace function work as its acting strange for me

I am 'randomising' some strings in a SQL Server table to do a primitive encryption on them.
I have a nested SQL replace function around 35 times (A-Z,1-9) that basically takes every letter in the alphabet and number and replaces it with another letter or number. example of which would be
Replace(Replace(Replace('a', 'c'), 'b', 'a'), 'c', 'b')
I figured that the replace function would go though a string like 'abc' and replace everything once and stop - 'cab'. It doesn't!
It seems to want to change some characters again resulting in 'abc'->'cab'->'ccb'.
This is fine except if I have another string called 'aac' this could result in duplicate string and I lose traceability back to original.
Can anyone explain how I could stop REPLACE() partially going back over my string?
SELECT * INTO example_temp FROM example;
Update KAP_db.dbo.example_temp Set col1 = replace(replace(replace(replace(replace(
replace(replace(replace(replace(replace(‌​replace(replace(replace(replace(replace(
replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(
replace‌​(replace(replace(replace(replace(replace(replace(replace(replace(replace(
col1, 'A', 'N'),'B', 'O'), 'C', 'P'), 'D', 'Q'), 'E', 'R'), 'F', 'S'), 'G', 'T'),
'H', 'U'), 'I', 'V'), 'J', 'W'), 'K', 'X'), 'L', 'Y'), 'M', 'Z'), 'O', 'A'), 'P', 'B'),
'Q', 'C'), 'R', 'D'),'S', 'E'),'T', 'E'),'U', 'E'),'V', 'F'),'W', 'G'),'X', 'H'),
'Y', 'I'),'Z', 'J'), '1', '9'),'2','8'),'3','7'),'4','6'),'5','5'),'6','4'),'7','3'),
'8','2'),'9','1'),' ','');
The above results in '8EVHUAB' and '8EVHHAB' both outputting '2DFEENA'
Update -------------------------------------------------------------------
Ok i have redone the code and so far have:
DECLARE #Input AS VarChar(1000)
DECLARE #i AS TinyInt
Declare #Substring AS VarChar(1000)
Declare #Prestring AS VarChar(1000)
Declare #Poststring AS VarChar(1000)
Select #Input='ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789'
SELECT #i = 1
Select #Substring ='na'
WHILE #i <= LEN(#Input) BEGIN
Select #Prestring = SUBSTRING(#Input,-1,#i)
Select #Poststring = SUBSTRING(#Input,#i+1,LEN(#Input))
SELECT #Substring = replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace
(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace
(SUBSTRING(#Input,#i,1), 'A', 'N'),'B', 'O'), 'C', 'P'), 'D', 'Q'), 'E', 'R'), 'F', 'S'), 'G', 'T'), 'H', 'U'), 'I', 'V'), 'J', 'W'), 'K', 'X'), 'L', 'Y'), 'M', 'Z'), 'N', 'A'), '0', 'B'), 'P', 'C')
, 'Q', 'D'),'R', 'E'),'S', 'E'),'T', 'E'),'U', 'F'),'V', 'G'),'W', 'H'),'X', 'I'),'Y', 'J'), '1', '9'),'2','8'),'3','7'),'4','6'),'5','5'),'6','4'),'7','3'),'8','2'),'9','1'),' ','')
Select #Input = #Prestring + #Substring + #Poststring
SELECT #i = #i + 1
print 'END
'
END
This doesnt work correctly though, the code does not execute as its written, any suggestions?
Why you're seeing this: replace is a function; all it knows are its arguments. replace(replace('aba', 'a', 'b'), 'b', 'a') is absolutely equivalent to replace('bbb', 'b', 'a'), because the outer replace has no way of knowing that its first argument was created by a different call to replace. Does that make sense?
You can think of it just like a function in algebra. If we define f(x) = x2, then f(f(2)) = f(22) = f(4) = 42 = 16. There's no way to tell f to behave differently when its argument is f(2) from when its argument is 4, because f(2) is 4.
Similarly, replace('aba', 'a', 'b') is 'bbb', so there's no way to tell replace to behave differently when its first argument is replace('aba', 'a', 'b') from when its first argument is 'bbb'.
(This is usually true in computer science. Functions in computer science aren't always like functions in algebra — for example, they frequently actually do things, rather than just returning a value — but it's usually the case that they receive arguments as values, or as opaque references to values, and have no way of knowing where they came from or how they were constructed.)
How to address this: I don't think there's any very clean way to do this. Gordon Linoff suggested that you could use intermediate placeholder characters (specifically — lowercase letters) that don't exist in the initial string and don't exist in the final string, so that you can safely replace them without worrying about interference; and I think that's probably the best approach.
Your results are not surprising, since each replace is returning the string to the next level. There is no way to distinguish between the original character value and the replaced value, when they are the same character.
If you were only working with alpha characters you could do the following.
Change the collation of the original string.
Upper case the string
Replace the upper case letters with the lower case
Lower case the entire string at the end (should be redundant)
Unfortunately, I can't think of an analog for numbers that would work the same way.
Here is a link to a site that has code for the function http://www.dbforums.com/microsoft-sql-server/1216565-oracle-translate-function-equivalent-sql-server.html. The equivalent function in Oracle is called translate.
The Replace() function simply performs the operation and returns. It doesn't keep state to the next Replace(). It doesn't prevent a later invocation from replacing the characters you previously replaced. To cycle characters around, you have to have an additional "placeholder" value, and then take care to not replace a character that was already change from something else.
First, let me show you an analogy:
There are three buckets full of an equal number of marbles. The buckets are labeled "A", "B", and "C". You must perform the following instructions:
Pour all marbles from A into C.
Pour all marbles from B into A.
Pour all marbles from C into B.
What result would you expect to have? The answer is: an empty bucket C, and B having twice the number of marbles as are in A.
If you want to preserve the marbles in their original counts, you have to have four containers so that you don't mix the marbles while moving them:
Pour all marbles from C into X.
Pour all marbles from A into C.
Pour all marbles from B into A.
Pour all marbles from X into B. (rather than from C--which already has A's marbles)
Now you have the results you expected, and can discard the empty bucket X.
Try this expression and see if it gives what you want:
Replace(Replace(Replace(Replace('c', char(1)), 'a', 'c'), 'b', 'a'), char(1), 'b')