0

I have a database with 10+ columns. Three of the columns represent the day, the month and the year. I now need a date as one Datatype.

I already tried some diffrent solutions explained on the internet, but they didn't work for mysql. And I can't find the mistake I made.

SELECT *,
CAST(
  CAST(CREATE_YEAR AS VARCHAR(4)) +
  RIGHT('0' + CAST(CREATE_MONTH AS VARCHAR(2)), 2) +
  RIGHT('0' + CAST(CREATE_DAY AS VARCHAR(2)), 2) 
AS DATETIME)
from incident_view

The name of the three columns are : CREATE_DAY, CREATE_MONTH and CREATE_YEAR.

Example how I want it to work out: The Column DAY has the number 27 in it, the month the number 5 and the year the number 2015. I want to the be displayed like : 27.5.2015 .

SELECT *,
STR_TO_DATE( 
     CONCAT( CREATE_DAY, '/', CREATE_MONTH, '/', CREATE_YEAR ),
     '%d/%m/%Y' 
   ) as rcvd_date
FROM incident_view
where CUSTOMER_COMPANY_NAME = "Company" AND (create_month = MONTH(NOW() - INTERVAL 1 MONTH) AND (create_year = YEAR(NOW() - INTERVAL 1 MONTH)))

This sql achieved me to have a logical Column in my database which is called rcvd_data. But it shows me the day-month-year-hour-minute-seconds, but I only need the day-month and the year.

Example of the data field : 01/02/2016 00:00:00

I would appreciate any help. Thank you.

2
  • 1
    All your syntax looks like SQL Server. Yet your question is tagged "mysql" and "toad", a tool associated with Oracle. What database are you really using? Tag your question appropriately. Mar 1, 2016 at 13:15
  • I am using toad for MySQL.
    – user5980704
    Mar 1, 2016 at 13:30

1 Answer 1

1

In MySQL, you can use:

select str_to_date(concat_ws('-', CREATE_YEAR, CREATE_MONTH, CREATE_DAY),
                   '%Y-%m-%d'
                  ) as CreateDate
2
  • Thank you for the reply. I followed your Instructions, but i receive the same result... Still see the hour minutes and seconds in the Column. Moreover if I try to remove for example the day like: SELECT *, str_to_date(concat_ws('-', CREATE_YEAR, CREATE_MONTH), '%Y-%m' ) as CreateDate FROM incident_view where CUSTOMER_COMPANY_NAME = "CrossCountry Trains" AND (create_month = MONTH(NOW() - INTERVAL 1 MONTH) AND (create_year = YEAR(NOW() - INTERVAL 1 MONTH))) it shows me null as data field
    – user5980704
    Mar 1, 2016 at 13:28
  • Looks like the problem is date output format. Should be DATE_FORMAT(the str_to_date result ,'%d/%m/%Y')
    – StanislavL
    Mar 1, 2016 at 14:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.