March 5, 2008 at 9:43 am
Hello,
My question is how I can get rid of the time in date filed. This query is scheduled to run every night and push the data to the Backlog_Daily table
INSERT INTO m2mqueries..Backlog_Daily
SELECT GETDATE() AS DataDate, SUM(sorels.funetprice *
(soitem.fquantity - (sorels.fshipbook + sorels.fshipbuy + sorels.fshipmake))) AS BackOrderTotal
FROM sorels INNER JOIN soitem
ON sorels.fsono = soitem.fsono AND sorels.finumber = soitem.finumber
INNER JOIN somast
ON soitem.fsono = somast.fsono
WHERE (sorels.forderqty > sorels.fshipbook + sorels.fshipbuy + sorels.fshipmake)
AND (somast.fstatus = 'Open')
This is the result and everyday I have to run the query and delete the time to be able to update my report. I need to add statement to my query to clean the date then insert it to the table
DataDate BackOrderTotal
3/2/2008 10.00
3/3/2008 16.45
3/3/2008 8:00:01Pm 12.91
March 5, 2008 at 9:55 am
Keeping in mind that datetime fields always store both date and time components no matter what (at least until you get to SQL Server 2008 with its new data types) - it sounds like you're looking to zero out the time values.
I would try something like this:
update MyTable
set DataDate=dateadd(day,datediff(day,0,DateDiff),0)
----------------------------------------------------------------------------------
Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?
March 5, 2008 at 10:28 am
You can make a function and use it in your insert statement as below. This way you will avaoid the update statement.
CREATE function [DateOnly](@DateTime DateTime)
-- Returns the DateTime minus the time portion
returns datetime
as
begin
return dateadd(dd, datediff(dd,0,@DateTime), 0)
end
INSERT INTO m2mqueries..Backlog_Daily
SELECT dbo.DateOnly(GETDATE()) AS DataDate, SUM(sorels.funetprice *
(soitem.fquantity - (sorels.fshipbook + sorels.fshipbuy + sorels.fshipmake))) AS BackOrderTotal
FROM sorels INNER JOIN soitem
ON sorels.fsono = soitem.fsono AND sorels.finumber = soitem.finumber
INNER JOIN somast
ON soitem.fsono = somast.fsono
WHERE (sorels.forderqty > sorels.fshipbook + sorels.fshipbuy + sorels.fshipmake)
AND (somast.fstatus = 'Open')
-----------------------------------------------------------[font=Arial Black]Time Is Money[/font][font=Arial Narrow]Calculating the Number of Business Hours Passed since a Point of Time[/url][/font][font=Arial Narrow]Calculating the Number of Business Hours Passed Between Two Points of Time[/font]
March 5, 2008 at 11:20 am
Or just use Matt's solution in the SELECT...
SELECT dateadd(day,datediff(day,0,getdate()),0) AS DataDate, ....
If it was easy, everybody would be doing it!;)
March 5, 2008 at 11:20 am
There's usually some merit and some future need for knowing the times. I think it's a huge loss of data to update datetimes that have the time to all having a 0 time. My recommendation is to add a caclulated column that strips the time. It requires absolutely no maintanence and, properly constructed, can be indexed.
--Jeff Moden
Change is inevitable... Change for the better is not.
March 5, 2008 at 11:34 am
Or if you just need the date only for a report, use this in the SELECT query in your report...
select convert(varchar(10), DataDate, 101)...
...and leave the data as is.
If it was easy, everybody would be doing it!;)
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply