How to get the difference between tow QDateTimes? - milliseconds

I want to get the milliseconds difference between two QDateTimes. My code is the following:
QDateTime t1 = QDateTime();
t1 = QDateTime::fromString("2019-12-16 23:59:59.974", "yyyy-MM-dd HH:mm:ss.zzz");
QDateTime t2 = QDateTime();
t2 = QDateTime::fromString("2019-12-17 00:00:00.018", "yyyy-MM-dd HH:mm:ss.zzz");
int nDiff = t2.time().msecsTo(t1.time());
printed nDiff value is 86399956.
What is wrong this code?

Related

I have A problem with dividing the value of 0 by 0 in an Access database

When I try to extract the result of Division 2 Field from table in access database
If I have a value of 0 an error occurs
sqlSTR = "SELECT TBL_Category_Item_File.Item_Org_Price2/TBL_Stocks_Balances.Item_QTY AS ['Price']FROM (((TBL_Category_Item_File INNER JOIN TBL_Suppliers_Product ON TBL_Category_Item_File.Item_ID = TBL_Suppliers_Product.Item_ID) INNER JOIN TBL_Suppliers ON TBL_Suppliers_Product.Supp_ID = TBL_Suppliers.Supp_ID) INNER JOIN TBL_Sub_categories ON TBL_Category_Item_File.ID_Sub_categories = TBL_Sub_categories.ID_Sub_categories) INNER JOIN TBL_Stocks_Balances ON (TBL_Stocks_Balances.Item_ID = TBL_Category_Item_File.Item_ID) AND (TBL_Category_Item_File.Item_BarCode = TBL_Stocks_Balances.Item_Barcode) WHERE tbl_Category_Item_File.Catg_ID =" & Split(cmblist.Text, " - ")(0)
If you are trying to avoid the error, you can use a CASE statement in your query to check for a 0 value in the Item_Org_Price2 field and return a different value if it is 0. For example, you could do something like this:
SELECT
CASE
WHEN TBL_Category_Item_File.Item_Org_Price2 = 0
THEN 0
ELSE TBL_Category_Item_File.Item_Org_Price2/TBL_Stocks_Balances.Item_QTY
END AS ['Price']
FROM (((TBL_Category_Item_File INNER JOIN TBL_Suppliers_Product ON TBL_Category_Item_File.Item_ID = TBL_Suppliers_Product.Item_ID) INNER JOIN TBL_Suppliers ON TBL_Suppliers_Product.Supp_ID = TBL_Suppliers.Supp_ID) INNER JOIN TBL_Sub_categories ON TBL_Category_Item_File.ID_Sub_categories = TBL_Sub_categories.ID_Sub_categories) INNER JOIN TBL_Stocks_Balances ON (TBL_Stocks_Balances.Item_ID = TBL_Category_Item_File.Item_ID) AND (TBL_Category_Item_File.Item_BarCode = TBL_Stocks_Balances.Item_Barcode) WHERE tbl_Category_Item_File.Catg_ID =" & Split(cmblist.Text, " - ")(0)

There's an error "Temporal data comparison should have the same data type."

created table as below:
login("admin", "123456")
if(existsDatabase("dfs://compoDB")){
dropDatabase("dfs://compoDB")
}
n = 1000000
ID = rand(100, n)
dates = 2017.08.07T00:00:00.000..2017.08.08T00:00:00.000
date = rand(dates, n)
x = rand(10.0, n)
t = table(ID, date, x)
dbDate = database(, VALUE, 2017.08.07..2017.08.11)
dbID=database(, RANGE, 0 50 100)
db = database("dfs://compoDB", COMPO, [dbDate, dbID])
pt = db.createPartitionedTable(t, `pt, `date`ID)
pt.append!(t)
then executed query as below:
select * from loadTable("dfs://compoDB", "pt") where date between 2017.08.07:2017.08.08
there's an error message:
Temporal data comparison should have the same data type.
why
The type of date in where clause should be the same as that in the table. The correct query statement is as follows:
select * from loadTable("dfs://compoDB", "pt") where date between 2017.08.07T00:00:00.000:2017.08.08T23:59:59.999

What is the Purpose of the IsNull in this query?

I have a desktop application that I am converting to web and I am having trouble understanding the purpose of the IsNull parts of the query. The query is for Ms SQL and I know it has a IsNull function but this is not it. So I'm confused as to it's purpose. Below is my query:
UPDATE tb_category
SET
Email = #Email,
CandidateID = #CandidateID,
Code = #Code,
TestDate = #TestDate,
Description = #Description,
PointsEarned = #PointsEarned,
PointsAvailable = #PointsAvailable,
Average25th = #Average25th,
Average75th = #Average75th,
ImportedDate = #ImportedDate,
CreationDate = #CreationDate,
TestNum = #TestNum,
CategoryNum = #CategoryNum
WHERE ((Email = #Original_Email)
AND (CandidateID = #Original_CandidateID)
AND (Code = #Original_Code)
AND (TestDate = #Original_TestDate)
AND ((#IsNull_Description = 1 AND Description IS NULL) OR (Description = #Original_Description))
AND (PointsEarned = #Original_PointsEarned)
AND ((#IsNull_PointsAvailable = 1 AND PointsAvailable IS NULL) OR (PointsAvailable =
#Original_PointsAvailable))
AND ((#IsNull_Average25th = 1 AND Average25th IS NULL) OR (Average25th = #Original_Average25th))
AND ((#IsNull_Average75th = 1 AND Average75th IS NULL) OR (Average75th = #Original_Average75th))
AND ((#IsNull_ImportedDate = 1 AND ImportedDate IS NULL) OR (ImportedDate = #Original_ImportedDate))
AND ((#IsNull_CreationDate = 1 AND CreationDate IS NULL) OR (CreationDate = #Original_CreationDate))
AND (TestNum = #Original_TestNum)
AND (CategoryNum = #Original_CategoryNum));
I tried simplifying the update statement by removing the IsNull sections but that did not work.
In SQL null is not equal (=) to anything—not even to another null, so in your query in case if both values are null (old and new one) you need to take that into account and check values with IS NULL.
I'm seeing this pattern repeated several times in the WHERE clause:
#IsNull_Description = 1 AND Description IS NULL
It means that a variable, #IsNull_SomeColumnName, which is presumably set earlier in the code, has a value of 1, and the column that the variable relates to is currently NULL.
The function IsNull(Param1, Param2) is used to substitute the value of the second parameter for the value of the first parameter if the first parameter IS NULL, and the function returns the value of Param2.
In SQL Server, and quite a few other RDBMSs, the IS NULL syntax is used to check if a value is currently NULL. Here, Description IS NULL will return TRUE if, well, Description is null, and FALSE if it is not.

Catching last datetime after update wont result

I have the following code:
UPDATE sm
SET sm_fecha_venc = (SELECT MIN(nd_fecha_venc) FROM [db_facturacion].[dbo].[tb_notas_debito]
WHERE sm_codigo = nd_num_servicio
-- AND nd_referencia = sm_cod_producto
AND nd_num_factura = sm_cod_factura
AND nd_estado = 'G')
,sm_fecha_ult_pago = (SELECT MAX(nd_fecha_pago) FROM [db_facturacion].[dbo].[tb_notas_debito]
WHERE sm_codigo = nd_num_servicio
-- AND nd_referencia = sm_cod_producto
AND nd_num_factura = sm_cod_factura
AND nd_estado = 'C')
,sm_fecha = GETDATE()
,sm_cod_factura_ren = #i_num_factura
OUTPUT DELETED.sm_fecha_venc AS FECHA_VENC_OLD,
DELETED.sm_fecha_ult_pago AS FECHA_ULT_PAGO_OLD,
DELETED.sm_fecha AS FECHA_OLD,
DELETED.sm_cod_factura_ren AS COD_FACTURA_REN_OLD
INTO #TABLATABLA --
FROM [db_facturacion].[dbo].[tb_servicios] sm --WITH(NOLOCK)
INNER JOIN #servicios AS T ON sm_codigo = num_servicio
WHERE sm_tipo_bien_protegido = 1
AND [sm_estado] = 1
--AND sm.sm_cod_forma_contrato = 1
AND sm.sm_tipo_inventario = tipo_inventario
/*===========================
INSERTED DELETED
=============================*/
UPDATE db_facturacion.[dbo].[tb_log_cambio_servicio]
SET cs_fecha_venc_old = FECHA_VENC_OLD
,cs_fecha_ult_pago_old = FECHA_ULT_PAGO_OLD
,cs_fecha_old = FECHA_OLD
,cs_cod_factura_ren_old = COD_FACTURA_REN_OLD
FROM #TABLATABLA
WHERE cs_codigo = #ID_ULTIMO_ING (LAST ##IDENTITY)
Im trying to save the last dates into a log table, just in case I have to return those dates back.
The code works good, just that it's catching the actual date and inserted in the log table.
What am I missing?

Linq: Group by Time Interval

I've the following Datatable:
[Date];[Time];[ProcessID];[ID]
24.09.2012;08:18:00;4000;1
24.09.2012;08:18:00;4000;2
24.09.2012;08:19:00;4001;3
24.09.2012;08:23:00;4002;4
24.09.2012;08:32:00;4003;5
...
As result I would like to have a grouping by a 15-minute Time-Interval to count the ProcessID´s:
[Interval];[ProcessID Count]
8:15:00 - 8:29:00;3
8:30:00 - 8:45:00;1
How to achieve this goal?
Here's the solution against Linq-to-SQL (on SQL Server) with the DateTime being in a single field (a LINQpad query):
Const BaseDate As Date = #9/24/2012#
Const PeriodMinutes As Integer = 15
Dim p = From t In TestData _
Group By q=CInt(Math.Floor((t.SomeDate - BaseDate).TotalMinutes/PeriodMinutes)) Into Group _
Select s=BaseDate.AddMinutes(q*PeriodMinutes), _
e=BaseDate.AddMinutes((q+1)*PeriodMinutes), _
ids=Aggregate g In Group Select g.ProcessID Distinct Into Count()
p.Dump
Using different types and other "little" adjustments could produce simpler SQL backend code.
For your actual database, you need to adjust the Group By, although it now depends upon what your Time column is converted to through Linq-to-SQL and/or your database's driver.
As it is a TimeSpan the Group By becomes something like:
Group By d=t.[Date], q=CInt(Math.Floor(t.[Time].TotalMinutes/PeriodMinutes)) Into Group _
Select s=d.AddMinutes(q*PeriodMinutes), _
e=d.AddMinutes((q+1)*PeriodMinutes), _
ids=Aggregate g In Group Select g.ProcessID Distinct Into Count()
This should work (however, not tested):
Dim groupedByQuarter =
From r In table
Let day = r.Field(Of Date)("Date").Date
Let time = r.Field(Of TimeSpan)("Time")
Let quarter = time.Add(TimeSpan.FromMinutes((-1) * time.Minutes Mod 15))
Group r By Key = New With {Key .day = day, Key .quarter = quarter} Into TimeGroup = Group
Select New With {
.Interval = String.Format("{0} - {1}",
Key.quarter,
Key.quarter.Add(TimeSpan.FromMinutes(14))),
.ProcessCount = TimeGroup.Select(Function(r) r.Field(Of Int32)("ProcessID")).Distinct().Count()
}