Change "money" symbol on Beekeeper SQL - sql

I'm student of SQL using Beekeeper Studio/Postgres, and I need to create a column using money.
But when I do SELECT * FROM produto, shows me "R$", my local monetary symbol.
Can I change the R$ to ʛ not only in my Beekeeper, but to everyone?
CREATE TABLE produto(
id SERIAL PRIMARY KEY,
codigo_produto int NOT NULL,
nome_produto VARCHAR(100) NOT NULL,
descricao_produto VARCHAR(100) NOT NULL,
estoque_produto int NOT NULL,
fabricacao_produto date NOT NULL,
valor_produto money NOT NULL
);
INSERT INTO produto (codigo_produto, nome_produto, descricao_produto, estoque_produto, fabricacao_produto, valor_produto) values
(1, 'firebolt', 'vassoura randolph spudmore', 1, '1993-02-08', '2000'),
(2, 'cleansweep eleven', 'vassoura companhia de vassouras cleansweep', 15, '1995-08-01', '500');
SELECT * FROM produto

Related

SQL inner join condition by actual date

I need to join actual document to people. Documents has date issued (passport, for example).
SQL Fiddle: http://sqlfiddle.com/#!9/3a8118/2/0
Table structure:
CREATE TABLE people
(
p_id INT NOT NULL AUTO_INCREMENT,
p_name VARCHAR(50) NOT NULL,
PRIMARY KEY(p_id)
);
INSERT INTO people (p_id, p_name)
VALUES (1, 'Name_1'),
(2, 'Name_2');
CREATE TABLE documents
(
d_id INT NOT NULL AUTO_INCREMENT,
d_people INT(10) NOT NULL,
d_date VARCHAR(10) NOT NULL,
PRIMARY KEY(d_id)
);
INSERT INTO documents (d_id, d_people, d_date)
VALUES (1, 1, '01.01.2022'),
(2, 2, '01.12.2021'),
(3, 1, '05.02.2022'),
(4, 1, '10.02.2022'),
(5, 2, '04.01.2022'),
(6, 1, '20.01.2022');
Query: condition is select actual document when date is 21.01.2022, it must return d_id = 6:
SELECT *
FROM people
INNER JOIN documents ON d_people = p_id
WHERE p_id = 1 AND ??? d_date 21.01.2022 ???
;
I need to do an inner join to return only this row:
use this query:
Fiddle
SELECT * FROM people
INNER JOIN documents ON d_people = p_id
WHERE p_id = 1 and d_date='20.01.2022';

Show the names of clients from Spain that are over the mean age aprox 38.2

RDBMS
enter image description here
I'm trying to gather the information that is required in this question, I have calculated clients age, here are the data for this table
CREATE TABLE `clientes` (
`id_cliente` int(11) NOT NULL,
`nombre` varchar(45) COLLATE utf8_bin NOT NULL,
`apellidos` varchar(45) COLLATE utf8_bin NOT NULL,
`dni_pasaporte` text COLLATE utf8_bin NOT NULL,
`telefono` mediumtext COLLATE utf8_bin DEFAULT NULL,
`sexo` mediumtext COLLATE utf8_bin NOT NULL,
`fecha_nacimiento` date NOT NULL,
`pais_nacimiento` varchar(45) COLLATE utf8_bin DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
--
-- Dumping data for table `clientes`
--
INSERT INTO `clientes` (`id_cliente`, `nombre`, `apellidos`, `dni_pasaporte`, `telefono`, `sexo`, `fecha_nacimiento`, `pais_nacimiento`) VALUES
(1, 'Carlos', 'Montero Suarez', '16066793V', '723510602', 'H', '1980-02-14', 'Spain'),
(2, 'Allison', 'Lukianov', '3258879821', '395404877', 'femenino', '1970-12-06', 'China'),
(3, 'Rey', 'Roja', '8298779244', '352682209', 'H', '1965-05-19', 'Spain'),
(4, 'Elton', 'Saintsbury', '446046705', '969547852', 'H', '1987-08-23', 'Tunisia'),
(5, 'Bert', 'Leckey', '732411565', '625573861', 'H', '1990-09-27', 'China'),
(6, 'Carmine', 'Eicke', '300115949', '261559877', 'femenino', '1985-08-03', 'China'),
(7, 'Maria ', 'Sanchez Cardoso', '14180343D', '441952642', 'femenino', '1979-05-21', 'Spain'),
(9, 'John ', 'Lennon', '236147952', '630745219', 'H', '1970-11-14', 'Tunisia'),
(10, 'Karla', 'Watson', '523617420', '923541078', 'femenino', '1980-05-12', 'Tunisia');
ALTER TABLE `clientes`
ADD PRIMARY KEY (`id_cliente`),
ADD UNIQUE KEY `Id_cliente_unique` (`id_cliente`) USING BTREE;
ALTER TABLE `clientes`
MODIFY `id_cliente` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=11;
What I need to calculate the average age between all 10 users on this table which is 38.2.
And I need to have all spanish users that are above this age, can you let me know how to get this information?
I have tried the following entry and I'm just getting the age of all clients
SELECT *,
YEAR(CURDATE()) - YEAR(fecha_nacimiento) - (RIGHT(CURDATE(), 5) < RIGHT(fecha_nacimiento, 5)) Age
from clientes
I want to know is how to calculate the average of all 10 clients based on current day and date of birth.
SELECT *,
YEAR(CURDATE()) - YEAR(fecha_nacimiento) - (RIGHT(CURDATE(), 5) < RIGHT(fecha_nacimiento, 5)) Age
(select avg(all(year(now()) - year(fecha_nacimiento)))) as Average
from clientes
but this only shows one client, how can I get the other two?

Multi-Query Queries [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
There are several tables in the database: films, persons and creators. Creators has two foreign keys to films and persons, as well as the fields "Character of participation" (director, actor, composer, etc.) and "Role" (see CREATE TABLE statements below). I would like to display a list of films in which the director is at the same time performing one of the main roles, indicating his last name and the role he played.
create table films (
film_id tinyint identity(1, 1),
film_name varchar(20) not null,
film_studio varchar(25) not null,
film_year int not null,
film_country varchar(20) null,
film_length tinyint not null,
film_genre varchar(15) not null,
constraint PK_films primary key(film_id),
);
create table persons (
person_id tinyint identity(1, 1),
person_name varchar(50) not null,
person_bday date not null,
person_dday date,
constraint PK_persons primary key(person_id),
);
create table creators (
creator_id tinyint identity(1, 1),
creator_film tinyint not null,
creator_person tinyint not null,
creator_who varchar(20) not null,
creator_role varchar(20),
constraint PK_creators primary key(creator_id),
constraint FK_CF foreign key(creator_film) references films(film_id),
constraint FK_CP foreign key(creator_person) references persons(person_id),
constraint CH_Who check(creator_who='director' or creator_who='actor' or creator_who='composer'),
);
insert into films(film_name, film_studio, film_year, film_country, film_length, film_genre) values ('Film1', 'Studio1', 2018, 'USA', 100, 'Genre1')
insert into films(film_name, film_studio, film_year, film_country, film_length, film_genre) values ('Film2', 'Studio2', 2018, 'USA', 120, 'Genre2')
insert into films(film_name, film_studio, film_year, film_country, film_length, film_genre) values ('Film3', 'Studio3', 2000, 'England', 90, 'Genre3')
insert into persons(person_name, person_bday, person_dday) values ('John Smitt', '1988-12-12', null)
insert into persons(person_name, person_bday, person_dday) values ('Mel Gibson
', '1988-12-12', null)
insert into persons(person_name, person_bday, person_dday) values ('Miley Cyrus', '2001-12-12', null)
insert into persons(person_name, person_bday, person_dday) values ('Deadpool', '1999-12-12', null)
insert into creators(creator_film, creator_person, creator_who) values (1, 1, 'Director')
insert into creators(creator_film, creator_person, creator_who, creator_role) values (1, 1, 'Actor', 'Main')
insert into creators(creator_film, creator_person, creator_who, creator_role) values (2, 3, 'Actor', 'Secondary')
insert into creators(creator_film, creator_person, creator_who, creator_role) values (3, 4, 'Actor', 'Secondary')
Desired result is: Film1 John Smith Main
http://sqlfiddle.com/#!18/9211f/6
SELECT
films.film_name,
persons.person_name,
actor.creator_role
FROM creators director
INNER JOIN creators actor
ON director.creator_person = actor.creator_person
AND actor.creator_who = 'Actor'
LEFT JOIN films
ON director.creator_film = films.film_id
LEFT JOIN persons
ON director.creator_person = persons.person_id
WHERE director.creator_who = 'Director'

How to automatically generate unique id in SQL like UID12345678?

I want to automatically generate unique id with per-defined code attach to it.
ex:
UID12345678
CUSID5000
I tried uniqueidentifier data type but it generate a id which is not suitable for a user id.
Any one have suggestions?
The only viable solution in my opinion is to use
an ID INT IDENTITY(1,1) column to get SQL Server to handle the automatic increment of your numeric value
a computed, persisted column to convert that numeric value to the value you need
So try this:
CREATE TABLE dbo.tblUsers
(ID INT IDENTITY(1,1) NOT NULL PRIMARY KEY CLUSTERED,
UserID AS 'UID' + RIGHT('00000000' + CAST(ID AS VARCHAR(8)), 8) PERSISTED,
.... your other columns here....
)
Now, every time you insert a row into tblUsers without specifying values for ID or UserID:
INSERT INTO dbo.tblUsersCol1, Col2, ..., ColN)
VALUES (Val1, Val2, ....., ValN)
then SQL Server will automatically and safely increase your ID value, and UserID will contain values like UID00000001, UID00000002,...... and so on - automatically, safely, reliably, no duplicates.
Update: the column UserID is computed - but it still OF COURSE has a data type, as a quick peek into the Object Explorer reveals:
CREATE TABLE dbo.tblUsers
(
ID INT IDENTITY(1,1) NOT NULL PRIMARY KEY CLUSTERED,
UserID AS 'UID' + RIGHT('00000000' + CAST(ID AS VARCHAR(8)), 8) PERSISTED,
[Name] VARCHAR(50) NOT NULL,
)
marc_s's Answer Snap
Reference:https://learn.microsoft.com/en-us/sql/t-sql/functions/newid-transact-sql?view=sql-server-2017
-- Creating a table using NEWID for uniqueidentifier data type.
CREATE TABLE cust
(
CustomerID uniqueidentifier NOT NULL
DEFAULT newid(),
Company varchar(30) NOT NULL,
ContactName varchar(60) NOT NULL,
Address varchar(30) NOT NULL,
City varchar(30) NOT NULL,
StateProvince varchar(10) NULL,
PostalCode varchar(10) NOT NULL,
CountryRegion varchar(20) NOT NULL,
Telephone varchar(15) NOT NULL,
Fax varchar(15) NULL
);
GO
-- Inserting 5 rows into cust table.
INSERT cust
(CustomerID, Company, ContactName, Address, City, StateProvince,
PostalCode, CountryRegion, Telephone, Fax)
VALUES
(NEWID(), 'Wartian Herkku', 'Pirkko Koskitalo', 'Torikatu 38', 'Oulu', NULL,
'90110', 'Finland', '981-443655', '981-443655')
,(NEWID(), 'Wellington Importadora', 'Paula Parente', 'Rua do Mercado, 12', 'Resende', 'SP',
'08737-363', 'Brasil', '(14) 555-8122', '')
,(NEWID(), 'Cactus Comidas para Ilevar', 'Patricio Simpson', 'Cerrito 333', 'Buenos Aires', NULL,
'1010', 'Argentina', '(1) 135-5555', '(1) 135-4892')
,(NEWID(), 'Ernst Handel', 'Roland Mendel', 'Kirchgasse 6', 'Graz', NULL,
'8010', 'Austria', '7675-3425', '7675-3426')
,(NEWID(), 'Maison Dewey', 'Catherine Dewey', 'Rue Joseph-Bens 532', 'Bruxelles', NULL,
'B-1180', 'Belgium', '(02) 201 24 67', '(02) 201 24 68');
GO
If you want to add the id manually you can use,
PadLeft() or String.Format() method.
string id;
char x='0';
id=id.PadLeft(6, x);
//Six character string id with left 0s e.g 000012
int id;
id=String.Format("{0:000000}",id);
//Integer length of 6 with the id. e.g 000012
Then you can append this with UID.
The 'newid()' method unique id generate for per record.
AddColumn("dbo.Foo", "Key", c => c.String(nullable: false, maxLength: 250, defaultValueSql: "newid()"));
Table Creating
create table emp(eno int identity(100001,1),ename varchar(50))
Values inserting
insert into emp(ename)values('narendra'),('ajay'),('anil'),('raju')
Select Table
select * from emp
Output
eno ename
100001 narendra
100002 rama
100003 ajay
100004 anil
100005 raju

Does number span exists in database

I hope you can understand my question, if not, please let me know...
In my form I have eight textboxes, like so (user wrote '25' to first textbox and '35' to second one):
Code1From: _25____ Code1To:_35____
Code2From: _______ Code2To:_______
Code3From: _______ Code3To:_______
Code4From: _______ Code4To:_______
My table looks like this:
create table MyTable (
Id int identity(1,1),
Code1From bigint, Code1To bigint,
Code2From bigint, Code2To bigint,
Code3From bigint, Code3To bigint,
Code4From bigint, Code4To bigint
)
Now I'd like to prevent inserting data, that is allready inserted. For example:
Data in MyTable:
Id, Code1From, Code1To, Code2From, Code2To, Code3From, Code3To, Code4From, Code4To
1, 1, 10, null, null, null, null, null, null
2, 11, 20, null, null, null, null, null, null
3, 21, 30, null, null, null, null, null, null
4, 31, 40, null, null, null, null, null, null
5, 41, 50, null, null, null, null, null, null
If user wants to insert for Code1 (or Code2, Code3 or Code4) from 25 to 35, I should raise an error
(because span from 25 to 35 is allready in the database - id 3 and 4).
However, user can insert span from 51 to 55 for example.
How can I determine, if a span is allready in my database? Now I could do something like this:
select *
from MyTable
where
#code1From between Code1From and Code1To
or #code1From between Code2From and Code2To
or #code1From between Code3From and Code3To
or #code1From between Code4From and Code4To
--
or #code1To between Code1From and Code1To
or #code1To between Code2From and Code2To
or #code1To between Code3From and Code3To
or #code1To between Code4From and Code4To
--
... and another 24 or statements
Is there any easier way to accomplish this?
If i understand you correctly, this might help you
DECLARE #Table TABLE(
FromVal1 FLOAT,
ToVal1 FLOAT,
FromVal2 FLOAT,
ToVal2 FLOAT,
FromVal3 FLOAT,
ToVal3 FLOAT,
FromVal4 FLOAT,
ToVal4 FLOAT
)
INSERT INTO #Table (FromVal1,ToVal1,FromVal2,ToVal2) SELECT 1, 10, 51, 60
INSERT INTO #Table (FromVal2,ToVal2) SELECT 11, 20
INSERT INTO #Table (FromVal3,ToVal3) SELECT 21, 30
INSERT INTO #Table (FromVal4,ToVal4) SELECT 31, 40
INSERT INTO #Table (FromVal1,ToVal1) SELECT 41, 50
DECLARE #FromVal FLOAT,
#ToVal FLOAT
SELECT #FromVal = 25,
#ToVal = 35
SELECT *
FROM #Table
WHERE NOT(FromVal1 > #ToVal OR ToVal1 < #FromVal)
OR NOT(FromVal2 > #ToVal OR ToVal2 < #FromVal)
OR NOT(FromVal3 > #ToVal OR ToVal3 < #FromVal)
OR NOT(FromVal4 > #ToVal OR ToVal4 < #FromVal)