How to make column name of query result in UPPERCASE (Postgres)
SELECT USER_NAME, USER_AGE from table;
Result
user_name
user_age
First
123
Second
234
Expectation : Result column name to be in uppercase
(USER_NAME USER_AGE instead of user_name and user_age).
You can create an alias:
SELECT user_name as "USER_NAME",
user_age as "USER_AGE"
from table;
For details on how identifiers are treated see the manual
Related
Suppose my table user_info has 2 columns, one is #username and another one is #info.
Now I already made a query "INSERT INTO user_info(username) value('')
How can I make another query to put data on column #info for the same username?? Because after the first query I'll have null for the column #info I believe.
Just to clarify, I don't get the #info when I have the username. Each user will get their info later. So I can't put then on the same query.
In that case, you need to update the row, simply using update on where the username exist
From documentation:
UPDATE table_name
SET column1 = value1,
column2 = value2,
...
WHERE condition;
Your case:
UPDATE user_info
SET info='new_information'
WHERE username='existing_username'
I'm using JSON to pass in a list of ID's, however the following query always returns all records.
select first_name
from app.users
where id in (
select id::varchar::bigint
from json_array_elements('[9497902]'::json) id
);
Swapping out the JSON with the ID's manually brings back the expected amount of records:
select first_name
from app.users
where id in (
9497902
);
Is there something I am missing to be able to work with JSON ID's?
demo: db<>fiddle
select name
from users
where id in (
select id.value::varchar::bigint
from json_array_elements('[1,3]'::json) id
);
The name id used within the subquery has been transferred from the outer query into the inner query. This can be checked just by executing this query:
select name
from users
where id in (
select id
);
The alias of your JSON function is id but this is the name of the result set not of the column. The default column name of this function is value. So correctly you have to use SELECT id.value... or simply SELECT value to referrence the correct value.
I am creating a viewer for PostgreSQL. My SQL needs to sort on the type that is normal for that column. Take for example:
Table:
CREATE TABLE contacts (id serial primary key, name varchar)
SQL:
SELECT id::text FROM contacts ORDER BY id;
Gives:
1
10
100
2
Ok, so I change the SQL to:
SELECT id::text FROM contacts ORDER BY id::regtype;
Which reults in:
1
2
10
100
Nice! But now I try:
SELECT name::text FROM contacts ORDER BY name::regtype;
Which results in:
invalid type name "my first string"
Google is no help. Any ideas? Thanks
Repeat: the error is not my problem. My problem is that I need to convert each column to text, but order by the normal type for that column.
regtype is a object identifier type and there is no reason to use it when you are not referring to system objects (types in this case).
You should cast the column to integer in the first query:
SELECT id::text
FROM contacts
ORDER BY id::integer;
You can use qualified column names in the order by clause. This will work with any sortable type of column.
SELECT id::text
FROM contacts
ORDER BY contacts.id;
So, I found two ways to accomplish this. The first is the solution #klin provided by querying the table and then constructing my own query based on the data. An untested psycopg2 example:
c = conn.cursor()
c.execute("SELECT * FROM contacts LIMIT 1")
select_sql = "SELECT "
for row in c.description:
if row.name == "my_sort_column":
if row.type_code == 23:
sort_by_sql = row.name + "::integer "
else:
sort_by_sql = row.name + "::text "
c.execute("SELECT * FROM contacts " + sort_by_sql)
A more elegant way would be like this:
SELECT id::text AS _id, name::text AS _name AS n FROM contacts ORDER BY id
This uses aliases so that ORDER BY still picks up the original data. The last option is more readable if nothing else.
I have a table where a user inputs name, dob, etc. and I have a User_Name column that I want automatically populated from other columns.
For example input is: Name - John Doe, DOB - 01/01/1900
I want the User_Name column to be automatically populated with johndoe01011900 (I already have the algorithm to concatenate the column parts to achieve the desired result)
I just need to know how (SQL, Trigger) to have the User_Name column filled once the user completes imputing ALL target columns. What if the user skips around and does not input the data in order? Of course the columns that are needed are (not null).
This should do it:
you can use a calculated field with the following calculation:
LOWER(REPLACE(Name, ' ', ''))+CONVERT( VARCHAR(10), DateOfBirth, 112))
In the below sample I have used a temp table but this is the same for regular tables as well.
SAMPLE:
CREATE TABLE #temp(Name VARCHAR(100)
, DateOfBirth DATE
, CalcField AS LOWER(REPLACE(Name, ' ', ''))+CONVERT( VARCHAR(10), DateOfBirth, 112));
INSERT INTO #temp(Name
, DateOfBirth)
VALUES
('John Doe'
, '01/01/1900');
SELECT *
FROM #temp;
RESULT:
I have a table with a Name column and a Log column.
Name Log
Michelle Bad 222 news travels id 54585 fast.
Lucy Barking 333 dogs id 545584 seldom bite.
Green Beauty is 444 in the id 85955 eyes of the beholder.
Gail Beggars 123 can't be ID 4658 choosers.
I want to extract only the ID digits from log column. Note that the word ID could be capitalized or not. Hence, the output should be like this:
Name ID
Michelle 54585
Lucy 545584
Green 85955
Gail 4658
I tried to use the following query:
select name
, substring(log from E'^(.*?)[id< ]') as id
from mytable;
However, I cannot have the output I need.
Something along these lines should work. Since you didn't provide CREATE TABLE and INSERT statements, I just used one row in a common table expression.
with data as (
select 'Michelle' as name, 'Bad 333 id 54342 wibble' as log
)
select name, substring(substring(log::text, '((id|ID) [0-9]+)'), '[0-9]+')
from data
where log::text ~* 'id [0-9]+';
The nested substring() calls first return the id number along with the string 'id', then return just the id number.