-2

In SQL Server, a column with Date data type stores the date as: yyyy-MM-dd. When selecting data from a column with Date data type using VB.NET it return that date as: MM/dd/yyyy.

How to tell SQL server to return the date as storder: yyyy-MM-dd. without converting the date to char in the select statement?

Imad Abu Hayyah
  • 434
  • 4
  • 13
  • 3
    A `DATE` in SQL Server does **NOT** have any format - it's just a binary value. The formatting only happens when it's being output - so you **cannot** "store" a date in a "given format" - you can just make sure to **return it** in the desired format (by converting to a string, for instance) – marc_s Apr 03 '20 at 20:12
  • 1
    Yes. Read the value from SQL Server as a DateTime, then use the ToString() method to format it how you like: .ToString("yyyy-MM-dd") – A. Niese Apr 03 '20 at 20:14
  • 1
    Since SQL Server NOT have any format, how it distinguish between dd/MM/yyyy and MM/dd/yyyy? – Imad Abu Hayyah Apr 03 '20 at 20:17
  • 3
    Please see https://stackoverflow.com/questions/1143259/what-is-the-internal-representation-of-datetime-in-sql-server All localized formatting is in your own application code, not the server – donPablo Apr 03 '20 at 20:26
  • If the date returned by a selected statement is 09/03/2020, then how to know that 09 is day or month? – Imad Abu Hayyah Apr 03 '20 at 21:32
  • You specify how to format the date in your code, like using `ToString("MM/dd/yyyy"). – Dan Guzman Apr 03 '20 at 21:44

1 Answers1

0
select mmddyyyy = convert(varchar(10), getdate(), 101), yyyymmdd = convert(varchar(10), getdate(), 121)

Use the convert function

Jon Hanlon
  • 162
  • 1
  • 8