Jump to content
Sign in to follow this  
A_T_C

Comparar Tabelas (Postgres)

Recommended Posts

A_T_C

Boa tarde!

Preciso de comparar valores q se encontram em tabelas distintas...

Por exemplo,

Tabela1 :

id | Data

1  |01092009

2  | 23072005

Tabela2

id  | Dia | Mes | Ano

1  | 01  |  09 |  2009

4  |12    | 02  |  2020

e neste caso queria comparar as datas e verificar quais sao iguais... Qual a melhor forma de o fazer?  😳

Share this post


Link to post
Share on other sites
bruno1234

Tens que converter os dados da segunda tabela numa string.

Tens aqui um exemplo de como obter o resultado que queres em SQL Server:

declare @T1 table(
id int,
data varchar(20)
)

declare @T2 table(
id int,
dia tinyint,
mes tinyint,
ano smallint
)

insert into @T1 values (1, '01092009')
insert into @T1 values (2, '23072005')

insert into @T2 values (1, 1,9,2009)
insert into @T2 values (2, 12,2,2020)

select b.id, b.dia, b.mes, b.ano
from @T1 a inner join @T2 b
on a.data = REPLICATE('0', 2 - LEN(b.dia)) + cast(b.dia as varchar(2)) +
		REPLICATE('0', 2 - LEN(b.mes)) + cast(b.mes as varchar(2)) +
		REPLICATE('0', 4 - LEN(b.ano)) + cast(b.ano as varchar(4))


Matraquilhos para Android.

Gratuito na Play Store.

https://play.google.com/store/apps/details?id=pt.bca.matraquilhos

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.