Adding DATE and TIME not working when TIME is midnight - datetime

I have created a table using the following DAX.
Date Selection =
ADDCOLUMNS (
CROSSJOIN(
CALENDAR ( MIN ( 'Date'[Date] ), MAX ( 'Date'[Date] )),
SELECTCOLUMNS(GENERATESERIES(0,23.5,.5),"Time",TIME([Value],([value]-INT([value]))*60,0))
),
"Visual Date", FORMAT([Date],"dd/mm/yy") + [Time],
"Order By", FORMAT([Date],"yyyy") & "/" & FORMAT([Date],"MM"),
"Type", "Half Hourly",
"Order", 5
)
So my issue is when I add my DATE and TIME column together to create a column called Visual Date, everything looks fine but when it reaches to the time 00:00:00 it does not seem to add that time to the corresponding day, it leaves it just as it is.
Is there something that I need to include in order to see 01/01/2021 00:00:00 ?

This is just a visual formatting issue. In the tab go to the Format field and select whatever you want to see.

Related

woocommerce booking subtract weeks or days from date picked

I am using Woothemes Woocommerce Bookings. I want users to be able to enter a date in the product booking date picker and have the system make the booking three weeks prior to that date.
e.g. customer uses the date picker to select 28/04/2018 (28th April 2018) but the booking is made for 07/04/2018 (7th April 2018).
All bookings are made in blocks of 5 weeks (35 days) but they enter in a date that results in their booking being 3 weeks (21 days) before that date, and 2 weeks (14 days) after. Customers only select one date, there is no start and end date for them to select.
The documentation references a hook called woocommerce_new_booking that you can use in order to modify the booking date before it's saved to the database. This code will modify the booking start and end date to 3 weeks prior to the date they select. Add this code to your functions.php file.
add_action( 'woocommerce_new_booking', 'modify_woocommerce_booking_date');
function modify_woocommerce_booking_date( $booking_id ) {
$booking = new WC_Booking( $booking_id );
$booking_start = $booking->get_start();
$booking_end = $booking->get_end();
$new_booking_start = strtotime ( '-3 week' , $booking_start ) ;
$new_booking_end = strtotime ( '-3 week' , $booking_end ) ;
$booking->set_start( $new_booking_start );
$booking->set_end($new_booking_end);
$booking->save();
}

.net datareader changing a date within string/varchar returned from SQL Server

Problem
In trying to get the week end date, the VB.NET DbDataReader seems to be changing the date within a string that is returned back from the database.
Using 18-jan-2018 and 1-mar-2018 as the start and end dates respectively, I expect to get back the following strings from the database.
Week Ending 21/01/2018
Week Ending 28/01/2018
Week Ending 04/02/2018
Week Ending 11/02/2018
Week Ending 18/02/2018
Week Ending 25/02/2018
Week Ending 04/03/2018
On my local development system, I get back the correct dates as above. On other development systems and production systems I get back
Week Ending 22/01/2018
Week Ending 29/01/2018
Week Ending 05/02/2018
Week Ending 12/02/2018
Week Ending 19/02/2018
Week Ending 26/02/2018
Week Ending 05/03/2018
Code
The SQL stored procedure takes a start and end date from the .net front end. The values passed in are correct.
SQL Stored Procedure
SET #dteStartDate = CAST(#prmStartDate As smalldatetime)
SET #dteEndDate = CAST(#prmEndDate As smalldatetime)
DECLARE #MyTempTable TABLE (
[tmpDate] [smalldatetime],
[NumReferrals] [int] NULL
)
DECLARE #dteCurrent AS SMALLDATETIME
DECLARE #NumReferrals AS INT
SELECT #dteCurrent = dbo.fnGetEndOfWeek(#dteStartDate)
WHILE #dteCurrent <= dbo.fnGetEndOfWeek(#dteEndDate)
BEGIN
--SELECT FROM DATA
INSERT INTO #MyTempTable (tmpDate, NumReferrals) VALUES (#dteCurrent, #NumReferrals)
SET #dteCurrent = DATEADD(Week, 1, #dteCurrent)
END
SELECT NumReferrals as Referrals,
'Week Ending ' + convert(nvarchar, tmpDate, 103) AS WeekEnding
FROM #MyTempTable
function fnGetEndOfWeek
CREATE FUNCTION dbo.fnGetEndOfWeek
(
#prmInputDate smalldatetime
)
BEGIN
Declare #AddNumOfDay INT
-- If input date is not Sunday, move it foward to Sunday
-- 1 Sunday
-- 2 Monday
-- ...
-- 7 Saturday
SELECT #AddNumOfDay = CASE (DATEPART(WEEKDAY, #prmInputDate))
WHEN '2' THEN '6'
WHEN '3' THEN '5'
WHEN '4' THEN '4'
WHEN '5' THEN '3'
WHEN '6' THEN '2'
WHEN '7' THEN '1'
WHEN '1' THEN '0'
END
RETURN DATEADD(d,#AddNumOfDay,#prmInputDate)
END
The results that come back from SQL Server are correct on all systems (Dev and Production.
The .NET code is as follows :
Dim dtSeriesData As New Data.DataTable
Using cmdGetSeries As SqlCommand = conLocal.CreateCommand()
Using sqlSeries As SqlDataAdapter = New SqlDataAdapter()
cmdGetSeries.CommandType = Data.CommandType.StoredProcedure
cmdGetSeries.CommandText = sp
For Each param As SqlParameter In sqlParams
If Not param Is Nothing Then
cmdGetSeries.Parameters.Add(param)
End If
Next
Using dr As System.Data.Common.DbDataReader = cmdGetSeries.ExecuteReader()
dtSeriesData.Load(dr) *
End Using
End Using
cmdGetSeries.Parameters.Clear()
End Using
At the asterisk (dtSeriesData.Load(dr)), the rows of data are for some reason changed, even though it is a string.
Question
Why would this be happening? we are all using .NET 4.5, SQL Server 2014. Even Casting the returned SELECT as a varchar, or setting the temporary table column type as varchar instead of datetime yields the same result, the String is manipulated by .NET except on my local machine. Is there some .net setting that i've missed somewhere?
EXTRA OUTCOMES FOLLOWING DISCUSSIONS IN COMMENTS
Using the jQuery UI DatePicker controller for selecting start and end dates, the following is executed on SQL Server (by using sql profiler)
exec spGetGraphReferralsbyWeekAll #prmStartDate='18 Jan 2018',#prmEndDate='01 Mar 2018'
This procedure returns the following data in SSMS
Week Ending 21/01/2018
Week Ending 28/01/2018
Week Ending 04/02/2018
Week Ending 11/02/2018
Week Ending 18/02/2018
Week Ending 25/02/2018
Week Ending 04/03/2018
Using the DataSet Visualiser in Visual Studio for dtSeries.Indexes(0).Table brings back
Week Ending 22/01/2018
The ##DATEFIRST variable brings back 7 in SSMS on machines that display the correct result, and machines that don't.
Changing the DateTime property from 103 to 127 within the stored procedure brings back the same (correct) date 2018-01-21T00:00:00 on machines that work and machines that don't work. The date is still changed by the dtSeriesData.Load part of the application.
conLocal is NOT a persistent SQL Connection
.AddWithValue is NOT used for adding parameters.

MS Acess: max(Date/Time field) on query when field may contain 00:00:00

I am trying to build a Query in MS Access that returns the last date/time for a given entity ID. Research shows that using the MAX() function on the corresponding field and using GROUP BY on the remaining fields appears to be the way to go.
However, this doesn't seem to work in the presence of values that hold 0 hours, 0 minutes and 0 seconds, as it shows those values as well. The query's SQL is as follows:
SELECT Int(Historico_Classificacoes.ID_Entidade) AS ID_Entidade, Max(Historico_Classificacoes.Timestamp_Classificacao) AS [Data da última classificação], Historico_Classificacoes.US_Indicia_Pais_Constituicao, Historico_Classificacoes.US_Indicia_Responsabilidades_Fiscais, Historico_Classificacoes.US_Indicia_Morada_Coletiva, Historico_Classificacoes.US_Indicia_Telefone, Historico_Classificacoes.US_Indicia_Proveniencia_Capital, Historico_Classificacoes.US_Indicia_Beneficiários, Historico_Classificacoes.US_Indicia_Naturalidade, Historico_Classificacoes.US_Indicia_Nacionalidade, Historico_Classificacoes.US_Indicia_Morada_Singular, Historico_Classificacoes.US_Indicia_Laboral
FROM Historico_Classificacoes
GROUP BY Int(Historico_Classificacoes.ID_Entidade), Historico_Classificacoes.US_Indicia_Pais_Constituicao, Historico_Classificacoes.US_Indicia_Responsabilidades_Fiscais, Historico_Classificacoes.US_Indicia_Morada_Coletiva, Historico_Classificacoes.US_Indicia_Telefone, Historico_Classificacoes.US_Indicia_Proveniencia_Capital, Historico_Classificacoes.US_Indicia_Beneficiários, Historico_Classificacoes.US_Indicia_Naturalidade, Historico_Classificacoes.US_Indicia_Nacionalidade, Historico_Classificacoes.US_Indicia_Morada_Singular, Historico_Classificacoes.US_Indicia_Laboral
ORDER BY Int(Historico_Classificacoes.ID_Entidade);
The Historico_Classificacoes table currently holds the following data:
"ID_Entidade";"Timestamp_Classificacao";"Classificacao_DMIF";"Notacao_Risco_BCFT";"US_Indicia_Pais_Constituicao";"US_Indicia_Responsabilidades_Fiscais";"US_Indicia_Morada_Coletiva";"US_Indicia_Telefone";"US_Indicia_Proveniencia_Capital";"US_Indicia_Beneficiários";"US_Indicia_Naturalidade";"US_Indicia_Nacionalidade";"US_Indicia_Morada_Singular";"US_Indicia_Laboral"
"62";20/9/2015 00:00:00;1;30;1;1;1;1;1;1;1;1;1;0
"62";28/9/2015 10:43:38;1;30;1;1;1;1;1;1;1;1;1;1
"62";29/9/2015 17:52:24;1;30;1;1;1;1;1;1;1;1;1;1
"62";29/9/2015 17:52:40;1;30;1;1;1;1;1;1;1;1;1;1
"98";20/9/2015 00:00:00;2;15;1;1;1;1;1;1;0;0;0;0
"98";20/9/2015 00:00:01;0;0;0;0;0;0;0;0;0;0;0;0
The query, when executed in Datasheet View, outputs the following:
"ID_Entidade";"Data da última classificação";"US_Indicia_Pais_Constituicao";"US_Indicia_Responsabilidades_Fiscais";"US_Indicia_Morada_Coletiva";"US_Indicia_Telefone";"US_Indicia_Proveniencia_Capital";"US_Indicia_Beneficiários";"US_Indicia_Naturalidade";"US_Indicia_Nacionalidade";"US_Indicia_Morada_Singular";"US_Indicia_Laboral"
62;29/9/2015 17:52:40;1;1;1;1;1;1;1;1;1;1
62;20/9/2015 00:00:00;1;1;1;1;1;1;1;1;1;0
98;20/9/2015 00:00:00;1;1;1;1;1;1;0;0;0;0
98;20/9/2015 00:00:01;0;0;0;0;0;0;0;0;0;0
There are duplicated records for entities 62 and 98, when only one record for each was expected. Am I missing something here? Why are the entries whose values hold 00:00:00 present?
You may want to consider using an additional query as an intermediate step that identifies the MAX Date/Time combination for each group ID first, then a follow up query that pulls the entire record where that Group ID, Date and Time match, this will ensure you won't have to use First or Min on the rest of your fields, and you will always get the correct data
You use Group By for the last fields like US_Indicia_Morada_Singular and US_Indicia_Laboral. You'll have to use First, Last, Min, or Max on these as well.
Here is your attempt (without the repeated alias)
SELECT INT(ID_Entidade) AS ID_Entidade
, MAX(Timestamp_Classificacao) AS [Data da última classificação]
, US_Indicia_Pais_Constituicao
, US_Indicia_Responsabilidades_Fiscais
, US_Indicia_Morada_Coletiva
, US_Indicia_Telefone
, US_Indicia_Proveniencia_Capital
, US_Indicia_Beneficiários
, US_Indicia_Naturalidade
, US_Indicia_Nacionalidade
, US_Indicia_Morada_Singular
, US_Indicia_Laboral
FROM Historico_Classificacoes
GROUP BY INT(ID_Entidade)
, US_Indicia_Pais_Constituicao
, US_Indicia_Responsabilidades_Fiscais
, US_Indicia_Morada_Coletiva
, US_Indicia_Telefone
, US_Indicia_Proveniencia_Capital
, US_Indicia_Beneficiários
, US_Indicia_Naturalidade
, US_Indicia_Nacionalidade
, US_Indicia_Morada_Singular
, US_Indicia_Laboral
ORDER BY INT(ID_Entidade);
From you comments, here is SQL that is close to what you need. I have added the field "AnotherField" for you as you may or may not need to add field here.
This currently selects the whole record from the table, but only the single "most recent" record for each value found in the AnotherField is listed.
It may be that you need more that one field where AnotherField appears in the SQL. Think of the field you use instead of AnotherField as being the fields that need to be used to find the maximum date record.
SELECT Main.*
FROM Historico_Classificacoes AS Main
INNER JOIN ( SELECT AnotherField
, MAX(Timestamp_Classificacao) AS [MaxDate]
FROM Historico_Classificacoes
GROUP BY AnotherField
)
AS MostRecent
ON ( Main.AnotherField = MostRecent.AnotherField
AND
Main.Timestamp_Classificacao = MostRecent.MaxDate
)

SQL Server Filtering by DateTime column, when TIME portion is provided sometimes

In an SSRS report, the user searches based on start date and end date.
The challenge is, as I discovered recently, he sometimes, not always, provides the time component while searching.
Currently, the filter is done like this:
if #pEndDate is null
SET #pEndDate = getdate()
SET #PEndDate = DateAdd(dd,1,#PEndDate)
SELECT ........
FROM .....
WHERE ( Createdon >= #PStartDate AND Createdon < #PEndDate)
This is fine when he searches without time (example - #PStartDate = 2/23/2015 and #PEndDate = 2/24/2015)
How should I structure the query to deal with the time portion when he provides it? (example - #PStartDate = 2/23/2015 15:00 and #PEndDate = 2/24/2015 15:00)
If this is answered elsewhere, please point me to it. Thank you.
If you just want to match the date part then there are lot options.
1) You can use the Date type for the parameter PEndDate and PStartDate to nullify the time part
2) You can use the Convert method to get only date part of the parameter while matching.CONVERT (DATE, #PEndDate) OR CONVERT(varchar,#PEndDate,103)
3) Get Date Part only from DateTime using DateTime functions
ATEADD(dd, 0,
DATEDIFF(dd, 0, #PEndDate))
4) Get Date Part only from DateTime using FLOOR and CAST functions
CAST( -- Convert the integer to DATE
FLOOR(-- Get largest Integer less than or equal to the decimal value
CAST(GETDATE() AS DECIMAL(12, 5)) -- Convert DATETIME to DECIMAL)
AS DATETIME) 'Date Part Only'
5) Get Date Part only from DateTime using DATEPART and CONVERT functions
CONVERT(VARCHAR(4),DATEPART(YEAR, #GETDATE))
+ '/'+ CONVERT(VARCHAR(2),DATEPART(MONTH, #GETDATE))
+ '/' + CONVERT(VARCHAR(2),DATEPART(DAY, #GETDATE))
'Date Part Only'
Use whichever method suits you and you find fancy.
UPDATE
As you mentioned you need to get the time part to 00:00 with date so you can try as,
SELECT CAST( convert(varchar(10),GETDATE(),112) AS DATETIME)
--This will give you 2015-02-27 00:00:00.000
SELECT DATEADD(ms,-3, DATEADD(day, DATEDIFF(day,0,GETDATE())+1,0))
--This will give you end of days time 2015-02-27 23:59:59.997
SELECT CONVERT(nvarchar,getdate(),103) + ' 12:59:59 PM'
--This will give you custom time 27/02/2015 12:59:59 PM

Difference between two date/time fields - Lotus Notes

I have three editable date/time fields which the first two is (field1 and field2), style: Calendar/time control. Both of them are showing the time: hour and minutes, eg: 15:51.
The third field also (editable) which I want to show the difference between field1 and field2.
Eg: If field1 is 14:41 and field2 is 14:30, then field3 = 00:11. I've tried field1-field2 but isn't working. The form has automatic refresh fields property. Thanks!
Your third field needs to be computed, not editable.
If it HAS to be editable for some reason, and you want it to update when the other two fields are changed, do this:
Create a new field and make it computed-for-display and hidden. Give it a formula like this
#If(field1=null | field2=null; #Return(""); "");
seconds := field1-field2;
hours := #Integer(seconds/3600);
minutes := #Modulo(#Integer(seconds/60); 60);
output := #Right("00" + #Text(hours); 2) + ":" + #Right("00" + #Text(minutes); 2);
#setfield("field3"; output);
#Command([ViewRefreshFields]);
""
Phil
I wrote this code, much easier...
Fields 'StartTime' and 'EndTime':
Type Date/Time, use Calendar/Time control, set it to only display time.
Check the property "Run Exiting/OnChange events after value changes".
The Exiting event should look like this:
Sub Exiting(Source As Field)
Call UpdateDuration()
End Sub
Field 'Duration':
Editable text field, but hidden.
Field 'dspDuration':
Computed for display text field. Value is just "Duration" (no quotes).
Then add the following code to the forms Global section:
Sub UpdateDuration()
Dim ws As New NotesUIWorkspace
Dim uidoc As NotesUIDocument
Dim starttime As NotesDateTime
Dim endtime As NotesDateTime
Dim duration As Integer
Set uidoc = ws.CurrentDocument
'*** Exit if not both times are entered
If uidoc.FieldGetText("StartTime") = "" Then
Exit Sub
Elseif uidoc.FieldGetText("StartTime") = "" Then
Exit Sub
End If
'*** Calculate duration in seconds and update field
Set starttime = New NotesDateTime( uidoc.FieldGetText("StartTime") )
Set endtime = New NotesDateTime( uidoc.FieldGetText("EndTime") )
duration = endtime.TimeDifference( starttime )
Call uidoc.FieldSetText("Duration", Cstr(duration) )
Call uidoc.Refresh()
End Sub
That's it. Easy, isn't it? If you want to modify the output (duration), you can easily do that, perhaps change it into minutes by diving it by 60.
Make sure you are getting the difference between two date time fields. If you need to, you can use the #TextToTime formula to convert text to a datetime type.
Then just subtract the first date from the second date and you'll get the difference in seconds.
Then divide that by 60 to get the difference in minutes.

Resources