Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 months ago.
Improve this question
I am practicing on a database on SQL server where the date column in the table is Nvarchar(255) thereby presenting the dates as five digit numbers eg 40542,40046 etc. How do I change the dates in the column to actual dates?
The articles I checked only helped me to change rows but I can't change all of them individually as that will take time.
If dates from Excel, this is to expand on my comment.
Here are two approaches
Example
Declare #YourTable Table ([SomeCol] nvarchar(255)) Insert Into #YourTable Values
('40542')
,('40043')
Select *
,AsDate1 = try_convert(datetime,try_convert(float,SomeCol)-2.0)
,AsDate2 = dateadd(day,try_convert(int,SomeCol),'1899-12-30') -- Note Start Date
From #YourTable
Results
SomeCol AsDate1 AsDate2
40542 2010-12-30 2010-12-30
40043 2009-08-18 2009-08-18
You have a strange database. Basically, you want
UPDATE TableName
SET NewDateColName = ConvertionFunction(OldDateColName)
But more info is needed on how to convert the 5 digit number encoded as a string, into a date.
CAST(OldDateColName as Date)
Might work, or you may have to apply some arithmetics after casting to int.
For excel dates:
UPDATE TableName
SET NewDateColName = CONVERT(numeric(18,4),OldDateColName,101)
The values of your "dates" suggests that your dates reflect an offset in days since the epoch (zero point on the calendar system) of 1 January 1900.
For instance, the values you cited are:
40452: Sunday, October 3, 2010
40046: Sunday, August 23, 2009
In which case you should be able to say something like
create view dbo.my_improved_view
as
select t.* ,
some_date = date_add( day,
convert( int , t.funky_date_column ),
convert( date , '1900-01-01' )
)
from dbo.table_with_funky_date_column t
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 10 months ago.
Improve this question
I have a table with a date and value for example:
AccID Date Value
1 01/01/2007 10
1 01/02/2008 20
1 01/03/2009 40
I want to create a new table that starts from the date in row 1 and ends with the date in row 2 and so on.....for example
AccID Date DateEnd Value
1 01/01/2007 01/02/2008 10
1 01/02/2008 01/03/2009 20
1 01/03/2009 01/04/2050 40
Select
date,
isnull(lead(date) over (partition by accID order by date), '01/04/2050') as DateEnd,
value
from column A
I have tried this code but I can't seem to get the correct output. This is the output I am currently getting
AccID Date DateEnd Value
1 01/02/2008 01/02/2009 20
1 01/02/2009 01/03/2007 40
1 01/01/2007 01/04/2050 10
You are not getting any output, you are getting an error
Incorrect syntax near 'partition'.
You get the correct results when you correct the error
Select
date,
isnull(lead(date) over (partition by accID order by date), '01/04/2050') as DateEnd,
value
from column A
Edit: Do yourself some favours and apply the following:
Avoid the use of reserved words or if you "must" use them, surround the column or table name with [ ] e.g. [date], [value],[column]
Research the function you're going to use to make the most of what it has to offer, to simplify your queries e.g. See the documentaion for Lead
Use date formats that are unambiguous e.g. '2050-04-01' in preference to '01/04/2050'. The latter could be either 1st April or 4th January depending on where in the world you happen to be
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
How do I calculate time these two blocks Start and Submit?
The Activity column has Start and Submit. I need to calculate the time difference between Submit and the Start before the Submit Activity. How do we calculate that?
Thanks!
Here is the sample Table:
Sample Table
I took the liberty of converting your image to a usable table:
DECLARE #Activity TABLE (Activity VARCHAR(MAX), Timestamp DATETIME)
INSERT #Activity
VALUES
('Start', '5/12/21 3:30 PM'),
('Start', '5/13/21 3:31 PM'),
('Start', '5/14/21 3:33 PM'),
('Submit', '5/15/21 3:36 PM'),
('Start', '5/14/21 3:37 PM')
Without any other identifier to associate the start and submit times, I will assume that the most recent Start applies to a Submit and that other Start entries are ignored. For any given Submit, one way to select a candidate Start is with a CROSS APPLY and appropriate selection, ordering, and TOP 1 criteria. Something like:
SELECT
StartTime = ST.TimeStamp,
SubmitTime = SU.TimeStamp,
Elapsed = SU.TimeStamp - ST.TimeStamp,
ElapsedMinutes = DATEDIFF(minute, ST.TimeStamp, SU.TimeStamp)
FROM #Activity SU
CROSS APPLY (
SELECT TOP 1 *
FROM #Activity ST
WHERE ST.Activity = 'Start'
AND ST.TimeStamp <= SU.TimeStamp
ORDER BY ST.TimeStamp DESC
) ST
WHERE SU.Activity = 'Submit'
Note the last record of your data is the actual closest match using the above criteria, yielding 1439 minutes of elapsed time. If your table contains keys that better associate your start and submit times, you should adjust the above logic accordingly.
I would also recommend an index on TimeStamp for efficient lookup.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I need to build a SQL query in which I can get time spent on multiple statuses (onHold,Waiting for customer,Resolved,Closed), so basically I do not want to include time spent on this statues and table looks like as below
So I need a query in which I can get actual time spent on ticket or time spent on status which I have mentioned so far I have tried below solutions and tried Cross APPLY but seems all did not help me as expected.
Tried below query so far and that gives me correct time spent on first status on-hold not after that:
SELECT t1.TICKETNUMBER,SUM(DATEDIFF(MINUTE,TICKETTIME,CloseTime)) as TotalMinutes
FROM [Admin].[TbtrnTicketHistory] t1
CROSS APPLY(SELECT TOP 1 TICKETTIME as CloseTime FROM [Admin].[TbtrnTicketHistory] t2 WHERE t1.TICKETNUMBER = t2.TICKETNUMBER and t2.TICKETHISTORYID > t1.TICKETHISTORYID ORDER BY t2.TICKETTIME) as t2
WHERE t1.CURRENTSTATUS_ANALYST not in('On-Hold','Waiting For Customer','Resolved','Closed') and t1.ticketnumber = '211135'
GROUP BY t1.TICKETNUMBER;
calculate difference between two times in two rows in sql
Calculate Time Difference Between Two Consecutive Rows
with SQL Server you can use those very usefull windowed functions LEAD and FIRST_VALUE :
select *
,[duration(sec)] = DATEDIFF(SECOND
,ticketTime
,LEAD(ticketTime,1,ticketTime)over(partition by ticketNumber order by ticketTime)
)
,[cumulative duration(sec)] = DATEDIFF( SECOND
, FIRST_VALUE(ticketTime)over(partition by ticketNumber order by ticketTime)
, ticketTime)
from (values
(1,cast('20211101 10:00:01' as datetime))
,(1,'20211101 10:00:33')
,(1,'20211101 10:01:59')
)T(ticketNumber,ticketTime)
ticketNumber
ticketTime
duration(sec)
cumulative duration(sec)
1
2021-11-01 10:00:01.000
32
0
1
2021-11-01 10:00:33.000
86
32
1
2021-11-01 10:01:59.000
0
118
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I am trying to view a column by adding 5 hours 30 min to a date time column using SQL Server using dateadd but it is not working
dateadd(hour, 5, dateadd(minute, 30, visit_start_time))
visit_start_time is a datetime column
Instead add 330 minutes (equivalent of 5 hours thirty minutes) as below:
Dateadd(minute,330,visit_start_time)
Or you can do it as you were trying:
Dateadd(hour,5, Dateadd(minute,30,visit_start_time))
Actually I Just realized your query should work in sql server. Which version of sql server you are using and what's not working?
There is multiple ways to do this:
Using time literal
DECLARE #table table(a datetime)
insert into #table values ('2020-02-02T10:00:00.000')
SELECT a, a + CAST('05:30:00' AS DATETIME) FROM #table
a
updateddate
2020-02-02 10:00:00.000
2020-02-02 15:30:00.000
Using DATEADD
DECLARE #table table(a datetime)
insert into #table values ('2020-02-02T10:00:00.000')
SELECT DATEADD(minute,30,DATEADD(hour,5,a)) from #table
SELECT DATEADD(minute,330,a) from #table
You can do the following:
DATEADD(n,330,visit_start_time)
n (the first parameter) is a shorthand for minute; 330 minutes = 5 hours (*60) + 30 minutes.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
In my table, I have a field RAZ (bit) whose value is 0 except at 5am, 1pm and 9pm when it's equal to 1. I would like to get the last rows in my table before RAZ is equal to 1.
Here is a sample data :
For exemple, the final request would display the row with idEvenement = 8454.
I will use this request in a stored procedure to compute indicators.
The RAZ field changes to 1 automatically, it means that the data has been reset.
I don't know if it's possible and how to do it.
You can use SQL Server windowing function LEAD to see if the next record's RAZ turns to 1 while it was 0 before. LEAD gets the next record based on RAZTime order. This returns ID = 8454 as the result
; WITH cte (ID, RAZTime, RAZ) AS (
SELECT 8456, convert(datetime, '13:01 pm'), 1
union SELECT 8455, convert(datetime, '13:00 pm'), 1
union SELECT 8454, convert(datetime, '12:59 pm'), 0
union SELECT 8453, convert(datetime, '12:58 pm'), 0
)
, q AS (
SELECT
*
, NextRAZ = LEAD (RAZ, 1, NULL) OVER (ORDER BY RAZTime)
FROM cte
)
SELECT *
FROM q
WHERE
RAZ = 0
AND NextRAZ = 1