Convert MS Access data query into SQL Server database query [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 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.

Related

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'

SQL Server Integer to Time [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 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.

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

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)

Error with select case statement [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
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. :)

Resources