December 10, 2021 at 8:25 pm
I am trying, and this is just a start I've made, to cast and add hh:mm:ss time to yyyy-mm-dd hh:mm:ss format. I realize I may have to declare some variables to get this to work and I'm sure I'll have to explain this more especially since 'DATEADD([created_at] + [event_sec])' needs much work:
INSERT INTO [portman].[dbo].[AgentHours]
([DOMAIN]
,[LOGIN TIMESTAMP]
,[LOGIN TIME]
,[LOGOUT TIMESTAMP]
)
SELECT
'EVQ' --literal
,cast([created_at] as datetime) --yyyy-mm-dd hh:mm:ss format
,cast([event_sec] as time) --hh:mm:ss format
,DATEADD([created_at] + [event_sec]) --yyyy-mm-dd hh:mm:ss format
FROM [Portman].[dbo].[ConvoHrs]
December 10, 2021 at 8:57 pm
The way I would do it is similar to this:
DECLARE @test1 DATE = GETDATE()
DECLARE @test2 TIME = GETDATE()
SELECT @test1, @test2, CAST(CAST(@test1 AS VARCHAR(255)) +' '+ CAST(@test2 AS VARCHAR(255)) AS DATETIME2)
If you need it as DATETIME instead of DATETIME2, you can cast that DATETIME2 value as a DATETIME. You can't cast it immediately to DATETIME due to TIME having too much precision.
I imagine there are better ways; but this method does work. With your above example, you would cast Created_at to a DATE then follow the above example.
The above is all just my opinion on what you should do.
As with all advice you find on a random internet forum - you shouldn't blindly follow it. Always test on a test server to see if there is negative side effects before making changes to live!
I recommend you NEVER run "random code" you found online on any system you care about UNLESS you understand and can verify the code OR you don't care if the code trashes your system.
December 10, 2021 at 9:00 pm
You shouldn't need to declare any variables if you're pulling it out of a table, you'll just need to do some dateadds/string manipulation.
DECLARE @timestamp varchar(30) = '2021-11-01 12:32:18'
DECLARE @time varchar(30) = '03:05:12'
SELECT DATEADD(hour, CAST(LEFT(@time, 2) AS int), CAST(@timestamp AS datetime))
December 10, 2021 at 9:44 pm
I think we may be on the right track but my data isn't presenting correctly. I'm getting (although the seconds are not visible, they are incorrect):
Logout Timestamp needs to be Login Timestamp with Login Time added.
INSERT INTO [portman].[dbo].[AgentHours]
([DOMAIN]
,[DATE]
,[LOGIN TIMESTAMP]
,[LOGIN TIME]
,[LOGOUT TIMESTAMP]
)
SELECT
'EVQ' --literal
,cast([created_at] as datetime)
,cast([created_at] as datetime)
,cast([event_sec] as time)
,DATEADD(hour, CAST(LEFT([event_sec], 2) AS int), CAST([created_at] AS datetime))
FROM [portman].[dbo].[ConvoHrs]
December 11, 2021 at 7:32 am
Can you provide DDL and sample data for dbo.ConvoHrs, rather than asking people to use guesswork?
The absence of evidence is not evidence of absence
- Martin Rees
The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
- Phil Parkin
December 11, 2021 at 4:59 pm
How is event_sec stored in the database? Is it stored as an actual time - or is it stored as a duration? If stored as a duration - then all you need to do is add the duration to get the logout timestamp, and add the duration to midnight and cast as time:
Select 'EVQ'
, event_date = cast(created_at as date)
, [login timestamp] = created_at --should already be datetime, no need to cast
, [logout timestamp] = dateadd(second, event_sec, created_at) -- just add the seconds
, [login time] = cast(dateadd(second, event_sec, '00:00:00') as time)
FROM [portman].[dbo].[ConvoHrs]
If event_sec is stored as a string - then you need to convert it to a duration, then you can add that duration to created_at to arrive at the logout timestamp:
Select 'EVQ'
, event_date = cast(created_at as date)
, [login timestamp] = created_at --should already be datetime, no need to cast
, [logout timestamp] = dateadd(second, datediff(second, '00:00:00', cast(event_sec as time), created_at) -- convert to seconds, then add
, [login time] = cast(event_sec as time)
FROM [portman].[dbo].[ConvoHrs]
If event_sec is already stored as a time - then you can eliminate the cast to time.
It all depends on the actual data types in the source table - so please help us help you and provide that information.
Jeffrey Williams
“We are all faced with a series of great opportunities brilliantly disguised as impossible situations.”
― Charles R. Swindoll
How to post questions to get better answers faster
Managing Transaction Logs
December 11, 2021 at 9:52 pm
I think we may be on the right track but my data isn't presenting correctly. I'm getting (although the seconds are not visible, they are incorrect):
Logout Timestamp needs to be Login Timestamp with Login Time added.
INSERT INTO [portman].[dbo].[AgentHours]
([DOMAIN]
,[DATE]
,[LOGIN TIMESTAMP]
,[LOGIN TIME]
,[LOGOUT TIMESTAMP]
)
SELECT
'EVQ' --literal
,cast([created_at] as datetime)
,cast([created_at] as datetime)
,cast([event_sec] as time)
,DATEADD(hour, CAST(LEFT([event_sec], 2) AS int), CAST([created_at] AS datetime))
FROM [portman].[dbo].[ConvoHrs]
We've been through this before. Post the data in a readily consumable format and remember that the article at the first link in my signature line below demonstrates one of many ways to do such a thing. It'll help us help you instead of post a graphics where, in your own words, the seconds are missing.
--Jeff Moden
Change is inevitable... Change for the better is not.
December 13, 2021 at 2:09 pm
I think we may be on the right track but my data isn't presenting correctly. I'm getting (although the seconds are not visible, they are incorrect):
Logout Timestamp needs to be Login Timestamp with Login Time added.
INSERT INTO [portman].[dbo].[AgentHours]
([DOMAIN]
,[DATE]
,[LOGIN TIMESTAMP]
,[LOGIN TIME]
,[LOGOUT TIMESTAMP]
)
SELECT
'EVQ' --literal
,cast([created_at] as datetime)
,cast([created_at] as datetime)
,cast([event_sec] as time)
,DATEADD(hour, CAST(LEFT([event_sec], 2) AS int), CAST([created_at] AS datetime))
FROM [portman].[dbo].[ConvoHrs]
I agree with Jeff here - we need to know the source datatypes. That being said, I would HOPE that you are storing the "created_at" and "event_sec" as DATETIME and TIME respectively.
That being said, I think that Jeffrey Williams's answer should work for you. I see no reason why that solution won't work anyways.
The above is all just my opinion on what you should do.
As with all advice you find on a random internet forum - you shouldn't blindly follow it. Always test on a test server to see if there is negative side effects before making changes to live!
I recommend you NEVER run "random code" you found online on any system you care about UNLESS you understand and can verify the code OR you don't care if the code trashes your system.
December 13, 2021 at 5:53 pm
-- here are the basic DDLs for my two tables:
CREATE TABLE [portman].[dbo].[AgentHours](
[LoginEntryID] [int] IDENTITY(1,1) NOT NULL,
[DOMAIN] [varchar](4) NOT NULL,
[DATE] [date] NOT NULL,
[LOGIN TIMESTAMP] [datetime2](0) NULL,
[LOGOUT TIMESTAMP] [datetime2](0) NULL,
[LOGIN TIME] [time](0) NULL
CREATE TABLE [portman].[dbo].[ConvoHrs](
[created_at] [varchar](200) NULL,
[event_sec] [varchar](200) NULL
December 13, 2021 at 6:22 pm
On the table ConvoHrs - why are both columns defined as varchar(200)? If you can have anything put into those columns - then that is what will happen and you will then need some validation code to verify the format of the data matches.
Much easier to assign the appropriate data types.
Since event_sec is a string - what is the format of that string? Is it HH:MM:SS - or something else? If it is HH:MM:SS - you do realize that you are now limited to 24 hours, unless you write additional code to parse the values.
Jeffrey Williams
“We are all faced with a series of great opportunities brilliantly disguised as impossible situations.”
― Charles R. Swindoll
How to post questions to get better answers faster
Managing Transaction Logs
December 13, 2021 at 6:28 pm
-- here are the basic DDLs for my two tables:
CREATE TABLE [portman].[dbo].[AgentHours](
[LoginEntryID] [int] IDENTITY(1,1) NOT NULL,
[DOMAIN] [varchar](4) NOT NULL,
[DATE] [date] NOT NULL,
[LOGIN TIMESTAMP] [datetime2](0) NULL,
[LOGOUT TIMESTAMP] [datetime2](0) NULL,
[LOGIN TIME] [time](0) NULL
CREATE TABLE [portman].[dbo].[ConvoHrs](
[created_at] [varchar](200) NULL,
[event_sec] [varchar](200) NULL
That helps but you forgot the "readily consumable data".
Again, please read and heed the article at the first link in my signature line below. It'll really help you get high quality answers.
--Jeff Moden
Change is inevitable... Change for the better is not.
December 13, 2021 at 6:29 pm
The ConvoHrs table is coming from a vendor we have no control over.
December 13, 2021 at 6:30 pm
What format would you prefer the data?
December 13, 2021 at 6:39 pm
We have links in our signatures that show how to do that, but simply put:
Insert Into someTable (col1, col2)
Values (val1, val2), (val3, val4), (val5, val6), ...
Jeffrey Williams
“We are all faced with a series of great opportunities brilliantly disguised as impossible situations.”
― Charles R. Swindoll
How to post questions to get better answers faster
Managing Transaction Logs
December 13, 2021 at 6:56 pm
What format would you prefer the data?
READ THE ARTICLE AND YOU'LL KNOW AS LEAST ONE WAY!
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 15 posts - 1 through 15 (of 17 total)
You must be logged in to reply to this topic. Login to reply