Topic: SQLQuery with localized date format

Does anyone knows a trick to display the result of SQL query including a date to be displayed in the localized date format the same way it does when using the auto display in a grid?

I already know how to force a date format with strftime with Sql but it becomes strange when the app displays a mix of date formats depending on how the grid has been designed, whether with autogrid or with an Sql query button/auto search.

This is important to me as I want the app to be international so I wonder what is used within MVD to display the date in local date format in the autogrids.

Re: SQLQuery with localized date format

tcoton wrote:

Does anyone knows a trick to display the result of SQL query including a date to be displayed in the localized date format the same way it does when using the auto display in a grid?

I already know how to force a date format with strftime with Sql but it becomes strange when the app displays a mix of date formats depending on how the grid has been designed, whether with autogrid or with an Sql query button/auto search.

This is important to me as I want the app to be international so I wonder what is used within MVD to display the date in local date format in the autogrids.

Attach Screenshot

3 (edited by tcoton 2023-09-18 19:50:41)

Re: SQLQuery with localized date format

No need for a screenshot.

Let's say my regional settings are set to this date time format: DD/MM/YYYY HH:MM:ss

Date time are stored in ISO8601 format by default when using SQLite : YYYY-MM-DD HH:MM:ss

Using a table grid to get a date time value returns the date in my regional settings: DD/MM/YYYY HH:MM:ss, if I change my regional settings, the results are displayed using these new settings.

Using an SQL query without forcing the date time format returns the ISO8601 date time: YYYY-MM-DD HH:MM:ss

unless someone knows a script that would force the date time to display using the regional settings.

My point is that I am a European living in the USA and their date time format is different compared to many countries and I am developing an app not only for the US but also for other countries and some of my grids require SQLqueries that cannot be made using the tablegrid default queries (i.e: complex joins). Not to mention that date separators differ from country to country: France is dd/mm/yyyy, Sweden is yyyy-mm-dd, Germany is dd.mm.yyyy, USA is mm/dd/yyyy and... the time in the US is in 12 hours format with AM and PM.

Post's attachments

Attachment icon Date Time Formats.png 122.96 kb, 3 downloads since 2023-09-18 

Re: SQLQuery with localized date format

DateTimeToStr()
DateToStr()
TimeToStr()
FormatDateTime()
SQLDateTimeToDateTime()
strftme()
and working with registry

Re: SQLQuery with localized date format

I have actually sent a screenshot in my previous answer to show that using any kind of datetostr variance does not help to make "universal" application, I wish I knew what is behind the tablegrid auto display query for the dates as it uses the regional settings every time.

Re: SQLQuery with localized date format

If you are interested in what's inside, take a book on Delphi.
But if you do it from what’s available in the program, that’s completely different.
I hope nothing prevents you from determining the date and time format on your computer ...
And the query in the script can be modified  ...

7 (edited by CDB 2023-09-19 09:34:20)

Re: SQLQuery with localized date format

I don't think this is exactly possible using SQLite.


I think the only way to do what you want, is to use MVD to interrogate Windows for the locale code, and then use a SQL iif.... or CASE function to then select the required SQL strftime formatting function.


As an aside I think SQL Server can do what you want.

On a clear disk you can seek forever

Re: SQLQuery with localized date format

Can you clarify your idea?

I don't think this is exactly possible using SQLite.
...then select the required SQL strftime formatting function....
...As an aside I think SQL Server...

I can't put it together.

9 (edited by CDB 2023-09-20 10:53:41)

Re: SQLQuery with localized date format

Naturally Sparrow I'm having difficulty getting my registry function to work, but I'll post my general idea.


Registry function.


function ReadReg(thekey, value: string): string;
var
  Rg: TRegistry;
begin
  //Create the Object
  Rg := TRegistry.Create;
  with Rg do
  begin    
    RootKey := HKEY_LOCAL_MACHINE;

   //False = no write ability

   if OpenKey(theKey, false) then
    begin     
      if ValueExists(value) then
     
        //Read the value from the field
        Result := Readstring(value)       
        
      else
        ShowMessage(The value + ' does not exists under ' + theKey);
    end
    else    
      //opening the key 
        ShowMessage('Error opening key : ' + theKey);
   CloseKey;
    end;
  end;

  //for demo purposes  called by
     form1.label1.TagString := ReadReg('\SYSTEM\CurrentControlSet\Control\Nls\Locale', 'Default');

Then from the locale code held in TagString

Do the following SQL Query pseudo code as I can't get the above function to work)

CASE when TagString = 0809 then
       format  dddd/mm/yyyy 
       AS thedate
else
CASE   when TagString = (whatever the US code is) then
       format  mm/dddd/yyyy 
       AS thedate

That is my general idea.

Oh yes, the error the above code keeps throwing is 'Error opening key'. The key exists as I'm looking at in Regedit.

On a clear disk you can seek forever

10 (edited by sparrow 2023-09-20 12:31:44)

Re: SQLQuery with localized date format

Here I'm querying the value of the first day of the week.

 
var
  reg: TRegistry;
begin
 reg := TRegistry.Create;
  reg.Access := KEY_READ;
  reg.RootKey := HKEY_CURRENT_USER;
  reg.OpenKey('Control Panel\International',false);
  frstDay := strtoint(reg.ReadString('iFirstDayOfWeek'));                    // Get first week day ( 6 - sunday, 0 - monday...)
  reg.Free;
end;

I would like to note that this registry section already contains the information you need, not just the locale code.
Separators, short date time format, long date time format, etc.


P.S. If you have a time and date format on the computer where the program is running, you can generate strftime()
and substitute it into your queries in the script.   '%Y-%m-%d'    '%Y/%m/%d'     '%d/%m/%Y'   '%d-%m-%Y' ... As you will need
For large output volumes in a "tablegrid" this will be the most optimal way, in my opinion. For single output in "Edit"
you can use the combination DateToStr(SQLDateTimeToDateTime()) .

Perhaps you will go a different way, your own.

11

Re: SQLQuery with localized date format

That seems a neater way to do it than my idea Sparrow.

Perhaps TCoton can let us know if that works for his project.

On a clear disk you can seek forever