Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

208
Vistas
ID with Year formatting in PostgreSQL

I'm working on a contract database in PostgreSQL. One column in my database is the contract number/id and should be formatted like ID/Year. I have a column with start of contract date from which I should take the Year from.

Right now I have the ID as a Serial that auto-increments but without the Year part.

I have
ID       |   DATE_FROM
----------------------- 
1        |   2016-10-01
2        |   2016-11-01
3        |   2017-01-01


I need
ID       |   DATE_FROM
----------------------- 
1/2016   |   2016-10-01
2/2016   |   2016-11-01
1/2017   |   2017-01-01

I'm new to PostgreSQL and I don't know how to do it but I'm sure there is a way to do it in PostgreSQL only and I will be glad if anyone could help.

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

You should change the field name "FROM" other wise you will confuse the keyword FROM

 SELECT ID::text || '/' || EXTRACT(YEAR FROM "FROM"),
        "FROM"
 FROM yourTable;

EDIT: For restart the counter by year you use ROW_NUMBER()

 SELECT ROW_NUMBER() OVER (PARTITION BY EXTRACT(YEAR FROM "FROM")
                           ORDER BY "ID")::text || 
        '/' || EXTRACT(YEAR FROM "FROM"),
        "FROM"
 FROM yourTable;

I assume the ID are in chronological order, other wise you can change the partition order to ORDER BY "FROM"

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda