How to change the Data Type for a Date Field

Carlton Patterson 761 Reputation points
2023-06-20T18:05:10.1066667+00:00

Can someone let me know what Data Type will remove the slashes from a date field?

For example, the as_of_date field is formatted as 31/05/2023. Is there format that will remove the slashes such that the date looks as follows 31052023 or 20230531

I have tried VARCHAR, but that just formats the date as 2023-05-31.

Any thoughts?

SQL Server | Other
{count} votes

5 answers

Sort by: Most helpful
  1. Ryan Jusay 165 Reputation points
    2023-06-21T00:14:47.4166667+00:00

    You can change the format of the field as you query your records.

    example:

    SELECT FORMAT(as_of_date, 'yyyyMMdd') AS 'FormattedAsOfDate', * FROM FundManagers

    or

    SELECT FORMAT(as_of_date, 'ddMMyyyy') AS 'FormattedAsOfDate', * FROM FundManagers

    you can change the way the field is presented by changing the 2nd parameter

    Year -> yyyy = 2023

    Month -> MM = 05

    Day - > dd = 31

    other parameters you can try and play around with:

    • dd - this is day of month from 01-31
    • dddd - this is the day spelled out
    • MM - this is the month number from 01-12
    • MMM - month name abbreviated
    • MMMM - this is the month spelled out
    • yy - this is the year with two digits
    • yyyy - this is the year with four digits
    • hh - this is the hour from 01-12
    • HH - this is the hour from 00-23
    • mm - this is the minute from 00-59
    • ss - this is the second from 00-59
    • tt - this shows either AM or PM
    • d - this is day of month from 1-31 (if this is used on its own it will display the entire date)
    • us - this shows the date using the US culture which is MM/DD/YYYY
    1 person found this answer helpful.
    0 comments No comments

  2. Naomi Nosonovsky 8,431 Reputation points
    2023-06-20T18:07:37.83+00:00

    Try using format function, e.g. format(dateColumn, 'yyyyMMdd') assuming dateColumn is the actual date in SQL Server.


  3. Viorel 122.6K Reputation points
    2023-06-20T19:24:04.76+00:00

    Maybe it has sense to keep the reasonable data allocation for as_of_date column. It is possible to define a new computed column that does not need additional space:

    alter table [kevin-sqldb].outbound.FundManagers add as_of_date_2 as convert(varchar(8), as_of_date, 112)
    

    Use as_of_date for fast calculations, and as_of_date_2 for formatted output.


  4. Olaf Helper 47,441 Reputation points
    2023-06-21T05:52:15.7233333+00:00

    For example, the as_of_date field is formatted as 31/05/2023

    A column of datatype "date" don't store any format information, it's just a date value.

    It's the frontend as presention layer that format the date value into a readable format.

    0 comments No comments

  5. Anonymous
    2023-06-21T07:26:23.66+00:00

    Hi @Carlton Patterson

    SQL requires the use of a select statement to display the contents of the query.

    If you need to change the date format, just do it in the select statement as Naomi does.

    It is not practical to start with the data type of the column.

    Best regards,

    Percy Tang

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.