SQL Server Integer to Time [closed] - sql-server

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.

Related

I want to concat or add date from one column with time from another and result should be in datatime [closed]

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 months ago.
Improve this question
select convert(datetime, FORMAT(Convert(datetime, '2022-08-27 01:00:00.000'),'yyyy:MM:dd')+' '+
FORMAT(Convert(datetime, '2022-08-27 12:01:30.000'),'hh:mm:ss'))
error:
Msg 241, Level 16, State 1, Line 9
Conversion failed when converting date and/or time from character string.
Just convert one side to date and the other to time, which will truncate them. Then convert them back to datetime (to avoid conversion problems) and add them together.
For example:
DECLARE #d1 datetime = '2022-08-27 01:00:00.000';
DECLARE #d2 datetime = '2022-08-27 12:01:30.000';
SELECT
CONVERT(datetime, CONVERT(date, #d1)) + CONVERT(datetime, CONVERT(time, #d2));
db<>fiddle

Query returns wrong row [closed]

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'

How to convert Number into String in Stored Procedure [closed]

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

Convert MS Access data query into SQL Server database query [closed]

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
I found a MS Access select query and in where clause I found some issue to understand
I want to convert into a SQL Server query please help.
(((TABLE1.COL1) > #12/31/2011#) AND
((TABLE2.LstUpdate) = DateValue(DFirst("RDate", "TABEL3"))))
I want to convert this MS Access select query into an equivalent T-SQL query
T-SQL equivalent for #12/31/2011# will be '20111231'.
T-SQL equivalent of DFirst("RDate","TABEL3") will be select top 1 RDate from TABEL3
DateValue is function converting string to datetime, if your RDate type is date or datetime then you don't need that conversion, if RDate is varchar or other string type - you can use convert function.

converting two column result to one column result in sql [closed]

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
I have stored procedure like this:
ALTER procedure [dbo].[Test] #locid int as begin
declare #Mns decimal , #dec decimal
select #dec= AVG( CONVERT(NUMERIC(18,2), DATEDIFF(MI,t.Paydate,t.DelDate) )) FROM Transaction_tbl t WHERE Locid=#locid;
select #Mns=#dec%60;
select HH=convert(decimal(10) ,#dec/60), mm=#Mns;
end
My out put is:
HH | mm
|
29 6 my out put is returning in two column,,i want to get out put in one column,
My expected out put like this:
HH:mm
29:6 how i can do this
Try something like this:
select HH=convert(varchar(10),(convert(decimal(10) ,#dec/60)))+':' +convert(varchar,#Mns)

Resources