postgresql - how to align columns when selecting cyrillic text (utf-8)? - postgresql-9.1

My database is utf-8. When selecting a text field from a table it looks like this:
postgres=# select '>'||exp_type||'<', exp_id from t_exp_types where exp_id in (3,11,12,15);
+-----------------------------------------+--------+
| ?column? | exp_id |
+-----------------------------------------+--------+
| >храна< | 3 |
| >почивка< | 11 |
| >превод< | 12 |
| >изравнителен превод< | 15 |
+-----------------------------------------+--------+
(4 rows)
How can I make the columns aligned properly? psql (PostgreSQL) 9.1.3
solution: my locale LC_ALL was set to 'C'. Changing to LC_ALL= fixed the problem

It works fine here:
regress=> WITH t_exp_types(exp_id, exp_type) AS (VALUES (3, 'храна'), (11, 'почивка'), (12, 'превод'), (15, 'изравнителен превод'))
regress-> select '>'||exp_type||'<', exp_id from t_exp_types where exp_id in (3,11,12,15);
?column? | exp_id
-----------------------+--------
>храна< | 3
>почивка< | 11
>превод< | 12
>изравнителен превод< | 15
(4 rows)
Fedora 19, Konsole 2.11.2 from KDE 4.11.2. My locale is:
$ locale
LANG=en_US.UTF-8
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=
Odd symptoms, though; a simple client_encoding mismatch would mangle the characters, not just the whitespace.

Related

Why SHOW ERRORS or SHOW WARNINGS lists only the very last error/warning?

I am using the SHOW ERRORS and SHOW WARNIGS statments (on MariaDB 10.10.2) and regardless of the max_error_count is in its default 64 value, the statements above only list the very last error/warning.
Question
How can I list all the last recent errors/warning according the max_error_count variable?
The SQL statement SHOW ERRORS/WARNINGS only returns error or warnings for the last statement which failed or produced a warning. They can be retrieved until another statement produced an error/warning.
If a statement produced more than one error/warnings, then the SHOW ERRORS/WARNINGS return up to max_error warnings.
Example:
delimiter $$
CREATE PROCEDURE p1() BEGIN DROP TABLE whichdoesnotexist; END $$
CREATE PROCEDURE p2() BEGIN CALL p1(); END $$
CREATE PROCEDURE p3() BEGIN CALL p2(); END $$
delimiter ;
SET ##max_error_count=2;
CALL p3();
SHOW WARNIINGS;
+-------+------+----------------------------------------+
| Level | Code | Message |
+-------+------+----------------------------------------+
| Error | 1051 | Unknown table 'test.whichdoesnotexist' |
| Note | 4094 | At line 2 in test.p1 |
+-------+------+----------------------------------------+
SET ##max_error_count=4;
CALL p3();
SHOW WARNINGS;
+-------+------+----------------------------------------+
| Level | Code | Message |
+-------+------+----------------------------------------+
| Error | 1051 | Unknown table 'test.whichdoesnotexist' |
| Note | 4094 | At line 2 in test.p1 |
| Note | 4094 | At line 2 in test.p2 |
| Note | 4094 | At line 2 in test.p3 |
+-------+------+----------------------------------------+
# This will not clear warning/errors
SELECT "1","2","3";
+---+---+---+
| 1 | 2 | 3 |
+---+---+---+
| 1 | 2 | 3 |
+---+---+---+
SHOW WARNINGS;
+-------+------+----------------------------------------+
| Level | Code | Message |
+-------+------+----------------------------------------+
| Error | 1051 | Unknown table 'test.whichdoesnotexist' |
| Note | 4094 | At line 2 in test.p1 |
| Note | 4094 | At line 2 in test.p2 |
| Note | 4094 | At line 2 in test.p3 |
+-------+------+----------------------------------------+
SHOW ERRORS and SHOW WARNINGS only show errors/warnings from the most recent statement in the same session. They will never show anything from previous statements or other sessions. max_error_count will reduce the number shown, but never cause anything else to be shown.
MariaDB [test]> select version();
+-----------------------------------------+
| version() |
+-----------------------------------------+
| 10.10.2-MariaDB-1:10.10.2+maria~ubu2204 |
+-----------------------------------------+
1 row in set (0.001 sec)
MariaDB [test]> select date(d) from (select '' d union all select 20220229) d;
+---------+
| date(d) |
+---------+
| NULL |
| NULL |
+---------+
2 rows in set, 2 warnings (0.002 sec)
MariaDB [test]> show warnings;
+---------+------+--------------------------------------+
| Level | Code | Message |
+---------+------+--------------------------------------+
| Warning | 1292 | Incorrect datetime value: '' |
| Warning | 1292 | Incorrect datetime value: '20220229' |
+---------+------+--------------------------------------+
2 rows in set (0.000 sec)
MariaDB [test]> set session max_error_count=1;
Query OK, 0 rows affected (0.001 sec)
MariaDB [test]> select date(d) from (select '' d union all select 20220229) d;
+---------+
| date(d) |
+---------+
| NULL |
| NULL |
+---------+
2 rows in set, 2 warnings (0.001 sec)
MariaDB [test]> show warnings;
+---------+------+------------------------------+
| Level | Code | Message |
+---------+------+------------------------------+
| Warning | 1292 | Incorrect datetime value: '' |
+---------+------+------------------------------+
1 row in set (0.000 sec)

MariaDB returns empty set when filtering by a datetime field on a 'view' table, while MySQL (and base view sql ) works fine [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
first question here so sorry if not good explained.
I have several views on my db for easier selects with multiple table joins, those views have a date_registered (datetime) field, but when I use WHERE on those fields it always returns empty set.
I'm using MariaDB 10.1.48 (latest one on ubuntu 18.04 repos), same tests on MySQL 5.7 works fine.
Easier to see with examples:
sample data
select id, date_registered from v_tracking limit 5;
+-------+---------------------+
| id | date_registered |
+-------+---------------------+
| 13258 | 2021-10-20 12:56:28 |
| 13257 | 2021-10-20 12:51:37 |
| 13256 | 2021-10-20 12:51:37 |
| 13255 | 2021-10-20 12:46:13 |
| 13254 | 2021-10-20 12:42:13 |
+-------+---------------------+
simple select with where date_registered > 'xxxx-xx-xx'
select id, date_registered from v_tracking where date_registered > '2021-10-01' limit 5;
Empty set (0.01 sec)
filter with other fields works
select id, date_registered from v_tracking where id_company = 6 limit 5;
+-------+---------------------+
| id | date_registered |
+-------+---------------------+
| 13227 | 2021-10-20 06:52:13 |
| 13226 | 2021-10-20 06:43:22 |
| 13225 | 2021-10-20 06:43:04 |
| 13224 | 2021-10-20 06:37:12 |
| 13223 | 2021-10-20 06:23:23 |
+-------+---------------------+
5 rows in set (0.01 sec)
And now the craziest part
adding an 'order by' works
select id, date_registered from v_tracking where date_registered > '2021-10-01' order by id desc limit 5;
+-------+---------------------+
| id | date_registered |
+-------+---------------------+
| 13258 | 2021-10-20 12:56:28 |
| 13257 | 2021-10-20 12:51:37 |
| 13256 | 2021-10-20 12:51:37 |
| 13255 | 2021-10-20 12:46:13 |
| 13254 | 2021-10-20 12:42:13 |
+-------+---------------------+
5 rows in set (0.01 sec)
and forcing conversion to date/timestamp too, but that would probably skip indexes
select id, date_registered from v_tracking where timestamp(date_registered) > '2021-10-01' limit 5;
+-------+---------------------+
| id | date_registered |
+-------+---------------------+
| 13258 | 2021-10-20 12:56:28 |
| 13257 | 2021-10-20 12:51:37 |
| 13256 | 2021-10-20 12:51:37 |
| 13255 | 2021-10-20 12:46:13 |
| 13254 | 2021-10-20 12:42:13 |
+-------+---------------------+
but not other way
select id, date_registered from v_tracking where date_registered > timestamp('2021-10-01') limit 5;
Empty set (0.01 sec)
Tried using 'xxxx-xx-xx xx:xx:xx' too, but same results, also thought that maybe there's a problem in using the date as a string (though I've had no problems with that before), but using where date_registered < now() also returns empty set.
If I use 'show create view v_tracking' to copy the whole SQL (with all joins and so) and add a where date_registered > 'xxxx-xx-xx' to run it (instead of using the view) it works too
WTF am i missing here??
and yes, date_registered is a datetime field
desc v_tracking;
+------------------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------------------+--------------+------+-----+---------+-------+
| id | int(11) | NO | | 0 | |
.....
| date_registered | datetime | YES | | NULL | |
+------------------------+--------------+------+-----+---------+-------+
After some more digging I ended up upgrading MariaDB version to a recent one, and the problem was solved.

Markdown table in Jupyter notebook not working

I know how to set to up a table in a jupyter notebook`. I even looked up internet and imitated it. However it is not working? Anyone can tell me what is wrong with my notebook, is there anything I should change while constructing markdown table
Tables | Are | Cool |
| ------------- |:-------------:| -----:|
| col 3 is | right-aligned | $1600 |
| col 2 is | centered | $12 |
| zebra stripes | are neat | $1 |
Two things:
the example code is missing the first | character
dollar signs need to be escaped with a backslash (\) since MathJax is enabled
Try this:
|Tables | Are | Cool |
| ------------- |:-------------:| ------:|
| col 3 is | right-aligned | \$1600 |
| col 2 is | centered | \$12 |
| zebra stripes | are neat | \$1 |

How to make a query for getting the specific rows with the latest time column value

Below is my sample data, I would like to get the host:value pair with the latest time.
+------+-------+-------+
| HOST | VALUE | TIME |
+------+-------+-------+
| A | 100 | 13:40 |
| A | 150 | 13:00 |
| A | 222 | 13:23 |
| B | 210 | 13:55 |
| B | 300 | 13:44 |
+------+-------+-------+
Wanted to get only rows with the latest time value for the each host column value.
The result should be like:
A 150 13:40
B 210 13:55
I think there are several analytical function to achieve this requirement in Oracle but I'm not sure what can I do in SQLite.
Can you let me know how I can make a query?
Here is an ANSI-compliant way of performing your query which should run on all versions of SQLite. For a potentially shorter solution see the answer by #CL.
SELECT t1.HOST || '-' || t1.VALUE || '-' || t1.TIME AS HOSTVALUETIME
FROM table t1 INNER JOIN
(
SELECT HOST, MAX(TIME) AS MAXTIME
FROM table
GROUP BY HOST
) t2
ON t1.HOST = t2.HOST AND t1.TIME = t2.MAXTIME
ORDER BY t1.HOST DESC
Output:
+---------------+
| HOSTVALUETIME |
+---------------+
| A-100-13:50 |
| B-210-13:55 |
+---------------+
In SQLite 3.7.11 or later, MAX() selects from which row in a group the other column values come:
SELECT Host,
Value,
MAX(Time)
FROM TheNameOfThisTableIsSoSecretThatICantTellYou
GROUP BY Host;

Nicer visual formatting for sqlite3 tables?

How do I format table output in sqlite? For example, if the default behavior for showing entries is hard to read when one of the columns has variable length text. If I do select a,b,c from table I get
a1|b1|c1
a2|b2|c2
while I want some nicer and easily readible formatting like..
++++++++++++++++
| a1 | b1 | c1 |
| a2 | b2 | c2 |
++++++++++++++++
you can use the dot.commands ".mode columns / .width"
sqlite3 test.db <<SQL
.mode columns
.width 3 10 10 4
select 'ABCDEFGHIJKLM','ABCDEFGHIJKLM','ABC','ABCDEFGHIJKLM';
SQL
will receive
ABC ABCDEFGHIJ ABC ABCD
but with no other formatings like borders etc, so very rudimentary
ant
For sqlite3 you just need a command
.mode table
and your tables should look like this:
+----------+-----------+
| per_id | per2_id |
+----------+-----------+
| 13449958 | 5135490 |
| 21085892 | 5135490 |
| 3291872 | 5135490 |
+----------+-----------+
In the case you want this to be permanent change, you need to write the same command .mode table in the file called .sqliterc (if it doesn't exist just make one) in your home directory.
And that should do it.

Resources