Claud30
Claud30

Reputation: 13

Cast as Date Only

My goal is to display this

02/28/19 63

from this. (date_birth is datetime in DB)

1963-02-28 00:00:00.000

I used this SQL query code

FORMAT(date_birth,'MM/dd/19 yy')

but my teacher wanted it in a cast so I did this

CAST(date_birth as date) as date_birth

How can i format the above date through cast?

Upvotes: 0

Views: 5321

Answers (1)

blurfus
blurfus

Reputation: 14031

Since date_birth is already a DateTime column in the DB, you can chain the function calls and call them together.

Something like this: FORMAT(CAST(date_birth as DATE),'MM/dd/19 yy')

Please note that the order matters here - also, if date_birth is a VARCHAR in the DB, then this might not work as the contents of the string could be different from something a Date could accept.

For info: https://www.sqlservertutorial.net/sql-server-system-functions/convert-datetime-to-date/

Upvotes: 1

Related Questions