Set Date Format to DDMMYYY

Bruce Minchinton 61 Reputation points
2020-10-19T21:52:45.457+00:00

Hello,
In the following script, how do I set the @CompletedDate & @ CompletedDate2 so that users enter the date in dd/mm/yyy format

SELECT
CONVERT (Date, [Order].CompletedDate) As DateOnly
,OrderType.Name
,[Order].OrderTotal
,[Order].Discount
FROM
OrderType
INNER JOIN [Order]
ON OrderType.OrderTypeId = [Order].OrderTypeId
WHERE [Order].CompletedDate >= CONVERT(Date, @CompletedDate)
AND [Order].CompletedDate < CONVERT(Date, DATEADD(Day, 1, @CompletedDate2))

Regards
Bruce

SQL Server Reporting Services
SQL Server Reporting Services
A SQL Server technology that supports the creation, management, and delivery of both traditional, paper-oriented reports and interactive, web-based reports.
3,063 questions
Developer technologies | Transact-SQL
0 comments No comments
{count} votes

Accepted answer
  1. Erland Sommarskog 121.8K Reputation points MVP Volunteer Moderator
    2020-10-19T22:00:04.927+00:00

    @CompletedDate and @CompletedDate2 should be of data type date, no other data type. This means that in SQL Server the question about format is irrelevant. Date does not have a format, it is just a value.

    On the other hand, on the client where you actaully pass the data and get it from the users, you need to consider this. Then again, this is often automatic. If you use a date-picker, I guess it outputs some sort of date object, which you pass to your parameter. But even if it is a string, you just take the string and let the regional settings on the machine decide, so that users can use whichever format they like.

    0 comments No comments

3 additional answers

Sort by: Most helpful
  1. EchoLiu-MSFT 14,621 Reputation points
    2020-10-20T02:16:21.233+00:00

    Hi @Bruce Minchinton ,

    The field can only input data according to the set data type (that is, only the data format of the data type supported by sqlserver can be input)

    @CompletedDate & @CompletedDate2 should be the date when declaring the data type, so users can only enter the date format supported by sql server, and DDMMYYY is not a date supported format.

    Please refer to the link for all formats supported by the date:
    date (Transact-SQL)datetime (Transact-SQL)

    If you have any question, please feel free to let me know.
    If the response is helpful, please click "Accept Answer" and upvote it.

    Regards
    Echo


    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

  2. Olaf Helper 47,441 Reputation points
    2020-10-20T06:28:41.807+00:00

    users enter the date in dd/mm/yyy format

    The CONVERT function accepts a style parameter, in this case style 103 = british format

    0 comments No comments

  3. Bruce Minchinton 61 Reputation points
    2020-10-20T06:42:12.277+00:00

    Thank you everyone. I changed the properties of CompletedDate(s) to date/time instead of text as I was following instructions on launching date picker.
    Date Picker works and as a bonus the date format of DDMMYYYY also works.

    No doubt I will be back online with further questions as I expand my capability, or try to.

    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.