Wednesday, March 7, 2012
datetime bug?
select convert (datetime, '2004-04-05 3:01:01:002')
--result: '2004-04-05 3:01:01:003'
select dateadd (ms, 1, '2004-04-05 3:01:01:001')
--result: '2004-04-05 3:01:01:000'
currently I am using version 8.00.818
anyone know how to fix this? thanks a million!That's cute. My version is the 859 on Personal and 818 on Enterprise and Standard, and I get this while just doing the CONVERT part of your code:
2004-04-05 03:00:00.910 2004-04-05 03:00:00.910
2004-04-05 03:00:00.911 2004-04-05 03:00:00.910
2004-04-05 03:00:00.912 2004-04-05 03:00:00.913
2004-04-05 03:00:00.913 2004-04-05 03:00:00.913
2004-04-05 03:00:00.914 2004-04-05 03:00:00.913
2004-04-05 03:00:00.915 2004-04-05 03:00:00.917
2004-04-05 03:00:00.916 2004-04-05 03:00:00.917
2004-04-05 03:00:00.917 2004-04-05 03:00:00.917
2004-04-05 03:00:00.918 2004-04-05 03:00:00.917
2004-04-05 03:00:00.919 2004-04-05 03:00:00.920|||i just found this on ms site:
http://support.microsoft.com/default.aspx?scid=kb;en-us;281676&Product=sql2k#appliesto
well, they say it's not a bug, it's 'designed' that way... bs...|||However, this produces the right result:
if cast('2004-04-05 03:00:00.918' as datetime) = dateadd(ms, 1, '2004-04-05 03:00:00.917')
print 'EQ'
else
print 'NEQ'
So I think it's a display formatting issue, not precision bug.|||it's a precision bug, try this:
select datediff (ms, '2004-04-05 3:01:01:005', '2004-04-05 3:01:01:008')|||they are equal because they are both incorrect...
hehe, two wrong answers don't make it right :P|||Man, this is baaaaaaaaaaaaaaaaaaaaaaad...
declare @.d1 datetime, @.d2 datetime
select @.d1='2004-04-05 3:01:01.004', @.d2='2004-04-05 3:01:01.008'
select @.d1, @.d2, datediff(ms, @.d1, @.d2)
if datediff (ms, @.d1, @.d2) = 4
print 'bamboo7 is wrong'
else
print 'bamboo7 is right'|||OK, reported this to MS, see what comes out of it...That's been bugging me since bamboo7's last post...|||You do know that ms are only recorded in .003 increments....
It can't go
.001
.002
.003
It's
.003
.006
.009
EDIT: BOL
datetime and smalldatetime
datetime
Date and time data from January 1, 1753, through December 31, 9999, with an accuracy of three-hundredths of a second, or 3.33 milliseconds.|||Oh man, why did I even bother!!!! Should have read BOL before starting this mess...|||That's just bad maths, you shouldn't display decimal places that represent more accuracy than you have provided.|||That's just bad maths, you shouldn't display decimal places that represent more accuracy than you have provided.Huh? I am not trying to display with more accuracy that I have provided (boy, that's a mouthfull)...And where do you see bad math here? As BOL says, - it's 0.003|||I don't mean you, I mean SQL Server. It is displaying to 1000ths of a second when it only supports to a 300th.
I was told that you should only show the number of decimal places that your result is accurate too. Under this logic, Microsoft should only provide 2 decimal places, not 3. Though I suppose this logic doesn't apply so neatly to timing precision.|||Oh, got it, you're right. It would have made much more sense to drop the 1000's precision and just let us live with 0.99
Friday, February 24, 2012
Dates
I have a problem with the DateAdd function
lets say the project is going to end in 35 days
the project is working day is 6 days a week
the report need to give a date in 35 days from now
using 6 days of working days or 5 or 7 working days ?
well 7 days is easy but 5 or 6 or 4 or 3 i have problem with
It sounds like you are trying to add 35 work days to todays date. You can't use the DateAdd function alone. You will need custom code to determine the actual number of days to add. Does your non-work day change (ex. Sunday one week, Sat. the next)? Is it always 6 days or is it based on a special calendar using holidays?|||the work days can change and holiday will be nice to have but it's not require.i have problem with the custom code to change the workdays around to find out when the project will finish|||How are you tracking how many work days there are in a particular week? Are you storing the information anywhere?|||the information is enter in the website.
I have a field in the column of the table
the project manger will enter how many days per week they will be working for the week
or rest of the project life. the days per week doesn't change too much
but it does happen.
DatePart function in ANSI SQL
How can I re-write the following code in ANSI SQL code:
select cast(datepart(month, dateadd(second, time_stamp, '1/1/1970 00:00:00')) as varchar) + '/' +
cast(datepart(day, dateadd(second, time_stamp, '1/1/1970 00:00:00')) as varchar) + '/'+ cast(datepart(year, dateadd(second, time_stamp, '1/1/1970 00:00:00')) as varchar), event_instance_id, max(time_stamp)
from usmuser.usm_sli_event_data
where event_instance_id=10019
group by cast(datepart(month, dateadd(second, time_stamp, '1/1/1970 00:00:00')) as varchar) + '/' +
cast(datepart(day, dateadd(second, time_stamp, '1/1/1970 00:00:00')) as varchar) + '/'+
cast(datepart(year, dateadd(second, time_stamp, '1/1/1970 00:00:00')) as varchar),
event_instance_id
order by event_instance_id
Thanks for your help!
-Parulcould you please explain why?
also, for those of us not patient enough to unravel the intricacies of this delectable code fragment, would you kindly please explain what it's doing|||The code should be portable so it can used be used on other databases as well, not just SQL Server.
Basically, the time_stamp field has number of seconds since 1/1/1970 and the datepart function is calculating the month, day, and year. The goal is to get the last time_stamp per event_instance_id per day.|||unfortunately, your quest will be unsatisfied
date functions are among the more un-robust of the ANSI SQL capabilities
there is practically no hope that you will get exactly the same code to run "on other databases as well, not just SQL Server"
even if we did manage to figure out a way to do what you're doing with ANSI SQL functions (and good luck to you, as i'm going to pass), it probably wouldn't run on SQL Server to start with|||This is what data abstraction layers are for...|||Thanks r937!
datepart datatype
can I in any chance pass a parameter?
(and not use the constants week, month etc.)
SELECT DateAdd(@.my_dt , 1 , CONVERT (DATETIME, '01/01/01')) As 'Date'
I think you would need dynamic SQL, e.g.
EXEC('SELECT DateAdd(' + @.my_dt + ', 1, ...') AS [Date];
Aaron Bertrand
SQL Server MVP
http://www.sqlblog.com/
http://www.aspfaq.com/5006
<retima@.gmail.com> wrote in message
news:1174495911.706163.321400@.e65g2000hsc.googlegr oups.com...
> On a paramater of datepart (like the first one in DateAdd),
> can I in any chance pass a parameter?
> (and not use the constants week, month etc.)
> SELECT DateAdd(@.my_dt , 1 , CONVERT (DATETIME, '01/01/01')) As 'Date'
>
|||You could use brute force.
DECLARE @.d datetime
SET @.d = GETDATE()
DECLARE @.x int
SET @.x = 7
SELECT CASE @.x
WHEN 1 THEN DATEPART(year, @.d)
WHEN 2 THEN DATEPART(quarter, @.d)
WHEN 3 THEN DATEPART(month, @.d)
WHEN 4 THEN DATEPART(dayofyear, @.d)
WHEN 5 THEN DATEPART(day, @.d)
WHEN 6 THEN DATEPART(week, @.d)
WHEN 7 THEN DATEPART(weekday, @.d)
WHEN 8 THEN DATEPART(hour, @.d)
WHEN 9 THEN DATEPART(minute, @.d)
WHEN 10 THEN DATEPART(second, @.d)
WHEN 11 THEN DATEPART(millisecond, @.d)
END as SomePart
Roy Harvey
Beacon Falls, CT
On 21 Mar 2007 09:51:51 -0700, retima@.gmail.com wrote:
>On a paramater of datepart (like the first one in DateAdd),
>can I in any chance pass a parameter?
>(and not use the constants week, month etc.)
>SELECT DateAdd(@.my_dt , 1 , CONVERT (DATETIME, '01/01/01')) As 'Date'
|||On Mar 21, 8:14 pm, Roy Harvey <roy_har...@.snet.net> wrote:
> You could use brute force.
> DECLARE @.d datetime
> SET @.d = GETDATE()
> DECLARE @.x int
> SET @.x = 7
> SELECT CASE @.x
> WHEN 1 THEN DATEPART(year, @.d)
> WHEN 2 THEN DATEPART(quarter, @.d)
> WHEN 3 THEN DATEPART(month, @.d)
> WHEN 4 THEN DATEPART(dayofyear, @.d)
> WHEN 5 THEN DATEPART(day, @.d)
> WHEN 6 THEN DATEPART(week, @.d)
> WHEN 7 THEN DATEPART(weekday, @.d)
> WHEN 8 THEN DATEPART(hour, @.d)
> WHEN 9 THEN DATEPART(minute, @.d)
> WHEN 10 THEN DATEPART(second, @.d)
> WHEN 11 THEN DATEPART(millisecond, @.d)
> END as SomePart
> Roy Harvey
> Beacon Falls, CT
> On 21 Mar 2007 09:51:51 -0700, ret...@.gmail.com wrote:
>
>
> - Show quoted text -
thanx guys, that helped!
datepart datatype
can I in any chance pass a parameter?
(and not use the constants week, month etc.)
SELECT DateAdd(@.my_dt , 1 , CONVERT (DATETIME, '01/01/01')) As 'Date'I think you would need dynamic SQL, e.g.
EXEC('SELECT DateAdd(' + @.my_dt + ', 1, ...') AS [Date];
Aaron Bertrand
SQL Server MVP
http://www.sqlblog.com/
http://www.aspfaq.com/5006
<retima@.gmail.com> wrote in message
news:1174495911.706163.321400@.e65g2000hsc.googlegroups.com...
> On a paramater of datepart (like the first one in DateAdd),
> can I in any chance pass a parameter?
> (and not use the constants week, month etc.)
> SELECT DateAdd(@.my_dt , 1 , CONVERT (DATETIME, '01/01/01')) As 'Date'
>|||You could use brute force.
DECLARE @.d datetime
SET @.d = GETDATE()
DECLARE @.x int
SET @.x = 7
SELECT CASE @.x
WHEN 1 THEN DATEPART(year, @.d)
WHEN 2 THEN DATEPART(quarter, @.d)
WHEN 3 THEN DATEPART(month, @.d)
WHEN 4 THEN DATEPART(dayofyear, @.d)
WHEN 5 THEN DATEPART(day, @.d)
WHEN 6 THEN DATEPART(week, @.d)
WHEN 7 THEN DATEPART(weekday, @.d)
WHEN 8 THEN DATEPART(hour, @.d)
WHEN 9 THEN DATEPART(minute, @.d)
WHEN 10 THEN DATEPART(second, @.d)
WHEN 11 THEN DATEPART(millisecond, @.d)
END as SomePart
Roy Harvey
Beacon Falls, CT
On 21 Mar 2007 09:51:51 -0700, retima@.gmail.com wrote:
>On a paramater of datepart (like the first one in DateAdd),
>can I in any chance pass a parameter?
>(and not use the constants week, month etc.)
>SELECT DateAdd(@.my_dt , 1 , CONVERT (DATETIME, '01/01/01')) As 'Date'|||On Mar 21, 8:14 pm, Roy Harvey <roy_har...@.snet.net> wrote:
> You could use brute force.
> DECLARE @.d datetime
> SET @.d = GETDATE()
> DECLARE @.x int
> SET @.x = 7
> SELECT CASE @.x
> WHEN 1 THEN DATEPART(year, @.d)
> WHEN 2 THEN DATEPART(quarter, @.d)
> WHEN 3 THEN DATEPART(month, @.d)
> WHEN 4 THEN DATEPART(dayofyear, @.d)
> WHEN 5 THEN DATEPART(day, @.d)
> WHEN 6 THEN DATEPART(week, @.d)
> WHEN 7 THEN DATEPART(weekday, @.d)
> WHEN 8 THEN DATEPART(hour, @.d)
> WHEN 9 THEN DATEPART(minute, @.d)
> WHEN 10 THEN DATEPART(second, @.d)
> WHEN 11 THEN DATEPART(millisecond, @.d)
> END as SomePart
> Roy Harvey
> Beacon Falls, CT
> On 21 Mar 2007 09:51:51 -0700, ret...@.gmail.com wrote:
>
>
>
> - Show quoted text -
thanx guys, that helped!
datepart datatype
can I in any chance pass a parameter?
(and not use the constants week, month etc.)
SELECT DateAdd(@.my_dt , 1 , CONVERT (DATETIME, '01/01/01')) As 'Date'I think you would need dynamic SQL, e.g.
EXEC('SELECT DateAdd(' + @.my_dt + ', 1, ...') AS [Date];
--
Aaron Bertrand
SQL Server MVP
http://www.sqlblog.com/
http://www.aspfaq.com/5006
<retima@.gmail.com> wrote in message
news:1174495911.706163.321400@.e65g2000hsc.googlegroups.com...
> On a paramater of datepart (like the first one in DateAdd),
> can I in any chance pass a parameter?
> (and not use the constants week, month etc.)
> SELECT DateAdd(@.my_dt , 1 , CONVERT (DATETIME, '01/01/01')) As 'Date'
>|||You could use brute force.
DECLARE @.d datetime
SET @.d = GETDATE()
DECLARE @.x int
SET @.x = 7
SELECT CASE @.x
WHEN 1 THEN DATEPART(year, @.d)
WHEN 2 THEN DATEPART(quarter, @.d)
WHEN 3 THEN DATEPART(month, @.d)
WHEN 4 THEN DATEPART(dayofyear, @.d)
WHEN 5 THEN DATEPART(day, @.d)
WHEN 6 THEN DATEPART(week, @.d)
WHEN 7 THEN DATEPART(weekday, @.d)
WHEN 8 THEN DATEPART(hour, @.d)
WHEN 9 THEN DATEPART(minute, @.d)
WHEN 10 THEN DATEPART(second, @.d)
WHEN 11 THEN DATEPART(millisecond, @.d)
END as SomePart
Roy Harvey
Beacon Falls, CT
On 21 Mar 2007 09:51:51 -0700, retima@.gmail.com wrote:
>On a paramater of datepart (like the first one in DateAdd),
>can I in any chance pass a parameter?
>(and not use the constants week, month etc.)
>SELECT DateAdd(@.my_dt , 1 , CONVERT (DATETIME, '01/01/01')) As 'Date'|||On Mar 21, 8:14 pm, Roy Harvey <roy_har...@.snet.net> wrote:
> You could use brute force.
> DECLARE @.d datetime
> SET @.d = GETDATE()
> DECLARE @.x int
> SET @.x = 7
> SELECT CASE @.x
> WHEN 1 THEN DATEPART(year, @.d)
> WHEN 2 THEN DATEPART(quarter, @.d)
> WHEN 3 THEN DATEPART(month, @.d)
> WHEN 4 THEN DATEPART(dayofyear, @.d)
> WHEN 5 THEN DATEPART(day, @.d)
> WHEN 6 THEN DATEPART(week, @.d)
> WHEN 7 THEN DATEPART(weekday, @.d)
> WHEN 8 THEN DATEPART(hour, @.d)
> WHEN 9 THEN DATEPART(minute, @.d)
> WHEN 10 THEN DATEPART(second, @.d)
> WHEN 11 THEN DATEPART(millisecond, @.d)
> END as SomePart
> Roy Harvey
> Beacon Falls, CT
> On 21 Mar 2007 09:51:51 -0700, ret...@.gmail.com wrote:
>
> >On a paramater of datepart (like the first one in DateAdd),
> >can I in any chance pass a parameter?
> >(and not use the constants week, month etc.)
> >SELECT DateAdd(@.my_dt , 1 , CONVERT (DATETIME, '01/01/01')) As 'Date'- Hide quoted text -
> - Show quoted text -
thanx guys, that helped!
Sunday, February 19, 2012
datediff with parameter
I am trying to run the following query but want to use a parameter instaed
of the -7
BETWEEN GETDATE() AND DATEADD(d, -7, GETDATE())
ive tried BETWEEN GETDATE() AND DATEADD(d, @.date, GETDATE()) with the
parameter set is an int & text but to no avaial.
Thanks in advanceTry BETWEEN GETDATE() AND DATEADD("d",CONVERT(INT,@.date),GETDATE())
I got it to work by converting the param to INT. It appears SSRS
defaults params to the nvarchar datatype.
Hang in there
toolman
Tango wrote:
> Hi
> I am trying to run the following query but want to use a parameter instaed
> of the -7
> BETWEEN GETDATE() AND DATEADD(d, -7, GETDATE())
> ive tried BETWEEN GETDATE() AND DATEADD(d, @.date, GETDATE()) with the
> parameter set is an int & text but to no avaial.
> Thanks in advance
Friday, February 17, 2012
DateDiff / DateAdd - please help me:(
hello,
i have a Pictures table: PictureID, Name, Description, DateAdded (GETDATE() when insert), IsActive...
i need to make some stored procedures to show me the pictures added in last 24hours, in last 3 days, last 2 weeks and so on
the pictures added in database are active (available to be seen by users) only 1yaer after the date added
I tryied to make a stored procedure (in fact i maked a lots of them, for 1day 3 days 1 week 1 month), but i have a problem with that DateDiff and DateAdd
Here is what i tryied
CREATE PROCEDURE LastAdded_2monthsAgoASSELECT Pictures.ProductID, Pictures.Name, Pictures.Description, Pictures.DateAddedFROM PicturesWHERE (DATEDIFF(month, Pictures.DateAdded,GETDATE()) >= 0)AND (DATEDIFF(month, Pictures.DateAdded,GETDATE()) <= 2)ORDER BY DateAddedDesc
I have a feeling that is wrong, please make your own version and show me what i should write...I don't know what should be first the today date or the DateAdded...
i need to select the last added products from a specific interval of time...
Should i do something with that "1 year available" likeWHERE (DATEDIFF(month,GETDATE(),DATEADD(year, 1, Products.DateAdded)) >= 0)AND (DATEDIFF(month,GETDATE(),DATEADD(year, 1, Products.DateAdded)) <= 2)I am sure is a stupid thig up there...if you can, make your own version how you would do it and show me..please help me
The simplest way to do this would be with a single stored procedure, and pass in the two dates to search between. (make sure you use a standard time scheme in order for this to be accurate, such as UTC/GMT.)
I suggest creating the start and end date in your [web] application, and your procedure should be like that below:
CREATE PROCEDURE LastAdded_2monthsAgo @.StartDatedatetime, @.EndDatedatetimeASSELECT Pictures.ProductID, Pictures.[Name], Pictures.[Description], Pictures.DateAddedFROM PicturesWHERE (Pictures.DateAddedBETWEEN @.StartDateAND @.EndDate)ORDER BY DateAddedDesc
Also note that you are using keywords for column names, which is why they show up blue. While this will still work in some cases, it is best to qualify the keyword as a column, which is why I used square brackets around those 2 column names. (Some people do this with every column.)
|||LAST 24 HOURS:
WHERE Pictures.DateAdded >= DATEADD(DD,-1,GETDATE())
LAST 3 DAYS:
WHERE Pictures.DateAdded >= DATEADD(DD,-3,GETDATE())
LAST 2 WEEKS:
WHERE Pictures.DateAdded >= DATEADD(DD,-14,GETDATE())
SAMPLE SPROC:
CREATE PROCEDURE LastAdded_2monthsAgo
@.Number_Of_Days int
AS
SELECT Pictures.ProductID, Pictures.Name, Pictures.Description, Pictures.DateAdded
FROM Pictures
WHERE Pictures.DateAdded >= DATEADD(DD,@.Number_Of_Days, GetDate())
ORDER BY DateAddedDesc
Hope this helps,
I should also clarify that when you are building your dates to search between, the first should have a time of 12:00 (midnight), and the end date should end at 11:59 PM. So if you are searching just for today's uploaded files, you would search between "9/28/2007 12:00:00 AM" and "9/28/2007 11:59:59 PM".
|||It's usually better if you use midnight the next day as the end point (It avoids date/time accuracy issues).
Like;
WHERE MyDATE>='9/28/2007 12:00:00 AM' AND MyDate<'9/29/2007 12:00:00 AM'
or more concisely:
WHERE MyDate>='9/28/2007' AND MyDate<'9/29/2007'
|||thank you to all for helping me
to show the pictures from the last 2 days or more i user the rhp_72 stored procedure,
and to show the pictures from a specific interval of time i used the ps2goat stored procedure
thank you, you resolved my problem
Tuesday, February 14, 2012
DateAdd() in Default Value
My goal is to set the default value of a smalldatetime field to the GetDate() plus 4 hours. I figure I need to use DateAdd() for this, but can't figure out how to place it in a default value. I can put GetDate() in default value, but DateAdd(hour,4,GetDate()) throws a syntax error.
Is there a simple way to accomplish this?
bes7252,What error are you seeing, exactly? I have no problem executing the following code:
create table t(
i int,
d datetime default (dateadd(hour,3,getdate()))
)
go
drop table t
Have you left out a ) somewhere?
Steve Kass
Drew University
http://www.stevekass.com
|||
Logically it doesn’t make any sense to have this expression on your column. Suppose if you generate the expression on your stored date then it is acceptable.
|||Steve,
I tried it again and didn't have any problems. I don't remember the error I was seeing yesterday, but must have had a typo-o in my expression. (I typed the one on the post from memory, not copy/paste).
Thanks!
Brian
DATEADD with 8 byte int?
I have a bigint column called "MillisecondsSince1970" that I need to convert to a date - SSIS is erroring out when I use DATEADD with the 8 byte int (if I use 4 byte it works but the column is bigger than 4 byte). The error is really lame:
[Derived Column [79]] Error: The "component "Derived Column" (79)" failed because error code 0xC0049067 occurred, and the error row disposition on "output column "Date" (100)" specifies failure on error. An error occurred on the specified object of the specified component.
Anyone have a way around it... a VB.NET equivalent of DATEADD or something else I can do?
Why not do it in stages? Convert milliseconds to say, days, and then add that to your date via dateadd. Then take the remaining milliseconds and add to the new date field. For each step, cast to INT and then pass into dateadd.So, 12636598775 milliseconds = 146 days, 22198775 milliseconds
In a derived column, for instance:
days = (DT_I4)(DT_NUMERIC, 30, 8)([millisecond_column] / 1000 / 60 / 60 / 24)
In a second derived column:
newdatecolumn = dateadd("d", [days], [date_column])
finaldatecolumn = dateadd("ms", (DT_I4)([millisecond_column] - ([days] * 24 * 60 * 60 * 1000)),[newdatecolumn])|||Phil that is an excellent idea... I kept trying ways to do it in one object - didn't think about doing it like that. (FYI VB.NET doesn't have milliseconds as a DateInterval so you can't do it in script without doing something like phil suggests)
dateadd update works but adds 2 or 3 yrs-not 1
Here is the page_load event:
Sub page_load(sender as object, e as eventargs)And here is the SPROC:
Try
Dim connection As SqlConnection = new SqlConnection(ConfigurationSettings.AppSettings("ConnectionString"))
Dim command As SqlCommand = new SqlCommand("Updateexpiredate", connection)
command.CommandType = CommandType.StoredProcedureDim param0 As SqlParameter = new SqlParameter("@.memberid",SqlDbType.Int)
param0.Direction = ParameterDirection.Input
param0.Value = memberid
command.Parameters.Add(param0)connection.Open()
command.ExecuteNonQuery()
connection.Close()
myerror.Text = "Thank You! Your account was updated"
Catch ex As Exception
myerror.Text = ex.Message
End Try
End Sub
CREATE PROCEDURE Updateexpiredate... I'm assuming you're calling that method or procedure 2 or three time. :)
(
@.memberid int
)
AS
UPDATE
members
SET
expiredate=(dateadd(year,1,expiredate))<--I also tried expiredate=(dateadd(month,12,expiredate)) with the same results
WHERE
memberID = @.memberID
GO
edited: Step through your app. It could be that it's called multiple times.|||How do I do that?
I don't even know where to begin with that.
Thanks|||The first thing I would do is turn tracing on for the page, and do a trace.write as the command right before the ExecuteNonQuery. This should give you more information on how many times that part of your code is being executed.
If you are not familiar with trace.write, look here:Trace Logging to Page Output. That is a good debugging tool to have in your pocket.
Terri|||What do you use to develop in? Is it Visual Studio, DreamWeaver, WebMatrix, notepad?
DATEADD returning odd count of records
We have a table here containing over 18 million rows and is updatred to
the tune of about 50,000 rows per day. Once a month I would like to run
a script that will delete any rows older than 18 months. All easy, I
here you say. Well when I do the following:
select count(*) from MyTable
where [Insertion Date] <= DATEADD(m, -18, getdate())
this returns a count of approx. 610,000, however when I workout all the
rows for the month of January 2004 (18 months ago the month I need to
remove) I return a count of approx 812,000
select count(*) from MyTable
where [Insertion Date] >= '20040101' and [Insertion Date] <= '20040131'
Apologies for being a tad vague here (obviously you don't know the ins
and outs of the data) but I was wondering if anyone else out there had
the same issue. The table schema for the first few columns is below:
CREATE TABLE [MyTable] (
[ID] [int] IDENTITY (1, 1) NOT NULL ,
[Consignment] [varchar] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
,
[Date] [datetime] NULL ,
[Batch ID] [int] NOT NULL ,
[Insertion Date] [datetime] NULL ,
Thanks
qhDATEADD(m, -18, getdate()) is giving you 26th Jan 2004. That means
everything older than or equal to 26th Jan 2004. But your second query is
looking for data between 1st and 31st of Jan, which obviously is going to
have 5 days worth of additional data.
--
HTH,
Vyas, MVP (SQL Server)
SQL Server Articles and Code Samples @. http://vyaskn.tripod.com/
"Scott" <quackhandle1975@.yahoo.co.uk> wrote in message
news:1122371409.746888.44830@.g49g2000cwa.googlegroups.com...
Hi,
We have a table here containing over 18 million rows and is updatred to
the tune of about 50,000 rows per day. Once a month I would like to run
a script that will delete any rows older than 18 months. All easy, I
here you say. Well when I do the following:
select count(*) from MyTable
where [Insertion Date] <= DATEADD(m, -18, getdate())
this returns a count of approx. 610,000, however when I workout all the
rows for the month of January 2004 (18 months ago the month I need to
remove) I return a count of approx 812,000
select count(*) from MyTable
where [Insertion Date] >= '20040101' and [Insertion Date] <= '20040131'
Apologies for being a tad vague here (obviously you don't know the ins
and outs of the data) but I was wondering if anyone else out there had
the same issue. The table schema for the first few columns is below:
CREATE TABLE [MyTable] (
[ID] [int] IDENTITY (1, 1) NOT NULL ,
[Consignment] [varchar] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
,
[Date] [datetime] NULL ,
[Batch ID] [int] NOT NULL ,
[Insertion Date] [datetime] NULL ,
Thanks
qh|||Hello, Scott
You should notice the fact that "DATEADD(m, -18, getdate())"
returns January 26, 2004 and in your second query you are
comparing with January 31, 2004.
If you want to obtain the last day of the current month,
you can use the following:
SELECT DATEADD(month, DATEDIFF(month, 0, getdate()) + 1, 0) - 1
So your query might be:
select count(*) from MyTable
where [Insertion Date] <= DATEADD(m,DATEDIFF(m,0,getdate())-17,0)-1
Razvan|||Hi Razvan,
Cheers for the reply, your solution sorted it. I didn't expect the
count to be over 200k for the days 26-31st Jan. I think it's myself
who doesn't know his own data!
;o)
Thanks
Scott
Razvan Socol wrote:
> Hello, Scott
> You should notice the fact that "DATEADD(m, -18, getdate())"
> returns January 26, 2004 and in your second query you are
> comparing with January 31, 2004.
> If you want to obtain the last day of the current month,
> you can use the following:
> SELECT DATEADD(month, DATEDIFF(month, 0, getdate()) + 1, 0) - 1
> So your query might be:
> select count(*) from MyTable
> where [Insertion Date] <= DATEADD(m,DATEDIFF(m,0,getdate())-17,0)-1
> Razvan
Dateadd problem
Try this:
print dateadd(y,+4, '29 feb 2008')
Funny how the 1 day that is used to go from 29 jan to 1 mar stack if you add multible years to a day.
+4 year should put it back on 29 feb 2012, +3 years should put it on 1 mar 2011.
Well, mabye it has been posted before, I just found it interesting.
That is because you are using y not yy. Look in the BOL for the parameters. dy/y is dayofyear not year.HTH, Jens K. Suessmeyer.
http://www.sqlserver2005.de
DateAdd narrowing conversion error
I am simply trying to take a date and add the value from another field to it using the following expression:
=dateadd("d", " & Fields!new_tgddatefrom.Value & ", Parameters!HolidayDate.Value)
new_tgddatefrom.Value is an int data type
HolidayDate.Value is a date
The expression works fine if I use an acutal number for the value to add but when I use the field.value I get an error:
Overload resoution failed because no accessible 'DateAdd can be called without a narrowing conversion.
Why or why is it so difficult to work with dates!?!? If anyone can help me get this working I would sincerely appreciate it!
Debra
Debra:The multiplier in the dateadd function must be an integer and since you say the field value is one why concatenate the quotes?
=dateadd("d", Fields!new_tgddatefrom.Value, Parameters!HolidayDate.Value)
Petr|||Dear
Plz check date type of date interval, both date should be date type.
Thanks
Manoj Singh
DateAdd narrowing conversion error
I am simply trying to take a date and add the value from another field to it using the following expression:
=dateadd("d", " & Fields!new_tgddatefrom.Value & ", Parameters!HolidayDate.Value)
new_tgddatefrom.Value is an int data type
HolidayDate.Value is a date
The expression works fine if I use an acutal number for the value to add but when I use the field.value I get an error:
Overload resoution failed because no accessible 'DateAdd can be called without a narrowing conversion.
Why or why is it so difficult to work with dates!?!? If anyone can help me get this working I would sincerely appreciate it!
Debra
Debra:The multiplier in the dateadd function must be an integer and since you say the field value is one why concatenate the quotes?
=dateadd("d", Fields!new_tgddatefrom.Value, Parameters!HolidayDate.Value)
Petr
|||Dear
Plz check date type of date interval, both date should be date type.
Thanks
Manoj Singh
DATEADD Issue
following code works when I run it from a query window in SQL 2005, but it
generates and error message when I schedule it as a job. The error I get
is...
Msg 1023, Level 15, State 1, Line 10
Invalid parameter 1 specified for dateadd.
I have tried the code below using "Month", "M", 'Month', 'M', 'mm', and more
as the parameters for DATEADD, but all return the same error from job
execution. I'm sure that I'm just missing something simple, but its driving
me crazy.
IF Day(GETDATE()) = 1
BEGIN
DECLARE
@.NewName VARCHAR(12),
@.Current VARCHAR(12),
@.DataFile VARCHAR(100),
@.LogFile VARCHAR(100),
@.ThisDate DATETIME
SELECT @.ThisDate = DATEADD("Month", -1,GETDATE())
SELECT @.NewName = 'BIDW_' + CONVERT(VARCHAR(4),Year(@.ThisDate)) + '_' +
RIGHT('0' + CONVERT(VARCHAR(2),Month(@.ThisDate)),2)
SELECT @.Current = 'BIDW_' + CONVERT(VARCHAR(4),Year(GETDATE())) + '_' +
RIGHT('0' + CONVERT(VARCHAR(2),Month(GETDATE())),2)
SELECT @.DataFile = 'D:\SQLServer2005\Databases\BIDW\' + @.Current +
'_Data.mdf'
SELECT @.LogFile = 'D:\SQLServer2005\Databases\BIDW\' + @.Current +
'_Log.ldf'
EXEC sp_renamedb 'BIDW_Monthend', @.NewName
USE BIDW
RESTORE FILELISTONLY
FROM BIDW_on_Domino1
RESTORE DATABASE BIDW_Monthend
FROM BIDW_on_Domino1
WITH RECOVERY,
MOVE 'BIDW' TO @.DataFile,
MOVE 'BIDW_log' TO @.LogFile
ENDWhenever you are stuck on a problem, just post the problem to everyone in a
newsgroup and within 2 minutes of posting the question, you will realize the
answer. Nevermind everyone, I figured it out. The first part of the DATEADD
parameter needs no quotes at all.
-Brian
"Brian VanDyke" <brianv@.community.nospam> wrote in message
news:%23wcLUNSaGHA.4144@.TK2MSFTNGP04.phx.gbl...
>I am attempting to automate a monthend snapshot of my data, and the
>following code works when I run it from a query window in SQL 2005, but it
>generates and error message when I schedule it as a job. The error I get
>is...
> Msg 1023, Level 15, State 1, Line 10
> Invalid parameter 1 specified for dateadd.
> I have tried the code below using "Month", "M", 'Month', 'M', 'mm', and
> more as the parameters for DATEADD, but all return the same error from job
> execution. I'm sure that I'm just missing something simple, but its
> driving me crazy.
> IF Day(GETDATE()) = 1
> BEGIN
> DECLARE
> @.NewName VARCHAR(12),
> @.Current VARCHAR(12),
> @.DataFile VARCHAR(100),
> @.LogFile VARCHAR(100),
> @.ThisDate DATETIME
> SELECT @.ThisDate = DATEADD("Month", -1,GETDATE())
> SELECT @.NewName = 'BIDW_' + CONVERT(VARCHAR(4),Year(@.ThisDate)) + '_' +
> RIGHT('0' + CONVERT(VARCHAR(2),Month(@.ThisDate)),2)
> SELECT @.Current = 'BIDW_' + CONVERT(VARCHAR(4),Year(GETDATE())) + '_' +
> RIGHT('0' + CONVERT(VARCHAR(2),Month(GETDATE())),2)
> SELECT @.DataFile = 'D:\SQLServer2005\Databases\BIDW' + @.Current +
> '_Data.mdf'
> SELECT @.LogFile = 'D:\SQLServer2005\Databases\BIDW' + @.Current +
> '_Log.ldf'
> EXEC sp_renamedb 'BIDW_Monthend', @.NewName
> USE BIDW
> RESTORE FILELISTONLY
> FROM BIDW_on_Domino1
> RESTORE DATABASE BIDW_Monthend
> FROM BIDW_on_Domino1
> WITH RECOVERY,
> MOVE 'BIDW' TO @.DataFile,
> MOVE 'BIDW_log' TO @.LogFile
> END
>
dateadd in expression error
=datepart("d",dateadd("d", 3, @.startDate))
I have this or something similar in many textboxes and they are all
returning the following error:
The value expression for the textbox â'textbox24â' contains an error:
[BC30037] Character is not valid.
Any ideas? Please help!Replace @.startDate with Parameters!StartDate.Value
GeoSYnch
"SharinDenver" <SharinDenver@.discussions.microsoft.com> wrote in message
news:CC20E91E-80CC-4CF0-AB8F-D02FCD513B73@.microsoft.com...
>I have the following in a textbox expression:
> =datepart("d",dateadd("d", 3, @.startDate))
> I have this or something similar in many textboxes and they are all
> returning the following error:
> The value expression for the textbox 'textbox24' contains an error:
> [BC30037] Character is not valid.
> Any ideas? Please help!
>
>|||THANK YOU!!!!!!
"GeoSynch" wrote:
> Replace @.startDate with Parameters!StartDate.Value
>
> GeoSYnch
>
> "SharinDenver" <SharinDenver@.discussions.microsoft.com> wrote in message
> news:CC20E91E-80CC-4CF0-AB8F-D02FCD513B73@.microsoft.com...
> >I have the following in a textbox expression:
> >
> > =datepart("d",dateadd("d", 3, @.startDate))
> >
> > I have this or something similar in many textboxes and they are all
> > returning the following error:
> >
> > The value expression for the textbox 'textbox24' contains an error:
> > [BC30037] Character is not valid.
> >
> > Any ideas? Please help!
> >
> >
> >
>
>
DateAdd function on 30th and 31st of month
When I run the dateadd function adding months on 30th and 31st of any
months returns the same result. I am OK with that because logically it
is correct.
If I have to do a reverse lookup for the date, how can I do that.
Here is the code sample...
select dateadd(mm,6,'2006-05-30 00:00:00.000')
select dateadd(mm,6,'2006-05-31 00:00:00.000')
-- 2006-11-30 00:00:00.000
-- 2006-11-30 00:00:00.000
select dateadd(mm,-6,'2006-11-30 00:00:00.000')
select dateadd(mm,-6,'2006-12-01 00:00:00.000')
--2006-05-30 00:00:00.000
--2006-06-01 00:00:00.000
The problem I have is when I do a reverse lookup I am missing all the
records which got created on
2006-05-31 00:00:00.000.
Thanks in Advance...
Thanks,
GaneshGanesh wrote:
> Hi Everyone,
> When I run the dateadd function adding months on 30th and 31st of any
> months returns the same result. I am OK with that because logically it
> is correct.
> If I have to do a reverse lookup for the date, how can I do that.
> Here is the code sample...
> select dateadd(mm,6,'2006-05-30 00:00:00.000')
> select dateadd(mm,6,'2006-05-31 00:00:00.000')
> -- 2006-11-30 00:00:00.000
> -- 2006-11-30 00:00:00.000
> select dateadd(mm,-6,'2006-11-30 00:00:00.000')
> select dateadd(mm,-6,'2006-12-01 00:00:00.000')
> --2006-05-30 00:00:00.000
> --2006-06-01 00:00:00.000
> The problem I have is when I do a reverse lookup I am missing all the
> records which got created on
> 2006-05-31 00:00:00.000.
> Thanks in Advance...
> Thanks,
> Ganesh
>
This will show you the date of the last day of the month "six months ago":
SELECT DATEADD(dd, -1, DATEADD(mm, DATEDIFF(mm, 0, DATEADD(mm, -5,
GETDATE())), 0))
Tracy McKibben
MCDBA
http://www.realsqlguy.com|||Please don't multi-post.
"Ganesh" <babuganesh2000@.gmail.com> wrote in message
news:1164661068.378626.271490@.l12g2000cwl.googlegroups.com...
> Hi Everyone,
> When I run the dateadd function adding months on 30th and 31st of any
> months returns the same result. I am OK with that because logically it
> is correct.
> If I have to do a reverse lookup for the date, how can I do that.
> Here is the code sample...
> select dateadd(mm,6,'2006-05-30 00:00:00.000')
> select dateadd(mm,6,'2006-05-31 00:00:00.000')
> -- 2006-11-30 00:00:00.000
> -- 2006-11-30 00:00:00.000
> select dateadd(mm,-6,'2006-11-30 00:00:00.000')
> select dateadd(mm,-6,'2006-12-01 00:00:00.000')
> --2006-05-30 00:00:00.000
> --2006-06-01 00:00:00.000
> The problem I have is when I do a reverse lookup I am missing all the
> records which got created on
> 2006-05-31 00:00:00.000.
> Thanks in Advance...
> Thanks,
> Ganesh
>
DateAdd function on 30th and 31st of month
When I run the dateadd function adding months on 30th and 31st of any
months returns the same result. I am OK with that because logically it
is correct.
If I have to do a reverse lookup for the date, how can I do that.
Here is the code sample...
select dateadd(mm,6,'2006-05-30 00:00:00.000')
select dateadd(mm,6,'2006-05-31 00:00:00.000')
-- 2006-11-30 00:00:00.000
-- 2006-11-30 00:00:00.000
select dateadd(mm,-6,'2006-11-30 00:00:00.000')
select dateadd(mm,-6,'2006-12-01 00:00:00.000')
--2006-05-30 00:00:00.000
--2006-06-01 00:00:00.000
The problem I have is when I do a reverse lookup I am missing all the
records which got created on
2006-05-31 00:00:00.000.
Thanks in Advance...
Thanks,
Ganesh
Ganesh wrote:
> Hi Everyone,
> When I run the dateadd function adding months on 30th and 31st of any
> months returns the same result. I am OK with that because logically it
> is correct.
> If I have to do a reverse lookup for the date, how can I do that.
> Here is the code sample...
> select dateadd(mm,6,'2006-05-30 00:00:00.000')
> select dateadd(mm,6,'2006-05-31 00:00:00.000')
> -- 2006-11-30 00:00:00.000
> -- 2006-11-30 00:00:00.000
> select dateadd(mm,-6,'2006-11-30 00:00:00.000')
> select dateadd(mm,-6,'2006-12-01 00:00:00.000')
> --2006-05-30 00:00:00.000
> --2006-06-01 00:00:00.000
> The problem I have is when I do a reverse lookup I am missing all the
> records which got created on
> 2006-05-31 00:00:00.000.
> Thanks in Advance...
> Thanks,
> Ganesh
>
This will show you the date of the last day of the month "six months ago":
SELECT DATEADD(dd, -1, DATEADD(mm, DATEDIFF(mm, 0, DATEADD(mm, -5,
GETDATE())), 0))
Tracy McKibben
MCDBA
http://www.realsqlguy.com
|||Please don't multi-post.
"Ganesh" <babuganesh2000@.gmail.com> wrote in message
news:1164661068.378626.271490@.l12g2000cwl.googlegr oups.com...
> Hi Everyone,
> When I run the dateadd function adding months on 30th and 31st of any
> months returns the same result. I am OK with that because logically it
> is correct.
> If I have to do a reverse lookup for the date, how can I do that.
> Here is the code sample...
> select dateadd(mm,6,'2006-05-30 00:00:00.000')
> select dateadd(mm,6,'2006-05-31 00:00:00.000')
> -- 2006-11-30 00:00:00.000
> -- 2006-11-30 00:00:00.000
> select dateadd(mm,-6,'2006-11-30 00:00:00.000')
> select dateadd(mm,-6,'2006-12-01 00:00:00.000')
> --2006-05-30 00:00:00.000
> --2006-06-01 00:00:00.000
> The problem I have is when I do a reverse lookup I am missing all the
> records which got created on
> 2006-05-31 00:00:00.000.
> Thanks in Advance...
> Thanks,
> Ganesh
>
DateAdd function on 30th and 31st of month
When I run the dateadd function adding months on 30th and 31st of any
months returns the same result. I am OK with that because logically it
is correct.
If I have to do a reverse lookup for the date, how can I do that.
Here is the code sample...
select dateadd(mm,6,'2006-05-30 00:00:00.000')
select dateadd(mm,6,'2006-05-31 00:00:00.000')
-- 2006-11-30 00:00:00.000
-- 2006-11-30 00:00:00.000
select dateadd(mm,-6,'2006-11-30 00:00:00.000')
select dateadd(mm,-6,'2006-12-01 00:00:00.000')
--2006-05-30 00:00:00.000
--2006-06-01 00:00:00.000
The problem I have is when I do a reverse lookup I am missing all the
records which got created on
2006-05-31 00:00:00.000.
Thanks in Advance...
Thanks,
GaneshGanesh wrote:
> Hi Everyone,
> When I run the dateadd function adding months on 30th and 31st of any
> months returns the same result. I am OK with that because logically it
> is correct.
> If I have to do a reverse lookup for the date, how can I do that.
> Here is the code sample...
> select dateadd(mm,6,'2006-05-30 00:00:00.000')
> select dateadd(mm,6,'2006-05-31 00:00:00.000')
> -- 2006-11-30 00:00:00.000
> -- 2006-11-30 00:00:00.000
> select dateadd(mm,-6,'2006-11-30 00:00:00.000')
> select dateadd(mm,-6,'2006-12-01 00:00:00.000')
> --2006-05-30 00:00:00.000
> --2006-06-01 00:00:00.000
> The problem I have is when I do a reverse lookup I am missing all the
> records which got created on
> 2006-05-31 00:00:00.000.
> Thanks in Advance...
> Thanks,
> Ganesh
>
This will show you the date of the last day of the month "six months ago":
SELECT DATEADD(dd, -1, DATEADD(mm, DATEDIFF(mm, 0, DATEADD(mm, -5,
GETDATE())), 0))
Tracy McKibben
MCDBA
http://www.realsqlguy.com|||Please don't multi-post.
"Ganesh" <babuganesh2000@.gmail.com> wrote in message
news:1164661068.378626.271490@.l12g2000cwl.googlegroups.com...
> Hi Everyone,
> When I run the dateadd function adding months on 30th and 31st of any
> months returns the same result. I am OK with that because logically it
> is correct.
> If I have to do a reverse lookup for the date, how can I do that.
> Here is the code sample...
> select dateadd(mm,6,'2006-05-30 00:00:00.000')
> select dateadd(mm,6,'2006-05-31 00:00:00.000')
> -- 2006-11-30 00:00:00.000
> -- 2006-11-30 00:00:00.000
> select dateadd(mm,-6,'2006-11-30 00:00:00.000')
> select dateadd(mm,-6,'2006-12-01 00:00:00.000')
> --2006-05-30 00:00:00.000
> --2006-06-01 00:00:00.000
> The problem I have is when I do a reverse lookup I am missing all the
> records which got created on
> 2006-05-31 00:00:00.000.
> Thanks in Advance...
> Thanks,
> Ganesh
>