Access Report - Calculating Hours between 2 times - ms-access

I have a SQL time off database with a Access front end. I currently have BeginTimeOff and EndTimeOff fields on the report. In my SQL database, these are Time(7) fields. I want a new field to show the time difference. I've tried to have the Control Source be equal to:
=DateDiff("n",CDate([BeginTimeOff]),CDate([EndTimeOff]))
AND
=DateDiff("n",[BeginTimeOff],[EndTimeOff])
AND
= [EndTimeOff] - [BeginTimeOff]
I can't get anything to work. I can subtract dates fine, just not times. Help!

Access does not have a time-only field type (Access Date/Time fields have both a date and time component), and any unknown field types in an ODBC linked table are usually mapped to Text. So if you have a SQL Server table with time(7) columns ...
CREATE TABLE [dbo].[TimeTest](
[Id] [int] NOT NULL,
[BeginTimeOff] [time](7) NULL,
[EndTimeOff] [time](7) NULL
...
then the corresponding ODBC linked table in Access will have Text(255) columns instead:
If you want to directly use the columns in the linked table then you will have to convert the values into a form that Access will accept before you can use functions like DateDiff() to do calculations with them. Specifically, Access Date/Time values do not support fractional seconds so you will have to remove them. That is,
CDate("07:59:00.0000000")
will fail with a "Type mismatch" error (run-time error 13), while
CDate("07:59:00")
works fine. You can use string manipulation functions like InStr(), Left(), Mid(), etc. to get rid of the fractional part of the string.
Another approach would be to create a SQL Server View that converts the DATE(7) columns to DATETIME
CREATE VIEW [dbo].[TimeView]
AS
SELECT
Id,
DATEADD(day, -2, CONVERT(DATETIME, BeginTimeOff)) AS BeginTimeOff,
DATEADD(day, -2, CONVERT(DATETIME, EndTimeOff)) AS EndTimeOff
FROM dbo.TimeTest
and then if you link to that View the columns will appear as Date/Time values in Access

Related

Grafana dashboard using MySQL: how should I deal with timestamp of "%Y%m%d%H%i%s" to use as time column of panel?

I want to visualize my MySQL DB table to an hourly basis graph using Grafana dashboard. The table that I'm working with has the attributes below, with unused ones including PK not mentioned:
SERVER_NAME varchar(250)
STAT_TYPE int(11)
STAT_DTM varchar(14)
CPU_MAX_USAGE int(11)
MEMORY_MAX_USAGE int(11)
What matters is STAT_DTM. Its format is "%Y%m%d%H%i%s", e.g. "20210621090000"; for 09:00:00 of June 21st 2021. I want this to be the X axis of the graph. Grafana guide says:
return column named time or time_sec (in UTC), as a unix time stamp or
any sql native date data type. You can use the macros below.
So I put unix_timestamp(date_format(str_to_date(substr(stat_dtm, 1, 10),'%Y%m%d%H'), '%Y-%m-%d %H:00:00')) but an error saying db query error: query failed - please inspect Grafana server log for details popped up.
select
unix_timestamp(date_format(str_to_date(substr(stat_dtm, 1, 10),'%Y%m%d%H'), '%Y-%m-%d %H:00:00')) as 'time',
CPU_MAX_USAGE,
MEMORY_MAX_USAGE
from lcop.tb_stat_os_day
where stat_type = 60 and server_name = 'LDFSWAS1'
The Panel I'm currently working on
The result of the query above
How can I set the timestamp correctly and show the graph? The table schema cannot be modified unfortunately and I can give any additional info if needed. Thanks in advance.
Let's simplify your type conversion there:
SELECT '20210621090000' as `src`,
UNIX_TIMESTAMP(STR_TO_DATE('20210621090000', '%Y%m%d%H%i%s')) as `dts`
The STR_TO_DATE() function can be given the full format, which can then be given to UNIX_TIMESTAMP. There is no need to make things more difficult with SUBSTR() or DATE_FORMAT() 👍🏻

SQL Query nvarchar to date

I am working on SAP HANA Studio and have tried to run SQL command that converts an entire column of field, nvarchar, into one of field, date.
My dates have format: dd-mon-yyyy (i.e '29-Mar-1997') with field nvarchar(11).
I have looked at previous questions and SQL command documentation (for functions like CAST, CONVERT, TO_DATE, STR_TO_DATE) and have not gotten a solution.
Typical errors I get are: Function not recognized, or, Error while parsing Service Date as DATE at function to_date().
Any suggestions?
Thanks
-Diana
Try TO_DATE():
select to_date(col, 'DD-MON-YYYY')
Obviously your database driver/layer in SAP HANA does not support all mySQL functions.
Please connect to your database directly (using command-line or a gui like HeidiSQL) and create a view in your database:
CREATE VIEW view_tablename AS
SELECT STR_TO_DATE(`Service Date`, '%d-%b-%Y') AS ServiceDateDt, * FROM tablename
Then use view_tablename instead of tablename in all your queries - because view_tablename has the additional date field "ServiceDateDt".

Table valued parameters for SSRS 2008

We have a requirement of generating SSRS reports from where we need to convert multi-valued string and integer parameters to datatable and pass it to stored procedure. The stored procedure contains multiple table type parameters. Earlier we used varchar(8000) but it was also crossing the datatype limit. Then we thought to introducing datatable concept. But we were not aware of how to pass values from SSRS.
We found a solution from GruffCode on Using Table-Valued Parameters With SQL Server Reporting Services.
The solution solved my problem, and we're able to generate reports. However, sometimes SSRS returns the two following errors:
An error has occurred during report processing.
Query execution failed for dataset 'DSOutput'.
String or binary data would be truncated. The statement has been terminated.
And
An unexpected error occurred in Report Processing.
Exception of type 'System.OutOfMemoryException' was thrown.
I'm not sure when and where it's causing the issue.
The approach outlined in that blog post relies on building an enormous string in memory in order to load all of the selected parameter values into the table-valued parameter instance. If you are selecting a very large number of values to pass into the query I could see it potentially causing the 'System.OutOfMemoryException' while trying to build the string containing the insert statements that will load the parameter.
As for the 'string or binary data would be truncated' error that sounds like it's originating within the query or stored procedure that the report is using to gather its data. Without seeing what that t-sql looks like I couldn't say why that's happening, but I'd guess that it's also somehow related to selecting a very large number of parameter values.
Unfortunately I'm not sure that there's a workaround for this, other than trying to see if you could figure out a way to select fewer parameter values. Here's a couple of rough ideas:
If you have a situation where users might select a handful of parameter values or all parameter values then you could have the query simply take a very simple boolean value indicating that all values were selected rather than making the report send all of the values in through a parameter.
You could also consider "zooming out" of your parameter values a bit and grouping them together somehow if they lend themselves to that. That way users would be selecting from a smaller number of parameter values that represent a group of the individual values all rolled up.
I'm not a fan of using a Text parameter and EXEC in the SQL statement like the article you referenced describes as doing so is subject to SQL injection. The default SSRS behavior with a Multi-value parameter substitutes a comma-separated list of the values directly in place of the parameter when the query is sent to the SQL server. That works great for simple IN queries, but can be undesirable elsewhere. This behavior can be bypassed by setting the Parameter Value on the DataSet to an expression of =Join(Parameters!CustomerIDs.Value, ", "). Once you have done that you can get a table variable loaded by using the following SQL:
DECLARE #CustomerIDsTable TABLE (CustomerID int NOT NULL PRIMARY KEY)
INSERT INTO #CustomerIDsTable (CustomerID)
SELECT DISTINCT TextNodes.Node.value(N'.', N'int') AS CustomerID
FROM (
SELECT CONVERT(XML, N'<A>' + COALESCE(N'<e>' + REPLACE(#CustomerIDs, N',', N'</e><e>') + N'</e>', '') + N'</A>') AS pNode
) AS xmlDocs
CROSS APPLY pNode.nodes(N'/A/e') AS TextNodes(Node)
-- Do whatever with the resulting table variable, i.e.,
EXEC rpt_CustomerTransactionSummary #StartDate, #EndDate, #CustomerIDsTable
If using text instead of integers then a couple of lines get changed like so:
DECLARE #CustomerIDsTable TABLE (CustomerID nvarchar(MAX) NOT NULL PRIMARY KEY)
INSERT INTO #CustomerIDsTable (CustomerID)
SELECT DISTINCT TextNodes.Node.value(N'.', N'nvarchar(MAX)') AS CustomerID
FROM (
SELECT CONVERT(XML, N'<A>' + COALESCE(N'<e>' + REPLACE(#CustomerIDs, N',', N'</e><e>') + N'</e>', '') + N'</A>') AS pNode
) AS xmlDocs
CROSS APPLY pNode.nodes(N'/A/e') AS TextNodes(Node)
-- Do whatever with the resulting table variable, i.e.,
EXEC rpt_CustomerTransactionSummary #StartDate, #EndDate, #CustomerIDsTable
This approach also works well for handling user-entered strings of comma-separated items.

inserting into userdefined data in sql server

Hi I am using SQL server 8.0 for my database. I dont know how to insert user define data.
This is my table.
column name: data type length allow_nulls
study_type_id int 4
study_type_name UD_NAME(varchar) 150
study_type_abbrev UD_NAME_SHORT(varchar) 50
order int
UD_NAME and UD_NAME_SHORT are user defined data types in sql enterprise manager. base type is varchar.
when i used insert command as below,
INSERT into study_type VALUES (15, 'test', 'TT',100)
It gives me "Implicit_conversion_error" I could not see ASP webpage link to that table.
And
INSERT into study_type (study_type_id, study_type_name, study_type_abbrev, order)
VALUES (15,CAST('test' as UD_NAME),CAST('TT' as UD_NAME_SHORT),100)
Then it said "type UD_NAME is not defined system type."
You cannot use user defined types in CAST and CONVERT functions

DST to UTC append query creates a type conversion error in access

I have a table I'm using as a source for an append query that calls upon a table query, which calls upon a union query to effectively adjust the eastern prevailing time to spring forward and fall back while converting to utc. there are only three fields in the table but I keep getting "access did not import .... due to type conversion". Please Help Me out!!! Thank you in advance
below is the access sql:
{append query}
INSERT INTO somePrice ( price )
SELECT DTQuery.Price
FROM DTQuery
WHERE (((DTQuery.EPT)<>[2ndsunday]));
{DTQuery}
SELECT
TransposeQuery.Field3 AS [Zone]
, DateSerial(Left([field1],4),Left(Right([field1],4),2),Right([field1],2))+[TransposeQuery]![Hour]/24 AS EPT, Val([Field8]) AS Price
, DateValue(DateSerial(Year([EPT]),3,14))-(Weekday(DateValue(DateSerial(Year([EPT]),3,14)),1)-1)+3/24 AS 2ndSunday
, DateValue(DateSerial(Year([EPT]),11,7))-(Weekday(DateValue(DateSerial(Year([EPT]),11,7)),1)-1)+3/24 AS 1stSunday
FROM TransposeQuery
ORDER BY
TransposeQuery.Field3
, DateSerial(Left([field1],4),Left(Right([field1],4),2),Right([field1],2))+[TransposeQuery]![Hour]/24, Val([Field8]);
First some general stuff: If you want to convert between timezones, which involves adding or subtracting a number of hours you might want to use to DateAdd function.
DateAdd("h", -2, [SourceDateTime])
This simply takes two hours off the SourceDateTime field.
Also rather than using the Left(Right( combination you can use Mid(string, start, length)
Mid("1234567890", 2, 4)
Returns 2345, but if you are dealing with dates just use DatePart
DatePart("h", "17/12/2011 08:10")
Returns 8.
As for the type conversion error, it's hard to say as you haven't given us the types of the fields in the destination table.
If you run the append query without the first line this will rule out the destination table. If it still fails then it might be in the where clause, so move the fields into the results set and make sure they are the same type. If it still fails then it must be in the source query so check DTQuery opens without any problems.