Current date being returned multiple times? - sql-server

I've got a simple query which works as expected the first time.
However, when I re-run it, it returns the same result 504 times, instead of once.
Does anyone know why this would happen?
SELECT GETDATE(), CAST (GETDATE() AS DATE) AS Casted_Date
from Production.Product

The count of rows based on your count of rows in Production.Product table.
This query is not valid anymore to validate since you are casting Date datatype as Date again.

Related

How to get the following output using SQL SERVER 2008

I need to implement a logic where I need to get the latest group data from a table .
Here is the example.
Result Date
PASS 6/1/2016
PASS 6/2/2016
FAIL 6/10/2016
FAIL 6/11/2016
PASS 6/20/2016
PASS 6/21/2016
FAIL 7/1/2016
FAIL 7/5/2016
PASS 7/10/2016
PASS 7/11/2016
Required output
ID Result Date
PASS 7/10/2016
PASS 7/11/2016
Can you please help me?
SELECT
ID,
RESULT,
MAX(Date) as Date
FROM Table
GROUP BY ID, RESULT
The solution returns a single row for each ID and RESULT combination as noted by the GROUP BY clause. The row that is returned is the one which has the MAX date, aka the most current date even if it's in the future, with this pairing or grouping.
SELECT * FROM *table_name* WHERE ID IN (9, 10)

SQL-Server Datetime in WHERE Clause Issue

enter image description hereI am creating a query of two values, a period number and the corresponding date. The periods are accounting periods that are similar but not identical to our months. The dates are largely correct, but there are a few inconsistencies that I thought I would clean up with a WHERE statement.
For example, the date 4/1/2015 belongs in period 3, but it shows up in both period 3 and period 4. To fix this, I thought that I would create a WHERE statement stating:
SELECT DISTINCT table1.period,
table1.datetime
FROM table1
WHERE table1.period <> 4 AND table1.datetime <> '4/1/2015'
This took away all of period 4 away instead of just the one I needed. I have also tried the datetime syntax:
AND table1.datetime <> '20150401 00:00:00.000'
Both syntaxes had the same results. I am fairly inexperienced at SQL and have always hated dealing with datetime values, so any help would be great.
-EDIT- Forgot to add single quotations to the first datetime WHERE clause.
Try following:
SELECT DISTINCT table1.period,
table1.datetime
FROM table1
WHERE CASE WHEN table1.period = 4 AND CONVERT(VARCHAR(8),table1.datetime,112) = '20150401' THEN 1 ELSE 0 END = 0
This only get rid of rows that both has period=4 and datetime=20150401, while your query first get rid of anything that has period=4(no matter what [datetime] is), then get rid of anything has datetime=20150401.
Did you say that period is a string field ? you mean a varchar ?
Also you want to check on the date part only, not the time ?
then you can try this
SELECT DISTINCT table1.period,
table1.datetime
FROM table1
WHERE table1.period <> '4'
AND convert(date, table1.datetime) <> '20150401'

Cast select column to date in view to then run query against

I have a view where I Select about 100 rows to allow users to easily query data. In this data, I have a field that is sometimes a date and sometimes text. A date or text depends on type. I cast to a date value like so.
SELECT Cast(Value as Date) as column
from Table
Where type = 1
When you then try to run a query against this column, you get a Conversion failed when converting date and/or time from character string. Here is the query.
SELECT Column
From View
WHERE Column BETWEEN '01/01/2015' AND '12/31/2015'
I have another field that is a date and if I replace it in this query, the query works. Likewise the data from the whole table will load. Any ideas are appreciated. Thanks
Basically, I need this Query to work and not give me the error described above.
select cast(value as date)
from Value
where type = 1
and cast(value as date) between '01/01/2015' and '12/31/2015'
My guess is that you have entries in that column that cannot be converted to datetime. You may be able to find them by running
Select * from table where isdate(value) = 0

How to use stored procedure to calculate DateTime difference in hours?

I have two tables, TicketReport and TimeTracker.
TicketReport Columns:
Ticket_Number
Report_DT (DateTime ticket was reported)
Response_Time (Hourly value. How long it took for this ticket to be responded to. This is where I need the place the hours. DateTime Reported minus DateTime started)
TimeTracker Column:
Ticket_Number
Time_Start( DateTime that work started on this ticket)
Right now, for every row in my TicketReport table, the Response_Time column contains either Null or just a test value of 1(hour).
*I need to calculate how many hours it took for a ticket to be responded to (ReportDT - Time_Start), and then insert that hourly value into the Report_DT column for each row in the TicketReport table.
I did some research and found DATEDIFF, but I think this only returns days, and even if it did return hours, im not sure how to use it.
How could I accomplish this in a stored procedure?
To update the response_time column with the number of hours you can use a simple update query (which you could wrap in a stored proc if you need.
The query could look like this:
-- uncomment the next line to create a sp...
-- create proc update_response_time as
update tr
set response_time = datediff(hour, report_dt, time_start)
from TicketReport tr
join TimeTracker tt on tr.Ticket_Number = tt.Ticket_Number
If you want to run it without updating (to see what the values would be) you can run it as a select query:
select *, datediff(hour, report_dt, time_start) as diff_in_hours
from TicketReport tr
join TimeTracker tt on tr.Ticket_Number = tt.Ticket_Number
Be aware that these queries assume that there are just one matching row in the TimeTracker table for each ticket. If there can be multiple rows you would need another solution. Also know that the hour value doesn't take minutes into account at all so if report_dt is at 12:00 and time_start is at 12:30 it would be reported as 0 hours, so maybe a finer granularity than hours would be more suitable.
You can use DATEDIFF function
Syntax
DATEDIFF ( datepart , startdate , enddate )
pass datepart hh to get hours

Performance issue with DATEADD function

This is my actual select query,
SELECT b.CaseNumber as CaseNumber,b.DebtorNr ,b.ActionDate,DATEADD(MONTH,-12,b.ActionDate) one_month,a.Registerdate --COALESCE(count(A.historynr),0) as DebtorActivity
from rr..r_basic_info b
join rr..activities_VW as A on b.DebtorNr=a.Debtornr
where
B.Debtornr = A.Debtornr
--and a.Registerdate<=b.ActionDate --this condition works
and a.registerdate >= DATEADD(month,-12,getdate()) --i have a problem with this condition and causing huge time consumption
I have a view defined here is activities_VW
select H.NR as historynr,o.debtornr as Debtornr, O.NR as ordernr, h.Actmenunr as Actmenunr,h.AGREEMENT as AgreementCode, h.Registerdate as Registerdate
from abc..history h join abc..orders o on o.NR=h.ORDERNR
and my execution plan is like
One more information for all rows b.actionDate column has identical value like '2015-04-11 08:37:44.037'.
I have checked with all date format but nothing wrong found.
For another case, I have different value for different rows in b.actionDate column and it is working fine for that case.
Thanks
I may be wrong in my understanding so take it only as a possibility - when using a function within a join criteria and/or where clause, in order to determine if data meets the criteria, it must be checked against every row in the table.
Think about your first part WHERE e.DATE <= a.joining_date - you can simply look directly at rows that are less than the e.DATE.
For your second part AND e.DATE >= DATEADD(MONTH, - 6, a.joining_date) - there is no column the is "joining date minus 6 months", so to determine if e.Date is greater than it, you would need to perform that calculation on every instance of a.joining_date in the table.
Remember that where clause information is not necessarily evaluated in the order is it written down in the query - so the rows you would think are eliminated by the first part of your where are not necessarily eliminated by it. So as one of the comments suggested, using a computed/persisted column on the DATEADD(MONTH, - 6, a.joining_date) would probably work well.
This is my actual select query,
SELECT b.CaseNumber as CaseNumber,b.DebtorNr ,b.ActionDate,DATEADD(MONTH,-12,b.ActionDate) one_month,a.Registerdate --COALESCE(count(A.historynr),0) as DebtorActivity
from rr..r_basic_info b
join rr..activities_VW as A on b.DebtorNr=a.Debtornr
where
B.Debtornr = A.Debtornr
--and a.Registerdate<=b.ActionDate --this condition works
and a.registerdate >= DATEADD(month,-12,getdate()) --i have a problem with this condition and causing huge time consumption
I have a view defined here is activities_VW
select H.NR as historynr,o.debtornr as Debtornr, O.NR as ordernr, h.Actmenunr as Actmenunr,h.AGREEMENT as AgreementCode, h.Registerdate as Registerdate
from abc..history h join abc..orders o on o.NR=h.ORDERNR
and my execution plan is like
One more information for all rows b.actionDate column has identical value like '2015-04-11 08:37:44.037'.
I have checked with all date format but nothing wrong found.
For another case, I have different value for different rows in b.actionDate column and it is working fine for that case.
Thanks

Resources