Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I'm trying to figure out how I can create a temp table in sql server that will create a number of rows based on a number.
So I have a query that results the number 35 and I want to create a temp table with 35 rows with the ID values 1 - 35.
declare #i int
set #i = 1
while (#i <= 35)
begin
insert tmp (id) values (#i)
set #i = #i + 1
end
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 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 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
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 does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to select the following from my table:
Hello James, your address is 1 Some Road.
using a query like this:
SELECT ('Hello ' + name + ', your address is ' + address) AS result FROM Users
The Users table has two varchar(50) fields called name and address. Is this possible using a simple, single line of T-SQL as above? When I run the query above it returns -1 records affected.
It depends which RDMBS you are using, syntax may be different. Here are some examples:
MySQL: SELECT CONCAT('Hello ',name) FROM USER
Oracle: SELECT 'Hello ' || name FROM USER
SQL Server: SELECT 'Hello' + name FROM USER
SOLVED
Apologies for the bad example - the data types I was working with are in fact int and that was why the concatenation wasn't working - they needed to be CAST first (thanks #bluefeet)
So this worked:
SELECT ('Hello ' + CAST(name AS varchar) + ', your address is ' + CAST(address AS varchar)) AS result FROM Users
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. :)