FORMAT (date|number, format_string)

This returns a string from a date or number using the format_string syntax. In the case of a date, the
format_string should be structured to indicate the year, month, day, etc. with their initial.

Working with time:

  • To remove the time from a date time use the TRUNC function.
  • To retrieve the time from a date use format(GETDATE(),’HH:mn:ss’)

See also: MONTH, DAYOFWEEK.

Example:

/* might return something like 29-2015-12 */ 
SELECT FORMAT (GETDATE(), ‘dd-yyyy-mm’)

/* returns 5,459,40 */
SELECT FORMAT (5459.4, '##,##0.00')

/* Returns name of month in full e.g. 'October' */
SELECT DISTINCT thedate,FORMAT(thedate,'MMMMM') AS MonthName FROM transact
TheDate Month
10/1/2000 October
10/2/2000 October
10/3/2000 October
10/4/2000 October

/* Returns name of month in abbreviated e.g. Oct */
SELECT DISTINCT thedate,FORMAT(thedate,'MMM') AS MonthName FROM transact
TheDate Month
10/1/2000 Oct
10/2/2000 Oct
10/3/2000 Oct
10/4/2000 Oct
Was this article helpful?
0 out of 0 found this helpful

Comments

0 comments

Please sign in to leave a comment.