|
Hi,
I am using sql server 2005.
When I am finding difference between two date I am getting one day less
For example
select datediff(d , '2008-09-01' , '2008-09-05')
I am getting 4 days I want it to be five day as 1,2,3,4,5 that is it should start from date to todate
How can I do it
Please help
Thank you
imran khan
|
|
|
|
|
Add 1 to the result.
DateDiff gives you the interval between the two dates.
|
|
|
|
|
imranafsari wrote: select datediff(d , '2008-09-01' , '2008-09-05')
5 - 1 = 4
Seems ok to me...add 1 to get your desired result.
Please remember to rate helpful or unhelpful answers, it lets us and people reading the forums know if our answers are any good.
|
|
|
|
|
select datediff(d , '2008-09-01' , '2008-09-05')+1
I Love T-SQL
"Don't torture yourself,let the life to do it for you."
If my post helps you kindly save my time by voting my post.
|
|
|
|
|
let it be
nelsonpaixao@yahoo.com.br
trying to help & get help
|
|
|
|
|
Hello Experts,
I have my data like below,
ID Fname Date_TimeWorked Status
1 MyName 2006-10-25 10:00:05 Ready
1 MYName 2006-10-25 11:00:05 Completed
1 MyName 2006-10-25 10:30:00
I want to know the time difference Between Ready-completed, ready-canceled.
How do I query this? Its urgent. Thanks in advance!!
|
|
|
|
|
For example:
SELECT DATEDIFF(minute, start.Date_TimeWorked, end.Date_TimeWorked) AS ElapsedMinutes
FROM TableName start,
TableName end
WHERE start.ID = end.ID
AND start.Status = 'Ready'
AND end.Status = 'Completed'
Mika
|
|
|
|
|
I have this in one table, what is TableName start,TableName end?
Thank you
|
|
|
|
|
Since I didn't know the actual name for your table I used only TableName. Let's say your table is named WorkData then the query would look like:
...
FROM WorkData start,
WorkData end
...
Start and end are aliases for the same table so that it can be referred twice. I used start alias (the alias can be whatever you want) for the row that defines the starting point for time and respectively end for ending point.
Hope this helps,
Mika
|
|
|
|
|
I just created a simple access database for it and tried, but it is prompting me Minute.. Entere Parameter value.
ID Fname Date_TimeWorked Status
1 MyName 2006-10-25 10:00:05 Ready
1 MYName 2006-10-25 11:00:05 Completed
1 MyName 2006-10-25 10:30:00 Cancled
|
|
|
|
|
The code was for SQL Server which most of the people here use. It's been ages since I last used Access, but try just subtracting the two times (end.Date_TimeWorked - start.Date_TimeWorked ). I'm not sure but it may give you the result you want.
|
|
|
|
|
Well I was playing with Excel sheets to database, so I was trying something on access database. and thank you for your reply, the query looks like it works.. but I got the idea which i was looking for, thanks 
|
|
|
|
|
You're welcome 
|
|
|
|
|
Hello Mika, I need your help on this query, you suggested me
SELECT DATEDIFF(minute, start.Date_TimeWorked, end.Date_TimeWorked) AS ElapsedMinutes
FROM TableName start,
TableName end
WHERE start.ID = end.ID
AND start.Status = 'Ready'
AND end.Status = 'Completed'
Ready-Complete ok, but I want one more condition added to it, I need
time between Date_Timeworked when status is Ready and Rejected?
|
|
|
|
|
If you want them on separate rows, you can use UNION and duplicate the query (just modify Status for end condition)
If you want them on the same row it would be something like (I'll change the aliases so that this becomes more clear):
SELECT DATEDIFF(minute, readyRow.Date_TimeWorked, completedRow.Date_TimeWorked) AS FromReadyToCompleted,
DATEDIFF(minute, readyRow.Date_TimeWorked, rejectedRow.Date_TimeWorked) AS FromReadyToRejected
FROM TableName readyRow
LEFT OUTER JOIN
TableName completedRow
ON readyRow.ID = completedRow.ID
LEFT OUTER JOIN
TableName rejectedRow
ON readyRow.ID = rejectedRow.ID
WHERE readyRow.Status = 'Ready'
AND completedRow.Status = 'Completed'
AND rejectedRow.Status = 'Rejected'
The query is now modified to outer joins since I believe that the same ID cannot be both completed and rejected at the same time.
The example may contain typos, but you'll get the idea
Hope this helps,
Mika
|
|
|
|
|
Hi!
Would it be possible to detect if your SQL database is opened and some actions (DELTE | INSERT| UPDATE) performed in the database. I want to detect it with pure SQL statements and a pre-defined trigger function could initiate an autoresponder which could tell me the IP address of the intruder?
Thanks
What a curious mind needs to discover knowledge is noting else than a pin-hole.
|
|
|
|
|
How would you know if the access is an intruder ? The whole idea of having a database is that select,insert,update and delete would be occurring all the time. 
|
|
|
|
|
Your are partially right about it but what if a user (not an authenticated one)tries to perform such action in the database?
Presumable it does mean that for an intruder: He has allready comprimised the server and captured the password and login ID of my database. What if SQL server is still up an running? Wouldn't it be possible to trigger one procedure within SQL database?
I am not an expert about SQL server. You may find the question quite absurd.
Thanks.
What a curious mind needs to discover knowledge is noting else than a pin-hole.
|
|
|
|
|
JUNEYT wrote: He has allready comprimised the server and captured the password and login ID of my database
So he would be able to start sql server anyway. If you have been compromised to this extent you are already, as they say round here, "up sh*t creek without a paddle"
Bob
Ashfield Consultants Ltd
|
|
|
|
|
I know how to set up a maintenance plan and all but only if I'm gonna backup the database on the "same" machine. Now I need to specify another machine to backup the database. The other machine also has sql server. How can I accomplish that? Please, Add step to the answer.
Thanks
|
|
|
|
|
I think this is a limitation of SQL Server, I know we use SQL Backup from Red-Gate to do this as well as compression. Bloody useful toolbox.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
When defining a backup, use a backup device which points to a network share. Preferrably use UNC naming. For more info: Backup Devices[^]
Hope this helps,
Mika
|
|
|
|
|
I didn't think you could point to a network share when backing up SQL Server - thank heavens I'm not employed as a DBA
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
i have a table with repeated values like this in block_name field:
DUBAI
DUBAI
MANAMA
DUBAI
MANAMA
BEIRUT
CAIRO
CASABLANCA
MANAMA
BEIRUT
DUBAI
how can i delete all reepeated values and just keep one value for each using SQL statement?
|
|
|
|
|
Do you have any primary key or other unique column or any date column which can be used for ordering on that table.
|
|
|
|