Joe
Joe

Reputation: 539

Combining date , month , year columns in SQL Server

dd-mm-yyyy format

DD(10)-MM(05)-YYYY(2013)

I have a table with DATE, MONTH, YEAR in separate columns. How I can combine them into a single column Created date?

Output must be: 10-05-2013 (DD-MM-YYYY format)

Upvotes: 0

Views: 9739

Answers (4)

Hong Van Vit
Hong Van Vit

Reputation: 2986

You can do it easily:

SELECT to_char(TO_DATE('10 01 2014', 'DD MM YYYY'),'dd-mm-yyyy') AS MYDATE FROM DUAL

Upvotes: 1

Juan Carlos Oropeza
Juan Carlos Oropeza

Reputation: 48207

I guess you want have a date field instead of a string. So use TO_DATE() function. You can format date anyway you want later.

TO_DATE(YEAR + '/' + MONTH + '/' + DATE, 'yyyy/mm/dd')

Upvotes: 1

justiceorjustus
justiceorjustus

Reputation: 1965

SELECT RIGHT('00' + DAY, 2) + '-' + RIGHT('00' + MONTH, 2) + '-' + YEAR AS Date 
FROM YourTable

Upvotes: 2

milton
milton

Reputation: 57

To concatenate columns you just need to use "+" in your select statement

SELECT DATE + '-'+ MONTH + '-'+ YEAR FROM table

Upvotes: 0

Related Questions