Home > Enterprise >  Get the date from a column in database SQL
Get the date from a column in database SQL

Time:11-26

I have a table column called as VoyageDate and it has values like this: Nov 25, 2021 12:00:00.0 AM. I try to view these dates in the frontend in this format YYYY-MM--DD. To do this I tried like this:

SELECT CAST(GETDATE() AS "VoyageDate") AS "voyageDate"

But it does not work correctly. How can I do this.

Thank you.

CodePudding user response:

The select should be written like this

SELECT FORMAT(GETDATE(), "YYYY--MM--DD") as "voyageDate";

You need to use function format(), to see the date in expected format.

You can read more here https://www.w3schools.com/sql/func_sqlserver_format.asp

If you are trying to read the value from column in table XXX (you don't specify it in question so replace the XXX with name of table) then the Select should look like this

SELECT FORMAT(VoyageDate, "YYYY--MM--DD") as "voyageDate" from XXX;
  •  Tags:  
  • sql
  • Related