Recent

Author Topic: MS sql. How to update the time in a Datetime  (Read 1087 times)

SteenJorgensen

  • Jr. Member
  • **
  • Posts: 59
MS sql. How to update the time in a Datetime
« on: December 08, 2018, 10:41:04 am »
How to update the time only in a datetime.

I miss a function eg.:
myfielddatetime = SETTIME([myfielddatetime], '20:16')

Are there any hint?

----------------------------------------
Lazarus version 2.0.2
FPC 3.0.4

Zvoni

  • Sr. Member
  • ****
  • Posts: 355
Re: MS sql. How to update the time in a Datetime
« Reply #1 on: December 08, 2018, 12:07:07 pm »
To clear it up:
You have a datetime-field already (!!) containing a date (e.g. Dec. 7th 2018) and now you want to ADD a Time (say 20:16)?
Curious if you'll see my hint.....
One System to rule them all, One IDE to find them,
One Code to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
People call me crazy, because i'm jumping out of perfectly fine aircrafts

lucamar

  • Hero Member
  • *****
  • Posts: 2857
Re: MS sql. How to update the time in a Datetime
« Reply #2 on: December 08, 2018, 01:36:26 pm »
How to update the time only in a datetime.

I miss a function eg.:
myfielddatetime = SETTIME([myfielddatetime], '20:16')

Are there any hint?

Well, there is something similar in dateutils: RecodeTime()

Code: Pascal  [Select][+][-]
  1. MyDataSet.FieldByName['MyDate'].AsDateTime := RecodeTime(
  2.     MyDataSet.FieldByName['MyDate'].AsDateTime,
  3.     NewHour, NewMinutes, NewSeconds, NewMilliseconds);

or maybe like this:

Code: Pascal  [Select][+][-]
  1. var ADateTime: TDateTime;
  2.  
  3. {. . .}
  4.  
  5.   ADateTime := MyDataSet.FieldByName['MyDate'].AsDateTime;
  6.   ADateTime := RecodeTime(
  7.     ADateTime,NewHour, NewMinutes, NewSeconds, NewMilliseconds);
  8.   MyDataSet.FieldByName['MyDate'].AsDateTime := ADateTime;

or if you insist on using a string:

Code: Pascal  [Select][+][-]
  1. var
  2.   ADateTime: TDateTime;
  3.   ATime: TDateTime;
  4.   dd, mm, yy,
  5.   hh, mn, ss, ms: word;
  6. begin
  7. {. . .}
  8.   { Get date from the field }
  9.   ADateTime := MyDataSet.FieldByName['MyDate'].AsDateTime;
  10.  
  11.   { Get the needed time vars from time string}
  12.   ATime := StrToTime('20:16');
  13.   DecodeDateTime(ATime, yy, mm, dd, hh, mn, ss, ms);
  14.  
  15.   { Recode the date with the new time }
  16.   ADateTime := RecodeTime(
  17.     ADateTime,NewHour, NewMinutes, NewSeconds, NewMilliseconds);
  18.  
  19.   { And set the field with the new value }
  20.   MyDataSet.FieldByName['MyDate'].AsDateTime := ADateTime;
  21. end;

You get the drift, I hope :D
« Last Edit: December 08, 2018, 01:57:02 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.8/FPC 3.0.4 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

 

TinyPortal © 2005-2018