Many times when working with date and times in select, where – the data doesn’t show up in the result window or is not in the format we are looking for. SQL server provides with some build in functions to format the date and time string for you.

When using date time the most common function used are

  • Getdate() – this provide the server current date/time.
  • Getutcdate() – this provides the universal date/time.

For converting date time into date/time formats we can use the convert function

Syntax : CONVERT(data_type(length), expression, style)

data_type: specify the datatype to convert expression to

expression: the value to convert to another data type

style (optional) : the format used to convert between data types

Example :

Select CONVERT(varchar, getdate(), 23)  — will display current date in YYYY-MM-DD format

Select CONVERT(varchar, getdate(), 1)  — will display current date in MM/DD/YYYY format

Below is the list of style (format) you can use to display date / Time / DateTime.