How to convert SQL query millisecondsSinceEpoch integer to DateTime in Dart? - sql

dateTime is stored as an INTEGER, how do I turn each row into Darts DateTime object without wasting time?
query = await db.rawQuery(
'''SELECT dateTime FROM days WHERE (dateTime BETWEEN ${start.millisecondsSinceEpoch} AND ${end.millisecondsSinceEpoch})''');
I want this query to return a map/list of DateTime insted of milliseconds.

why not just :
fetch all data
for each data convert it into a DateTime, like this :
DateTime day = DateTime.fromMillisecondsSinceEpoch(data['timestamp_day']);

Related

Convert String datetime to numeric date time

I have a column that I need converting from string date time to an actual data time data value.
The current format is as follows
15-JUN-22 10.24.10.414000
and I need to change it to the following format
15-06-22 10.24.10.414000
I use a stored procedure to automatically change the format, but it fails on this stage due to the non numeric characters.
Is there a way map or change all the string months to int values within the datetime? and if so how?
Converts used so far
TRY_CAST(CREATE_DATE AS DATETIME2(7)) AS CREATE_DATE
AND
CASE WHEN LEN([INTERVAL_START_DATE]) > 0 THEN TRY_CONVERT(DATETIME2, [INTERVAL_START_DATE], 103) ELSE NULL END
try this
CAST({your field name} as date) CONVERT(date,'{systemClock.UtcNow.ToString("o")}',127)

Convert varchar which represents DateTime in universal format (dd/MM/yy hh:mm) to DateTime

I have a table with a varchar column which represent date and time in the next format:
dd/MM/yy hh:mm
For example: 27/01/13 07:57
I need to convert this column to DateTime type.
I tried to do it without success.
Assume that #varcharDT contains the VarChar DateTime and I want insert the converted value to #dateTimeDT variable.
DECLARE #varcharDT varchar(1000)
SELECT #varcharDT = dateTimeColumn from dbo.tempTable
--Trying to convert
DECLARE #dateTimeDT DateTime
SET #dateTimeDT= CONVERT (DATETIME, #varcharDT )
The last line raise the next exception:
Conversion failed when converting date and/or time from character string.
I think the conversion fails because my VarChar DateTime is in custom format.
How can I solve it?
I am working with SQL Server 2008 R2
Thanks
Try:
CONVERT(datetime, #varcharDT, 3)
The last number is the style for the convert. The list can be found in the MSDN documentation article CAST and CONVERT (Transact-SQL).

How to add both date and time in DateTime variable

I have two datetime variables in VB.Net
Dim inDt As Datetime //contains date
Dim inTime As DateTime //contains time
I want to add both these variable in single DateTime variable , i didn't find any .Net function to do that.Is there any other way to do it?
Illustrating my comment above, you can do this:
DateTime combinedDateTime = inDt +
new TimeSpan(0, inTime.Hour, inTime.Minute, inTime.Second, inTime.Millisecond);
You could make use of the Ticks property and the AddTicks method:
Dim result As DateTime
result = inDt.AddTicks(inTime.Ticks)

How do I strip the date off of a datetime string in SQL SSIS?

I'm working on a data warehouse project and would like to know how to (preferably in a Derived Column component in a Data flow) strip the date piece off of a SQL datetime record.
Once I have the datetime converted to just a time I am going to do a lookup on the time to find the related time record in a time dimension table.
Can someone give me a simple function to accomplish this inside a derived column transform?
Example: Transform a datetime such as "12/02/2008 11:32:21 AM" into simply "11:32:21 AM".
I would just do a cast to DT_DBTIME type (using Derived Column transform, or Convert type transform). DT_DBTIME contains just (hours, minutes, seconds) part of the date/time, so you'll get rid of the date part.
If you need to do this in a variable expression Michael's solution won't work, but you can use the following expression:
(DT_DATE)(DT_DBDATE)GETDATE()
(DT_DBDATE) converts the current date and time to a date only. But the new datatype is not compatiple with SSIS's datetime. Therefore you'll have to use (DT_DATE) for converting to a compatible type.
Courtesy of this solution belongs to Russel Loski who has posted it in his blog:
http://www.bidn.com/blogs/RussLoski/ssas/1458/converting-datetime-to-date-in-ssis
Actually if you reverse the first 2 expressions like this: (DT_DBDATE)(DT_DATE)GETDATE()
instead of (DT_DATE)(DT_DBDATE)GETDATE(), then you will TRUNCATE the time off the date field.
If the DT_DATE expression is before the DT_DBDATE expression, you will still have the time portion in your output, but it will be reset to all zeroes.
Ran into this with writing a report for a scheduling app, needed the time that was stored as part of a datetime data type. I formated the datetime as 0 which gives you this mon dd yyyy hh:miAM (or PM), and just did a substring of that which returned the time only in an AM/PM format.
Example below.
DECLARE #S DATETIME = GETDATE()
SELECT SUBSTRING(CONVERT(NVARCHAR(30), #S , 0) , 13 , 10) AS ApptTime
, CONVERT(NVARCHAR(30), #S , 0) AS ApptDate
I personally use a series of functions for this. E.g.:
ALTER FUNCTION [dbo].[TIMEVALUE]
(
#Datetime datetime
)
RETURNS datetime
AS
BEGIN
RETURN (#Datetime - CAST(ROUND(CAST(#Datetime AS float), 0, 1) AS datetime))
END
I'd love to claim all the credit but it should really go to this guy.

how to convert nvarchar to time ,not datetime?

DECLARE #DateNow smalldatetime
SET #DateNow='12:30'
select #DateNow
-------------------------------------OR--------------------------------------
select CAST( '12:30' as datetime )
Result: 1900-01-01 12:30:00.000 (i don't want this)
But i need this result in time format not string not datetime?
Result: 12:30 (i want this)
Like José said, you can use CONVERT to display a datetime as date. MSDN has a list of all possible formats. For example format 8 is hh:mi:ss:
select convert(varchar(32),getdate(),8)
12:51:21
Now, you can cut the seconds off by specifying less characters:
select convert(varchar(5),getdate(),8)
12:51
Another often used format is 121, yyyy-mm-dd hh:mi:ss.mmm(24h):
select convert(varchar(32),getdate(),121)
2009-05-08 12:51:21.987
Where you can pick the time part like:
select substring(convert(varchar(32),getdate(),121),12,5)
12:51
Or to combine the string trickeries:
select right(convert(varchar(16),getdate(),121),5)
12:51
Right? Right!
There's a TIME type in SQL Server 2008, but in previous versions, you can represent it as a varchar for display.
This is how you can retrieve the time portion of a DATETIME field in the format you want.
DECLARE #DateNow smalldatetime
SET #DateNow = GETDATE() -- 2009-05-08 12:58:02.680
SELECT CONVERT(CHAR(5), #DateNow, 8)
-- this returns the time portion as 12:58
You can use the CONVERT function.
By the way, there's no "TIME" datatype in SQL Server. So, the converted result will always be a STRING.
EDIT: There's no "TIME" datatype in SQL Server versions < SQL Server 2008.
If you need to compare, add and/or subtract time (only) values, think about the possibility to use a INT to store the "TIME" value and then to CONVERT it to CHAR when displaying the result.