Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
SQL Server : I need to find difference between two times in the format of HH:MM and both columns having time datatype:
SELECT DATEDIFF(MINUTE, sh.ToTime, sh.FromTime)
FROM ShiftTimes sh
INNER JOIN StdBreaksInShifts sb ON sb.ShiftId = sh.ShiftId
Table:
Fromdate todate
----------------------------------------
08:15:00.0000000 16:30:00.0000000
Can anyone suggest how to do this?
You can use CONVERT with style to get part of the corresponding format. Something like this:
DECLARE #FromTime TIME = '08:15:00.0000000'
,#ToTime TIME = '16:30:00.0000000';
SELECT #FromTime
,#ToTime
,CONVERT(VARCHAR(5), DATEADD(minute, DATEDIFF(minute, #FromTime, #ToTime), 0), 114)
08:15:00.0000000 16:30:00.0000000 08:15
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 2 years ago.
Improve this question
I have a query like this:
SELECT
[tblticket].[TicketID],
[Tblcustomers].[CustomerAccNo],
[tblticket].[TicketDate], [tblticket].[Collectdate],
[tblticket].[TicketTotal], [tblticket].[UserAccNo],
[Tblcustomers].[FirstName], [Tblcustomers].[Surname]
FROM
TbLTicket
INNER JOIN
Tblcustomers ON TblTicket.[CustomerAccNo] = Tblcustomers.[CustomerAccNo]
WHERE
TicketDate BETWEEN '13/07/2020' and '24/07/2020'
When I run this query between the following dates as seen in the query, I get results with ticket dates that are 13/09/2019 included, please what am I doing wrong.
Please why are 13/09/2019 rows being included?
why are 13/09/2019 rows being included?
You are doing string comparisons while you want date comparison.
If TicketDate is of a date-like datatype (as it should be) then consider using an unambiguous date format for the literals:
TicketDate between '20200713' and '20200724'
Else you need to cast it first. SQL Server is quite good at interpreting date formats, so this might be sufficient:
cast(TicketDate as date) between '20200713' and '20200724'
The last resort is to rebuild the date with datefromparts(). Assuming string format DD/MM/YYYY:
datefromparts(
substring(TicketDate, 7, 4),
substring(TicketDate, 4, 2),
substring(TicketDate, 1, 2)
) between '20200713' and '20200724'
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 years ago.
Improve this question
I came into a scenario where table containing the time values in integer datatype and I need to convert them to TIME datatype to use DATEDIFF() function.
Ex: if column value is 449, then it's 04:49:00:00; if the column contains 25, then 02:05:00:00.
If anyone has a user-defined procedure or code that should be more helpful to use them in Select statement as I need to pass this value to DATEDIFF().
TRY THIS!!!!!
CREATE FUNCTION dbo.udfConv(
#quantity INT
)
RETURNS VARCHAR(25)
AS
BEGIN
RETURN (SELECT CAST(CASE WHEN LEN(#quantity)=2 THEN '0'+LEFT(#quantity,1) +':'+'0'+RIGHT(#quantity,1)+':00:00'
WHEN LEN(#quantity)=3 THEN '0'+LEFT(#quantity,1) +':'+RIGHT(#quantity,2)+':00:00'
WHEN LEN(#quantity)=4 THEN LEFT(#quantity,2) +':'+RIGHT(#quantity,2)+':00:00'
END AS TIME))
END;
THIS IS NOT EXACT THE DATETIME FORMAT. DATETIME FORMAT IS SOMETHING LIKE DD-MM-YYYY HH:MM: SS. HOWEVER, THIS QUESTION NEEDS MORE IMPROVEMENT.
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 6 years ago.
Improve this question
I have numbers and i want to convert those number into string in the stored procedure.
Please reply.
Thanks !
Without knowing what database you're using, we cannot give an exact syntax. Here's the syntax for MSSQL:
CAST(column AS VARCHAR(10))
Or
CONVERT(VARCHAR(10), column)
A conversion in T-SQL can be done with the CAST function.
A sample can be:
SELECT CAST(myNumber AS VARCHAR) FROM myTable
where myNumber is your numeric field, and myTable the table in which the field is present. A cast can be used as a parameter for joins too.
Another function you can use is STR:
SELECT STR(myField) FROM myTable
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 6 years ago.
Improve this question
I am getting below error.
Conversion failed when converting date and/or time from character string.
when making the query:
Select guid,
CONVERT(VARCHAR(50),DATEADD(day,30,U73_SCANDATE)) as scan
from emp;
U73_SCANDATE data type is varchar, and its like
26-NOV-08 01.00.00.000000000 PM
How can I convert this to datetime and use dateadd properly?
You got to use some weird stuff to get this as datetime:
DECLARE #U73_SCANDATE varchar(50) = '26-NOV-08 01.00.00.000000000 PM'
SELECT CAST(
--This part replaces - with spaces and add 20 to year part
STUFF(REPLACE(LEFT(#U73_SCANDATE,9),'-',' '),8,0,'20') +
--Here we change . to : in a time part
REPLACE(SUBSTRING(#U73_SCANDATE,10,10),'.',':') +
--Take first 3 digits of miliseconds
LEFT(RIGHT(#U73_SCANDATE,12),3)+
--And PM/AM part
RIGHT(#U73_SCANDATE,2)
as datetime)
Output:
2008-11-26 13:00:00.000
EDIT
To use directly on your table:
SELECT guid,
CAST(
--This part replaces - with spaces and add 20 to year part
STUFF(REPLACE(LEFT(U73_SCANDATE,9),'-',' '),8,0,'20') +
--Here we change . to : in a time part
REPLACE(SUBSTRING(U73_SCANDATE,10,10),'.',':') +
--Take first 3 digits of miliseconds
LEFT(RIGHT(U73_SCANDATE,12),3)+
--And PM/AM part
RIGHT(U73_SCANDATE,2)
as datetime) as scan
from emp;
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 8 years ago.
Improve this question
How to convert this T-SQL query into Oracle?
if((select case
when (select top 1 AlertMessage
from ims.dbo.alertlist
where SystemID=#meter
order by TimePeriod desc
) like '%NO_COMMUNICATION_Resolved' then 1
else 0 end)=1)
begin
INSERT INTO [ims].[dbo].[alertlist]
([SiteID],[ThresholdNumber],[SystemID],
[AlertMessage],[TimePeriod],[AlertType],[PollID])
VALUES
(1,#thresnumber,#meter,#message,getdate(),1,0)
end
As pointed out by Dan Puzey, your question is too broad and it would require a lot of trial and error. However, I'll try to put you on the right track with something that might solve the first part of your problem.
DECLARE v_IsMessageResolved int;
-- This query will retrieve the value of the first row returned by the sub-query, mimicking the TOP 1 of SQL Server, and it will store the value of MessageResolved in a Variable
SELECT
MessageResolved into v_IsMessageResolved
FROM
(
-- This query will return 1 for all the Messages that match the LIKE clause, ordered by TimePeriod in descending order.
SELECT
CASE
WHEN AlertMessage LIKE '%NO_COMMUNICATION_Resolved' THEN 1
ELSE 0
END AS MessageResolved
,RANK() OVER (ORDER BY TimePeriod DESC) AS MessageRank
FROM
ims.alertlist
WHERE
(SystemID = :meter)
)
WHERE
(MessageRank = 1)
-- At this point, it will be a matter of checking the value of v_IsMessageResolved and, if it's "1", run the INSERT
Please note that I know SQL Server very well, but I never used Oracle, therefore my solution might not be perfect (or even run at all, as I don't have an environment to test it). This also means that you can find the answers to the remaining questions you might have with a simple search, as I did. :)