DB2 Stored procedure - Add hours as input to a timestamp - datetime

I want to create a stored procedure that has a IN_NROFHOURS as input.
Then I want to create a cursor that fetches all rows with the following condition:
select * from listtable where startdatetime + in_nrofhours > current timestamp
My questions are:
What is the best datatype for IN_NROFHOURS;
Given the datatype you suggest, how should the "where" condition be stated?
Thanks for your help, I appreciate that.

Related

Need help in teradata

In one of the tables I've a time column where the data is something like this :
01:21:00.000000
for all the records.
I want to retrieve the data which looks like below.
01:21:00 in teradata.
Please advise. I'm new to teradata and don't know how to achieve this
I don't know a reasonable way to go to less precision in time (or timetamps) in Teradata. Assuming your column is time(6), you can't just cast that to time(0). You get a DateTime field overflow error.
I do it by casting it to a character field and then back to time(0):
select *
from
<your table>
where
cast(cast <your column> as varchar(8)) as time(0) = '01:21:00'
If your column is timestamp, then we can use Timestamp(0) is YYYY-MM-DDbHH:MI:SS and Timestamp(6) is YYYY-MM-DDbHH:MI:SS.ssssss (milliseconds extra).
If your column is just time, then we can use 'CAST'.
select cast( as time(0)) from ;

how to select records matching current date and hour of timestamp column in oracle

I am new to oracle DB and writing sql queries. I have timestamp column in Table. I need to select the records which matches to current date and hour ignoring rest of timestamp value.
i found a way with trunc function as below which is giving me the records matching criteria
select * from TABLE where trunc(COLUMN_NAME,'HH') = trunc(systimestamp,'HH')
I came to know that above query wont use indexes if any, on that coulmn. pls guide me with efficient query. Thanks in advance.

Calculating the percentage of dates (SQL Server)

I'm trying to add an auto-calculated field in SQL Server 2012 Express, that stores the % of project completion, by calculating the date difference by using:
ALTER TABLE dbo.projects
ADD PercentageCompleted AS (select COUNT(*) FROM projects WHERE project_finish > project_start) * 100 / COUNT(*)
But I am getting this error:
Msg 1046, Level 15, State 1, Line 2
Subqueries are not allowed in this context. Only scalar expressions are allowed.
What am I doing wrong?
Even if it would be possible (it isn't), it is anyway not something you would want to have as a caculated column:
it will be the same value in each row
the entire table would need to be updated after every insert/update
You should consider doing this in a stored procedure or a user defined function instead.Or even better in the business logic of your application,
I don't think you can do that. You could write a trigger to figure it out or do it as part of an update statement.
Are you storing "percentageCompleted" as a duplicated column value in the same table as your project data?
If this is the case, I would not recommend this, because it would duplicate the data.
If you don't care about duplicate data, try something separating the steps out like this:
ALTER TABLE dbo.projects
ADD PercentageCompleted decimal(2,2) --You could also store it as a varchar or char
declare #percentageVariable decimal(2,2)
select #percentageVariable = (select count(*) from projects where Project_finish > project_start) / (select count(*) from projects) -- need to get ratio by completed/total
update projects
set PercentageCompleted = #percentageVariable
this will give you a decimal value in that table, then you can format it on select if you desire to % + PercentageCompleted * 100

Compare time only in sql database (asp.net)

i wanna compare time and show records that are greater than the spcified time. For example in my database I have a field "TimeDate" where I store date and time together. Now i want to show records that are only greater than lets say this time "19/04/2012 2:34:27 PM". Is there anyway I can achieve this.
I have started with this code.
Select * from Table where TimeDate > '19/04/2012 2:34:27 PM'.
First of all the datatype is datetime in sql,
your sample code is right.
I am sure that sql is intelligent enough, for your current query.

How to format date and time values for SQLite?

Can't believe I have to ask this here. Even googling didn't help.
I want to do this:
insert into atable (adatefield,atimefield,adatetimefield) values (A,B,C);
adatefield is defined as DATE
atimefield is defined as TIME
adatetimefield is defined as DATETIME
What do I put for A, B and C?
It's nice that it's free but the documentation for SQLite is awful.
A date, time and datetime field will actually all store datetime, it just depends on what you insert into it, and they will all work with date strings.
You can
insert into atable values("2010-11-16","14:12:22","2010-11-16 14:12:22");
or you can actually just insert "2010-11-16 14:12:22" into all the fields and then select them back with:
select date(adatefield), time(atimefield), datetime(adatetimefield) from atable;
If however you want to make sure that the fields only contain exactly a date,time or datetime and you're inserting from variables, then you can
insert into atable values(date(thedate),time(thedate),datetime(thedate));
Sqlites typelessness makes some things really easy, but can confuse or complicate some other things.

Resources