I am making a small program and it uses WMI. There is a value which is, supposedly, a date and time. The value appears to be:
"20140221163958.621000+330".
Can anyone tell me which format is this and how to convert this to normal readable DateAndTime.
This is the official TechNet article regarding the timestamp format for WMI queries:
http://technet.microsoft.com/en-us/library/ee156576.aspx
Witch as it so happens gives a nice example in VB:
dtmInstallDate = "20140221163958.621000+330"
WMIDateStringToDate = CDate(Mid(dtmInstallDate, 5, 2) & "/" & _
Mid(dtmInstallDate, 7, 2) & "/" & Left(dtmInstallDate, 4) _
& " " & Mid (dtmInstallDate, 9, 2) & ":" & _
Mid(dtmInstallDate, 11, 2) & ":" & Mid(dtmInstallDate, _
13, 2))
' would produce "2/21/14 4:39:58 PM"
A quick search showed that it is the Standard Windows Format (UTC) (http://msdn.microsoft.com/en-us/library/aa394370(v=vs.85).aspx for more reference). To Convert it, I used a simple function.
Private Function ConvertToDateTime(ByVal in As String) As String
Return CDate(Mid(inDate, 5, 2) & "/" & _
Mid(inDate, 7, 2) & "/" & Left(inDate, 4) _
& " " & Mid(inDate, 9, 2) & ":" & _
Mid(inDate, 11, 2) & ":" & Mid(inDate, _
13, 2))
End Function
http://technet.microsoft.com/en-us/library/ee156576.aspx
you can try this way
SELECT
SUBSTRING(date, 1, 4) as year,SUBSTRING(date, 5, 2) as month,
SUBSTRING(date, 7, 2) as day,SUBSTRING(date, 9, 2) as hours,
SUBSTRING(date, 11, 2) as minutes,SUBSTRING(date, 13, 2) as seconds
FROM
table1
DEMO HERE
if you want update your table and change this column use this
Update table1 set date =
concat(SUBSTRING(date, 1, 4),' ',SUBSTRING(date, 5, 2),' ',
SUBSTRING(date, 7, 2),' ',SUBSTRING(date, 9, 2),':',
SUBSTRING(date, 11, 2),':',SUBSTRING(date, 13, 2))
demo
Related
I need to insert an IIF into the following expression in micorsoft report builder, and I keep getting an error. I want it to say if >1,1,""
=Round((Fields!percent_excellent.Value + Fields!percent_good.Value) * 100,0) & "% (" & Round(((Fields!percent_excellent.Value + Fields!percent_good.Value) - Fields!peer_group.Value) * 100,0) & "%)"
I don't know what you tried but this should work:
=IIF(Round((Fields!percent_excellent.Value + Fields!percent_good.Value) * 100,0) > 1,
100,
Round((Fields!percent_excellent.Value + Fields!percent_good.Value) * 100,0)) &
"% (" & Round(((Fields!percent_excellent.Value + Fields!percent_good.Value) - Fields!peer_group.Value) * 100,0) & "%)"
I think you'd be better using FORMATPERCENT to format your number with a set number of decimal places and adding the percent sign.
=FORMATPERCENT(
IIF(
(Fields!percent_excellent.Value + Fields!percent_good.Value) * 100 > 1,
1,
(Fields!percent_excellent.Value + Fields!percent_good.Value) * 100
)
, 0) & " ("
FORMATPERCENT(((Fields!percent_excellent.Value + Fields!percent_good.Value) - Fields!peer_group.Value) * 100, 0) & ")"
Please find my below sample table,where i have applying if condition for multiplying Numerical data.
Query Code
sql_string = "Select [Sheet5$].[num] * IIf(([Sheet5$].[to]-[Sheet5$].[frm])<365, .4," & _
"IIf(([Sheet5$].[to]-[Sheet5$].[frm])>365, 1, 0)) from [Sheet5$]"
Above code will help me out to apply the condition on one column i.e num only.But i want to apply it on multiple columns around 20-30 columns which is available in my actual data.it is possible to mention column names but it is not possible to write the above if condition to each column. please guide me on the same.
Do you mean something like this?
Public Sub test()
Dim sql_string As String
Dim sheet_name As String
sheet_name = "Sheet5$"
sql_string = "Select "
sql_string = sql_string & GetField(sheet_name, "num") & ","
sql_string = sql_string & GetField(sheet_name, "num1") & ","
sql_string = sql_string & GetField(sheet_name, "num2") & ","
sql_string = sql_string & GetField(sheet_name, "num3")
sql_string = sql_string & " from [" & sheet_name & "]"
Debug.Print sql_string
End Sub
Public Function GetField(ByVal sheet_name As String, ByVal fieldName As String)
GetField = "[" & sheet_name & "].[" & fieldName & "] * IIf(([" & sheet_name & "].[to] - [" & sheet_name & "].[frm]) < 365, 0.4, IIf(([" & sheet_name & "].[to] - [" & sheet_name & "].[frm]) > 365, 1, 0))"
End Function
Result:
Select [Sheet5$].[num] * IIf(([Sheet5$].[to] - [Sheet5$].[frm]) < 365, 0.4, IIf(([Sheet5$].[to] - [Sheet5$].[frm]) > 365, 1, 0)),
[Sheet5$].[num1] * IIf(([Sheet5$].[to] - [Sheet5$].[frm]) < 365, 0.4, IIf(([Sheet5$].[to] - [Sheet5$].[frm]) > 365, 1, 0)),
[Sheet5$].[num2] * IIf(([Sheet5$].[to] - [Sheet5$].[frm]) < 365, 0.4, IIf(([Sheet5$].[to] - [Sheet5$].[frm]) > 365, 1, 0)),
[Sheet5$].[num3] * IIf(([Sheet5$].[to] - [Sheet5$].[frm]) < 365, 0.4, IIf(([Sheet5$].[to] - [Sheet5$].[frm]) > 365, 1, 0)) from [Sheet5$]
I am trying to verify if a site selected from a cascading combobox (named cboSite) matches any of the various site names in the column "within 10" in the table named SLA. if so i want it to calculate a function i have come up with the code. The first bit of the expression is executed however if i choose a site that is not in the within 10 column in SLA then my elseif statement is not excuted.
Private Sub txtRTF_Click()
If Not ISNull (DLookup("Within10", "SLA", "Within10 = '" & Me.cboSite.Value & " '")) Then
Me.txtRTF = DateAdd("h", 2, [Date Fault Lodged])
ElseIf Not ISNull (DLookup("10_50", "SLA", "10_50 = '" & Me.cboSite.Value & " ' ")) Then
Me.txtRTF = DateAdd("h", 4, [Date Fault Lodged])
ElseIf Not ISNull (DLookup("50_80", "SLA", "50_80 = '" & Me.cboSite.Value & " ' ")) Then
Me.txtRTF = DateAdd("h", 8, [Date Fault Lodged])
ElseIf Not ISNull (DLookup("80_100", "SLA", "80_100 = '" & Me.cboSite.Value & " ' ")) Then
Me.txtRTF = DateAdd("d", 2, [Date Fault Lodged])
ElseIf Not ISNull (DLookup("Over100", "SLA", "Over100 = '" & Me.cboSite.Value & " ' ")) Then
Me.txtRTF = DateAdd("d", 10, [Date Fault Lodged])
End If
End Sub
What i am trying to achieve is very similar to when creating login access in ms access. when someone enters the username and password it checks in the table to see if the username and password are correct then allow use of forms etc.
I am not trying to check if the value is Null or empty or incorrect. I am trying to verify that the value selected is in the table SLA in the column Within10 if so execute Me.txtRTF = DateAdd("h", 2, [Date Fault Lodged])or else check in the next column in the table SLA.
Check for Null:
Private Sub txtRTF_Click()
If Not IsNull(DLookup("Within10", "SLA", "Within10 = '" & Me.cboSite.Value & "'")) Then
Me.txtRTF = "Something"
End If
End Sub
Your:
DateDiff("hm", 0, 8, [Date Fault Lodged])
doesn't make sense at all, so read up on the syntax; it is not to guess what you are trying to do with this.
Regardless of what you want to display in txtRTF, the current type mismatch error is in the IF statement: you are checking a Variant/String, which is not a Boolean. What you want is to check if the result of the lookup is empty:
If DLookup("[Within10]", "SLA", "[Within10] = '" & Me.cboSite.Value & "'") <> Empty Then
Me.txtRTF.Value = "..."
End If
I have a sub that is supposed to take a number of assignments (this number changes daily) and assign them to 7 associates. There are some conditions to this:
If the # of assignment is less than 7, it assigns all of them to a random associate.
If the # is divisible by 7, it assigns an equal number to each.
If it is not divisible by 7, it assigns equally and then gives the remainder to a random associate.
The problem is the random part. I really don't understand how random works in vba, or at least it seems like it should be super easy, but it's not (maybe). But I have this written and it's not working. (Associates(Int(Rnd() * 7) + 1)). Here is my relevant code:
Earlier in the sub I create an array of the associates and I use some dcounts to get the total assignments for that day:
Dim Associates(6) As Integer
Associates(0) = 4687 'Anita
Associates(1) = 4247 'Alberto
Associates(2) = 2167 'Jeff
Associates(3) = 4334 'Lisa
Associates(4) = 4441 'Carrie
Associates(5) = 2052 'Bobby
Associates(6) = 4657 'Simona
'
Dim Person As Variant
'
TotalPop = DCount("LNo", "qry_PT_Assign")
FractionPop = Int(TotalPop / 7)
LeftPop = TotalPop - (FractionPop * 7)
'
and then I try to actually assign them.
'Assign to Associates
If TotalPop < 7 Then
DoCmd.RunSQL "UPDATE tbl_Assignments SET AudTellerID = " & (Associates(Int(Rnd() * 7) + 1)) & " WHERE AudTellerID IS NULL"
ElseIf LeftPop = 0 Then
For Each Person In Associates
DoCmd.RunSQL "UPDATE tbl_Assignments SET AudTellerID = " & Person & " WHERE LNo IN (SELECT TOP " & FractionPop & " LNo FROM tbl_Assignments WHERE AudTellerID Is Null)"
Next
Else
For Each Person In Associates
DoCmd.RunSQL "UPDATE tbl_Assignments SET AudTellerID = " & Person & " WHERE LNo IN (SELECT TOP " & FractionPop & " LNo FROM tbl_Assignments WHERE AudTellerID Is Null)"
Next
DoCmd.RunSQL "UPDATE tbl_Assignments SET AudTellerID = " & (Associates(Int(Rnd() * 7) + 1)) & " WHERE AudTellerID IS NULL"
End If
As per my comment, try generating the random number first, assign it to a variable, and then pass the variable into Associates().
Dim rndInt as Integer
rndInt = Int(Rnd() * 7) + 1
Associates(rndInt)
Then as #Chips said, you can use
Debug.print rndInt
Or
Msgbox rndInt
to check its value
That way you'll be able to see what number is actually being generated
The trick is to use a negative seed that changes constantly.
So add a time dependant seed to Rnd which changes for every unique id like in this sample select query:
SELECT
Table1.ID,
Table1.SomeField,
Table1.AnotherField,
Rnd(-Timer()*[ID]) AS RandomIndex
FROM
Table1
ORDER BY
Rnd(-Timer()*[ID]);
In your code, the expression could be something like this:
.. " & (Associates(Int(Rnd(-Timer()*" & [ID] & ") * 7) + 1)) & " ..
Is it possible to convert date 4 digit year 2 digit month and 2 digit day to dd (3 digit month) 4 digit year?
Right now I have the input of date "use date" as user entered YYYYMMDD. I prefer to use the calendar input as it keeps the date consistent
A Date/Time value is actually a double precision float number.
So you can take a number, and use CDate to represent it as a date.
? CDate(41668.0)
1/29/2014
The display format of the date value is a separate issue. The same numeric date value can be displayed in whatever format you prefer.
? Format(CDate(41668.0), "yyyymmdd")
20140129
? Format(CDate(41668.0), "dd mmm yyyy")
29 Jan 2014
But the actual date value (the number) is unchanged --- that number doesn't get "converted" regardless of how it's displayed.
If your issue is that the users are working with a text value instead of a Date/Time value, you either have to convert that text to a valid Date/Time value or modify your application so they enter Date/Time values instead of text.
The second alternative is less fuss. But if you're stuck with dates as text, you can do something like this ...
use_date = "20140129"
' transform it to a string CDate can accept ...
? Left(use_date, 4) & "-" & Mid(use_date, 5, 2) & "-" & Right(use_date, 2)
2014-01-29
' get the date from that string ...
? CDate(Left(use_date, 4) & "-" & Mid(use_date, 5, 2) & "-" & Right(use_date, 2))
1/29/2014
' finally make it a string in your desired format ...
? Format(CDate(Left(use_date, 4) & "-" & Mid(use_date, 5, 2) & "-" & Right(use_date, 2)), "dd mmm yyyy")
29 Jan 2014
try
DIM DateStr : DateStr = "20140119" 'Your date
Response.Write "DEBUG: DateStr = " & DateStr & "<br>"
'Split number so can use Date functions
DIM NewDate : NewDate = DateSerial(CInt(Mid(DateStr, 1, 4)), CInt(Mid(DateStr, 5, 2)), Mid(DateStr, 7, 2))
Response.Write "DEBUG: NewDate = " & NewDate & "<br>"
TheDate=CDate(NewDate)
Response.Write "DEBUG: CDate(NewDate) = " & TheDate & "<br>"
DIM FinalDate: FinalDate = DatePart("d", TheDate) & " "
FinalDate = FinalDate & MonthName(Month(TheDate),1) & " "
FinalDate = FinalDate & DatePart("yyyy", TheDate)
Response.write "DEBUG: Required Date = " & FinalDate