whats new ¦  programming tips ¦  indy articles ¦  intraweb articles ¦  informations ¦  links ¦  interviews
 misc ¦  tutorials ¦  Add&Win Game

Tips (1541)

Database (90)
Files (137)
Forms (107)
Graphic (114)
IDE (21)
Indy (5)
Internet / LAN (130)
IntraWeb (0)
Math (76)
Misc (126)
Multimedia (45)
Objects/
ActiveX (51)

OpenTools API (3)
Printing (35)
Strings (83)
System (266)
VCL (242)

Top15

Tips sort by
component


Search Tip

Add new Tip

Add&Win Game

Advertising

28 Visitors Online


 
...adapt DateTime values for different SQL-Server formats?
Autor: Sven Söhnel
Homepage: http://www.soehnel-software.de
[ Print tip ]  

Tip Rating (10):  
     


{
Wenn man mit verschiedensprachigen (MS-)SQL-Servern arbeitet,
hat man ab und an das Problem, Datumswerte in ein für den
jeweiligen Server verständliches Format umzuwandeln.
}

{
If you work with different (MS-)SQL-Server, you have sometimes the
problem what the date value is in the correct format.
}


function TForm1.GetSQLDateTimeFormat(UDL: string): string;
begin
  
Screen.Cursor := crSQLWait;
  if ADOConnection1.Connected then ADOConnection1.Close;
  ADOConnection1.ConnectionString := 'FILE NAME=' + UDL;
  ADOQuery1.SQL.Clear;
  ADOQuery1.SQL.Add('sp_helplanguage @@LANGUAGE');
  Application.ProcessMessages;
  try
    try
      
ADOQuery1.Open;
    except
      on 
E: Exception do MessageBox(Handle,
          PChar('Die Abfrage konnte nicht geöffnet werden:' + #13#10 + #13#10 + E.Message),
          PChar('Fehler!'), 16);
    end;
    if (ADOQuery1.Active) and (ADOQuery1.RecordCount > 0) then
      
Result := ADOQuery1.FieldByName('dateformat').AsString;
  finally
    
Screen.Cursor := crDefault;
  end;
end;



function DateTimeToSQLDateTimeString(Data: TDateTime; Format: string;
  OnlyDate: Boolean = True): string;
var
  
y, m, d, h, mm, s, ms: Word;
begin
  
DecodeDate(Data, y, m, d);
  DecodeTime(Data, h, mm, s, ms);
  if Format = 'dmy' then
    
Result := IntToStr(d) + '-' + IntToStr(m) + '-' + IntToStr(y)
  else if Format = 'ymd' then
    
Result := IntToStr(y) + '-' + IntToStr(m) + '-' + IntToStr(d)
  else if Format = 'ydm' then
    
Result := IntToStr(y) + '-' + IntToStr(d) + '-' + IntToStr(m)
  else if Format = 'myd' then
    
Result := IntToStr(m) + '-' + IntToStr(y) + '-' + IntToStr(d)
  else if Format = 'dym' then
    
Result := IntToStr(d) + '-' + IntToStr(y) + '-' + IntToStr(m)
  else
    
Result := IntToStr(m) + '-' + IntToStr(d) + '-' + IntToStr(y); //mdy: ; //US
  
if not OnlyDate then
    
Result := Result + ' ' + IntToStr(h) + ':' + IntToStr(mm) + ':' + IntToStr(s);
end;



//Example:
//Beispiel:

procedure ConvertSQLDateTime;
begin
  
ShowMessage(DateTimeToSQLDateTimeString(now, GetSQLLanguage('C:\DBEngl.udl')));
end;

 

Rate this tip:

poor
very good


Copyright © by SwissDelphiCenter.ch
All trademarks are the sole property of their respective owners