Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

233
Visualizações
Converting date and time TEXT fields to one INT unix timestamp field in SQL

I currently have a mySQL database with two TEXT fields: Date and Time. These are in the format 'dd/mm/yyyy' and '0:00' respectively, for example '11/08/2020' and 19:12. I want to create a third field called Timestamp (of type INT) and convert the two original fields into this timestamp field and remove the date/time text fields.

I have done a bit of research in regards to using UNIX_TIMESTAMP() and STR_TO_DATE() but I can't seem to get it to work, the format seems to be wrong for it.

How can I achieve this in SQL and convert two string fields which represent date and time into a third field to replace them both which just stores the unix timestamp?

This is my best attempt so far..

SELECT UNIX_TIMESTAMP(STR_TO_DATE(CONCAT(`InfractionDate`, " ", `InfractionTime`), '%d %M %Y %h:%i%p')) FROM `playerinfractions`

The table is called playerinfractions and the date/time are stored in the TEXT fields InfractionDateand InfractionTime.

Many thanks in advance!

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

The format pattern that you use in the function STR_TO_DATE()is wrong.
Try this:

SELECT 
  UNIX_TIMESTAMP(
    STR_TO_DATE(
      CONCAT(`InfractionDate`, ' ', `InfractionTime`), 
      '%d/%m/%Y %H:%i')
  )     
FROM `playerinfractions`
over 4 years ago · Santiago Trujillo Relatório

0

You need to tell STR_TO_DATE the correct format of your date and time, which you suggested was '%d/%m/%Y %H:%i'

SELECT UNIX_TIMESTAMP( 
            STR_TO_DATE( 
                CONCAT('10/08/2020', ' ', '12:20'), '%d/%m/%Y %H:%i' ) 
            )
        )
FROM `playerinfractions`

So using your columns

SELECT UNIX_TIMESTAMP( 
            STR_TO_DATE( 
                CONCAT(`InfractionDate`, ' ', `InfractionTime`), '%d/%m/%Y %H:%i' ) 
            )
        )
FROM `playerinfractions`
over 4 years ago · Santiago Trujillo Relatório

0

I tried passing a string to the same function in below format and it worked. Also you can share your format to check it further.

select UNIX_TIMESTAMP(STR_TO_DATE(CONCAT('8/12/2020',' ', '12:01:49' ),'%m/%d/%Y %h:%i:%s'))
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda