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

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 check if a value in the column gets updated? [closed]

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 4 years ago.
Improve this question
I have column named HireDate in table named employeeInfo. I simply need to check whenever the value or Date in the HireDate column gets updated using simple SQL script.
I am unable to write a simple script by which I can check it.
You need to put trigger on the table. And if you want to execute your script when HireDate changed you can use "IF UPDATE(HireDate)". Check the following sample :
CREATE TRIGGER dbo.TR_employeeInfo_CheckHireDate
ON dbo.employeeInfo
AFTER UPDATE--,INSERT
AS BEGIN
SET NOCOUNT ON;
IF UPDATE (HireDate)
BEGIN
--put your Update,Delete or Insert statments here
END
END

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

SQL After Insert Trigger - Insert values from Select Statement [closed]

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 7 years ago.
Improve this question
In my database I have one table which when there is a record inserted into it a trigger will use information from the data inserted, run a select statement on the rest of the database, and insert the result into another database on the same server.
Ive never written a trigger before and Im not entirely sure that the examples out there help a lot.
Thanks in advance
its always good to start learning ...here is some sample code and some links to start ..
create trigger <<give your triggername>>
on
<<give your tablename>>
after insert
as
begin
select * from inserted--gives me data inserted
--run on rest of database--your logic goes here
select * from inserted i
join
some table tbl
on
tbl.id=i.id
insert into anotherdatabase.dbo.anotherdatabasetable
select * from inserted
end
Also please be aware that ,if the above trigger fails due to some reason ,your insert also will be rolled back..
some good links:
http://www.sqlteam.com/article/an-introduction-to-triggers-part-i
Insert Update trigger how to determine if insert or update
sql server trigger

Resources