I need help converting UTC date that is saved as the bigint number 1538397000000 to a CST datetime.
I have tried
select
Dateadd(s,convert(Bigint,1538397000000/1000,convert(datetime,'1-1-1970'))
You can create a function that converts UTC to local time. Assuming your above code correctly converts the bigint to a proper UTC date/time value, then the following will convert that value to CST.
create function UTCDateTimeToLocal(#value datetime) returns datetime as
begin
declare #utc datetime = getutcdate()
,#local datetime = getdate()
,#diff int
,#rtn datetime
set #diff = datediff(millisecond, #utc, #local)
set #rtn = dateadd(millisecond, #diff, #value)
return #rtn
end
go
select dbo.UTCDateTimeToLocal(dateadd(s,cast(1538397000000/1000 as bigint),convert(datetime,'1-1-1970')))
--drop function UTCDateTimeToLocal
Related
I want to convert UTC datetiem stored in postgres db whilte comparing with user given datetime with given zone
data.filter(
and_(
func.timezone(user_time_zone, Table.created_at) >= start_date,
func.timezone(user_time_zone, Table.created_at) <= end_date,
)
)
This is not working, any suggestions
I have a timestamp stored this way in database:
2021-08-14 12:19:58+00
I need to display it in this format
2021-08-14 12:19:58 UTC
Use the to_char function with the TZ format:
SELECT to_char(current_timestamp, 'YYYY-MM-DD HH24:MI:SS TZ');
to_char
══════════════════════════
2021-09-21 23:06:15 CEST
(1 row)
I have created ssrs report where user can input date start and end parameter in this format yyyy-mm-dd as they are not aware of UTC Ticks. but the query that runs in the back to pull data requires UTC ticks to be inputted . Here is query and how can i modify this query to input so converts user input date into UTC ticks before it runs?
With all due respect to HABO as shown in Convert DATE to UTCTicks for sql query
declare #TicksPerDay as BigInt = 24 * 60 * 60 * Cast( 10000000 as BigInt );
select DateDiff( day, '00010101', '2020-03-02' ) * #TicksPerDay as TodayInUTCTicks;
I have a column called Last Payment as such
last payment
12DEC09:00:00:00
all the observations follow this structure, I've tried taking a substring such that
data want;
set have;
last_payment=substr(last_payment,1,7);
run;
that doesn't work, I've tried formatting the date with the date7. and date.9 but both just return ********, can someone help me format it into a ddmmmyy ty.
You have to use datapart function before formating it in date7. or date9.
Example:
data new;
format date_new date9. ;
date_new = datepart("12DEC09:00:00:00"dt);
run;
The censored text means wrong format used.
if last_payment is a string then replace "datetime_as_string " with last_payment.
if last_payment is datetime then replace "datetime_as_dt" with last_payment.
The code below will handle the two cases where you are reading the date time as string and numeric values.
Code:
data new;
format datetime_as_dt datetime21. dt_to_date date7. datetime1 date7.;
datetime_as_dt="12DEC09:00:00:00"dt;
datetime_as_string ="12DEC09:00:00:00";
/*Converting datetime to date*/
dt_to_date= datepart(datetime_as_dt);
/*converting datetime string to date*/
/*Option1: Convert string to datetime then extract date*/
datetime1 = datepart(input(datetime_as_string,datetime21.));
/*Option2: Extract date from string then convert to date*/
datetime2 = scan(datetime_as_string,1,':');
put _all_;
run;
Log:
datetime_as_dt=12DEC2009:00:00:00
datetime_as_string=12DEC09:00:00:00
dt_to_date=12DEC09
datetime1=12DEC09
datetime2=12DEC09
Table created:
I have a text field that is in this format May 21 2013 9:45AM. How would I convert that to datetime? I tried the following
UPDATE WaterRevLienInfo
SET LienDate = CONVERT(DATETIME, CONVERT(VARCHAR(30), LienDate), 101)
It works as a select and comes out like 2013-05-21 09:45:00.000 but not as an update. Any help would be great.
You appear to be "round tripping" the the LienDate field by casting it to a varchar and then back to a datetime. I'm not sure what this is accomplishing.
However, if you have a text value in the format you specified (which is, by default, how SQL represents datetime fields when casted to a varchar), you can just do a straight convert:
DECLARE #DateText varchar(30) = 'May 21 2013 9:45AM';
UPDATE WaterRevLienInfo
-- Convert the DateText string value for storage in a datetime field.
SET LienDate = CONVERT(datetime, #DateText);
This should also work with other fields in the same table.
UPDATE WaterRevLienInfo
SET LienDate = CONVERT(datetime, DateTextField);
The datetime hasn't any format it just a data. The presentation must care about the format.