Search code examples
sqlsql-serversql-server-2008delphidelphi-7

Delphi7: Get SQL server system Date and Time format


I want to get Regional date and time format for SQL Server.

I am using Delphi7 here. My scenario is as below:

If my server's regional date format is (e.g.) yyyymmdd and client's SQL server is having mm/dd/yyyy (or any other valid date format) than query generated should contain proper date format.

Note: I have code written in delphi7 which set default format for MSSQL and will read server's regional date format too. And will convert the server's regional date format to MSSQL date format.


Solution

  • If my server's regional date format is (e.g.) yyyymmdd and client's SQL server is having mm/dd/yyyy (or any other valid date format) than query generated should contain proper date format.

    You are making it to difficult for yourself.
    The queries that you build do not have to care anything at all about how the regional format of your client machine or your sql server is setup.
    There are language neutral formats that sql server will always understand, regardless of any regional setting.

    You can find them here http://karaszi.com/the-ultimate-guide-to-the-datetime-datatypes

    So when you really want to build your query in text than use
    "YYYYMMDD" for date, and "YYYYMMDD hh:mm:ss" ex('20170831 14:23:05') for datetime.
    Now you don't have to care about the formats anymore.

    Better would be offcourse to use parameters for your queries.