Showing posts with label date. Show all posts
Showing posts with label date. Show all posts

Monday, March 26, 2012

Problems getting Date

Hello there

I have a date value on a sql server database.
When viewing this value using Enterprise Manager Tools it shows the date as 2006-11-23 10:59:52

When i load this value into my webservice object to pass to my handheld device its shows the date as 2006-11-23T10:59:51.6130000-00:00

Im trying to retrieve a date value from my sql server ce 2.0 database.
When i use Query Analyser on the device and view the date it shows 2006-11-23 10:59:51.613000000

Now in my application when i use .ExecuteScalar or .ExecuteReader and reader.GetDate() to retrieve this value it returns 2006-11-23 10:59:51

I need to pass the date from the device to the webservice which will return any records greater than this date but since there is a minute difference in the dates it does not work right....

How can i sort this out?

My only idea is to convert the date being inserted on the device database to be of yyyy-mm-dd hh:mm:ss only

DateTime is not stored as yyyy-mm-dd or as MM-DD-YY or whatever other string representations of the date and time there are.

Here's what date time really is (http://msdn2.microsoft.com/en-us/library/ms172424.aspx):

Date and time data from January 1, 1753, to December 31, 9999, with an accuracy of one three-hundredth second, or 3.33 milliseconds. Values are rounded to increments of .000, .003, or .007 milliseconds.

Stored as two 4-byte integers. The first 4 bytes store the number of days before or after the base date, January 1, 1900. The base date is the system's reference date. Values for datetime earlier than January 1, 1753, are not permitted. The other 4 bytes store the time of day represented as the number of milliseconds after midnight. Seconds have a valid range of 0–59.

My point is: these dates of yours are the same, just printed and rounded differently. I’m assuming you mean a second difference, not minute - note EM rounds 1.6 second to 2 resulting in 10:59:52 while printing it on device results in 10:59.51 with .6 second been truncated.

sql

Wednesday, March 21, 2012

Problems calling ::fn_helpcollations() on MS SQL 7.0

Hi.
I have som date/time inconsistency between a MS SQL 7.0 srv. database which
I built up on a MS SQL 2000 srv. database. I was told to use the SELECT *
FROM ::fn_helpcollations() to find out the settings on my (old) 7.0
database, but it fails (incorrect syntax near ':'.). What's the correct
syntax on MS SQL 7.0?
Regards
Mr. SmithHi
There is no correct syntax for SQL Server 7.0 and Functions as Functions are
not supported in SQL Server 7.0.
Regards
--
Mike Epprecht, Microsoft SQL Server MVP
Zurich, Switzerland
IM: mike@.epprecht.net
MVP Program: http://www.microsoft.com/mvp
Blog: http://www.msmvps.com/epprecht/
"Mr. Smith" <nospam@.blindfolded.gone> wrote in message
news:Olby%23$CxFHA.1032@.TK2MSFTNGP12.phx.gbl...
> Hi.
> I have som date/time inconsistency between a MS SQL 7.0 srv. database
> which I built up on a MS SQL 2000 srv. database. I was told to use the
> SELECT * FROM ::fn_helpcollations() to find out the settings on my (old)
> 7.0 database, but it fails (incorrect syntax near ':'.). What's the
> correct syntax on MS SQL 7.0?
> Regards
> Mr. Smith
>

Tuesday, March 20, 2012

Problems calling ::fn_helpcollations() on MS SQL 7.0

Hi.
I have som date/time inconsistency between a MS SQL 7.0 srv. database which
I built up on a MS SQL 2000 srv. database. I was told to use the SELECT *
FROM ::fn_helpcollations() to find out the settings on my (old) 7.0
database, but it fails (incorrect syntax near ':'.). What's the correct
syntax on MS SQL 7.0?
Regards
Mr. SmithHi
There is no correct syntax for SQL Server 7.0 and Functions as Functions are
not supported in SQL Server 7.0.
Regards
--
Mike Epprecht, Microsoft SQL Server MVP
Zurich, Switzerland
IM: mike@.epprecht.net
MVP Program: http://www.microsoft.com/mvp
Blog: http://www.msmvps.com/epprecht/
"Mr. Smith" <nospam@.blindfolded.gone> wrote in message
news:Olby%23$CxFHA.1032@.TK2MSFTNGP12.phx.gbl...
> Hi.
> I have som date/time inconsistency between a MS SQL 7.0 srv. database
> which I built up on a MS SQL 2000 srv. database. I was told to use the
> SELECT * FROM ::fn_helpcollations() to find out the settings on my (old)
> 7.0 database, but it fails (incorrect syntax near ':'.). What's the
> correct syntax on MS SQL 7.0?
> Regards
> Mr. Smith
>

Problems calling ::fn_helpcollations() on MS SQL 7.0

Hi.
I have som date/time inconsistency between a MS SQL 7.0 srv. database which
I built up on a MS SQL 2000 srv. database. I was told to use the SELECT *
FROM ::fn_helpcollations() to find out the settings on my (old) 7.0
database, but it fails (incorrect syntax near ':'.). What's the correct
syntax on MS SQL 7.0?
Regards
Mr. Smith
Hi
There is no correct syntax for SQL Server 7.0 and Functions as Functions are
not supported in SQL Server 7.0.
Regards
Mike Epprecht, Microsoft SQL Server MVP
Zurich, Switzerland
IM: mike@.epprecht.net
MVP Program: http://www.microsoft.com/mvp
Blog: http://www.msmvps.com/epprecht/
"Mr. Smith" <nospam@.blindfolded.gone> wrote in message
news:Olby%23$CxFHA.1032@.TK2MSFTNGP12.phx.gbl...
> Hi.
> I have som date/time inconsistency between a MS SQL 7.0 srv. database
> which I built up on a MS SQL 2000 srv. database. I was told to use the
> SELECT * FROM ::fn_helpcollations() to find out the settings on my (old)
> 7.0 database, but it fails (incorrect syntax near ':'.). What's the
> correct syntax on MS SQL 7.0?
> Regards
> Mr. Smith
>

Friday, March 9, 2012

problem: select Max(date) which is less than actual Max(date)

based on the data at the beginning of this thread I use this query and get
this result
SELECT recid, Max(curDate) curDate, MAX(expDate) expDate
FROM #temp
GROUP BY recid
HAVING MAX(expDate)='1/25/06'
--gives the correct result for this query but......
recID curDate expDate
1 2005-11-28 00:00:00.000 2006-01-25 00:00:00.000
4 2005-11-23 00:00:00.000 2006-01-25 00:00:00.000
5 2005-11-27 00:00:00.000 2006-01-25 00:00:00.000
I am selecting only rows where the Max(expDate) is '1/25/06'. That would
include rows with recID of 1, 4, and 5 since recID's 2, 3, and 6 have a
Max(expDate) of '2/1/06' which is greater than '1/25/06'.
Well, I need to write another query that selects not only the rows with a
Max(expDate) of '1/25/06', but will also select rows that contain an expDate
of '1/25/06' -- IF -- the corresponding curDate for that row and recID i
s
the Max(curDate) for that recID for the corresponding expDate of '1/25/06'.
recID 6 contains a row with expDate of '1/25/06' which is not its
Max(expDate). However, the corresponding curDate for that row is '1/5/06'
OK. I see your problem. I screwed up. The curDate was supposed to be
'1/5/06' which would make it the Max(curDate) for recID 6 for the
corresponding expdate of '1/25/06'. So I need to pick up that row in
addition to the rows that have a Max(expDate) of '1/25/06', even though for
recID 6 expDate of 1/25/06 is not the Max(expDate), but curDate is
Max(curDate) for recID 6 expDate 1/25/06.
Sorry for the goof up. Any suggestions how I could write such a query?
"Roy Harvey" wrote:

> Rich,
> This is just a wee bit hard for me to sort out. One part in
> particular has me especially .
> The total set of data for recID = 6 is:
>
> Your desired result includes:
> I simply do not see where a curDate in 2006 came from that input data.
> Your specs seem to talk about using a different value for expDate
> under specific circumstances, but I was unable to see anything about
> using any different calculation for curDate than a value of curDate.
> Roy Harvey
> Beacon Falls, CT
>Here is what I came up with, including the last couple of lines of
data load with the correction. The basic idea is to write it as two
queries with a UNION. The first part is what you already have
working. The second part tries to get the row that meets the
secondary criteria. Note that the first query is pretty much
duplicated in the second for the NOT IN test.
While it appears to return the result you wanted from the data
provided, I do not guarantee it will do as well when you have the real
data.
select 6, '1/5/06', '1/25/06' union -- corrected curDate
select 6, '11/27/05', '2/1/06'
SELECT recid, Max(curDate) curDate, MAX(expDate) expDate
FROM #temp
GROUP BY recid
HAVING MAX(expDate)='1/25/06'
UNION ALL
SELECT *
FROM #temp as X
WHERE recid NOT IN --the list of recid from beginning
(SELECT recid
FROM #temp
GROUP BY recid
HAVING MAX(expDate)='1/25/06')
AND expDate = '1/25/06'
AND curDate >=
(select curDate from #temp as Y
where X.recid = Y.recid
and Y.expDate =
(select max(expDate) from #temp as Z
where X.recid = Z.recid))
Roy Harvey
Beacon Falls, CT|||Thanks very much. The Union query was the route I was going to go, but I
thought that maybe there a more sophisticated way to go. I guess I need to
have a little more confidence in myself.
Actually, the way you did it is fairly sophisticated. My idea of using a
Union query was way more verbose, and thus, I was trying to avoid it. But
you nailed it
thanks very much for your help.
Rich
"Roy Harvey" wrote:

> Here is what I came up with, including the last couple of lines of
> data load with the correction. The basic idea is to write it as two
> queries with a UNION. The first part is what you already have
> working. The second part tries to get the row that meets the
> secondary criteria. Note that the first query is pretty much
> duplicated in the second for the NOT IN test.
> While it appears to return the result you wanted from the data
> provided, I do not guarantee it will do as well when you have the real
> data.
> select 6, '1/5/06', '1/25/06' union -- corrected curDate
> select 6, '11/27/05', '2/1/06'
>
> SELECT recid, Max(curDate) curDate, MAX(expDate) expDate
> FROM #temp
> GROUP BY recid
> HAVING MAX(expDate)='1/25/06'
> UNION ALL
> SELECT *
> FROM #temp as X
> WHERE recid NOT IN --the list of recid from beginning
> (SELECT recid
> FROM #temp
> GROUP BY recid
> HAVING MAX(expDate)='1/25/06')
> AND expDate = '1/25/06'
> AND curDate >=
> (select curDate from #temp as Y
> where X.recid = Y.recid
> and Y.expDate =
> (select max(expDate) from #temp as Z
> where X.recid = Z.recid))
> Roy Harvey
> Beacon Falls, CT
>|||If I may, I added one more twist to this query. I added one more row to
#temp where recID 2 now also has an expDate of 1/25/06 which is less than it
s
max(expDate) of 2/1/06 - and recID 2 also has a Max(curDate) that correspond
s
to the expDate of 1/25/06 - same situation at recID 6 where the curDate that
corresponds to recID 2 expDate of 2/1/06 is less than the recID 2
Max(curDate) of 1/7/06. The 2nd part of the current Union query only picks
up recID 6.
So what I need to do is modify your query to see if I can pick up recID 2 in
addition to recID 6 along with the original recID's of 1, 4, 5.
This is the part where I was afraid of getting verbose. I will experiment
The real scenario has hundreds of thousands of records and there are rows
that match my sample. The pain.
"Rich" wrote:
> Thanks very much. The Union query was the route I was going to go, but I
> thought that maybe there a more sophisticated way to go. I guess I need t
o
> have a little more confidence in myself.
> Actually, the way you did it is fairly sophisticated. My idea of using a
> Union query was way more verbose, and thus, I was trying to avoid it. But
> you nailed it
> thanks very much for your help.
> Rich
> "Roy Harvey" wrote:
>|||I must be trying too hard. Your query works fine with the additional new ro
w
for recID 2. Sorry bout that.
"Rich" wrote:
> If I may, I added one more twist to this query. I added one more row to
> #temp where recID 2 now also has an expDate of 1/25/06 which is less than
its
> max(expDate) of 2/1/06 - and recID 2 also has a Max(curDate) that correspo
nds
> to the expDate of 1/25/06 - same situation at recID 6 where the curDate th
at
> corresponds to recID 2 expDate of 2/1/06 is less than the recID 2
> Max(curDate) of 1/7/06. The 2nd part of the current Union query only pic
ks
> up recID 6.
> So what I need to do is modify your query to see if I can pick up recID 2
in
> addition to recID 6 along with the original recID's of 1, 4, 5.
> This is the part where I was afraid of getting verbose. I will experiment
> The real scenario has hundreds of thousands of records and there are rows
> that match my sample. The pain.
>
> "Rich" wrote:
>|||so, taking this to the nth level (for posterity incase I need to refer to
this again), I added yet one more row to recID 2. This additional row is a
2nd expDate of 1/25/06. So now recID 2 has 2 rows with expdate of 1/25/06.
One of these rows contains a Max(curDate) for recID 2.
create table #temp(
recID int, curDate datetime, expDate datetime)
insert into #temp
select 1, '11/1/05', '1/5/06' union
select 1, '11/5/05', '1/13/06' union
select 1, '11/12/05', '1/25/06' union
select 1, '11/28/05', '1/25/06' union
select 2, '11/7/05', '1/7/06' union
select 2, '11/13/05', '1/12/06' union
select 2, '11/27/05', '1/15/06' union
select 2, '1/7/06', '1/25/06' union
select 2, '1/8/06', '1/25/06' union
select 2, '12/1/05', '2/1/06' union
select 3, '11/3/05', '1/7/06' union
select 3, '11/8/05', '1/12/06' union
select 3, '11/17/05', '1/23/06' union
select 3, '12/1/05', '2/1/06' union
select 4, '11/5/05', '1/3/06' union
select 4, '11/9/05', '1/7/06' union
select 4, '11/19/05', '1/14/06' union
select 4, '11/23/05', '1/25/06' union
select 5, '11/5/05', '1/3/06' union
select 5, '11/9/05', '1/7/06' union
select 5, '11/19/05', '1/25/06' union
select 5, '11/27/05', '1/25/06' union
select 6, '11/5/05', '1/3/06' union
select 6, '11/9/05', '1/7/06' union
select 6, '11/19/05', '1/25/06' union
select 6, '1/5/06', '1/25/06' union
select 6, '11/27/05', '2/1/06'
The goal is to retrieve only the rows where Max(expDate) is 1/25/06, and to
also retrieve rows that contain an expdate of 1/25/06 that is not the
Max(expDate) but these additional rows contain a curdate in the same row as
the expDate of 1/25/06 which is the Max(curDate) for that recID. In this
sample, recID 2 contains 2 rows with expDate of 1/25/06 of which one of thos
e
2 rows contains the Max(curDate) for recID 2, and dittor for recID 6. The
goal is to retrieve rows for recID 1, 4, 5 which contain Max(expDate) of
1/25/06 and also rows for recID 2 and recID6 which contain a Max(curDate) fo
r
the rows that contain an expDate of 1/25/06.
What I did was to modify Roy's query as follows:
SELECT recid, Max(curDate) curDate, MAX(expDate) expDate
FROM #temp
GROUP BY recid
HAVING MAX(expDate)='1/25/06'
UNION ALL
select recID, Max(curDate) m, expdate from --I added this line
(SELECT * FROM #temp as X
WHERE
recid NOT IN --the list of recid from beginning
(SELECT recid FROM #temp GROUP BY recid
HAVING MAX(expDate)='1/25/06')
AND
expDate = '1/25/06'
AND curDate >
(select curDate from #temp as Y
where X.recid = Y.recid
and Y.expDate =
(select max(expDate) from #temp as Z
where X.recid = Z.recid))) t1
Group By recid, expdate
--this gives
recID curDate expDate
1 2005-11-28 00:00:00.000 2006-01-25 00:00:00.000
4 2005-11-23 00:00:00.000 2006-01-25 00:00:00.000
5 2005-11-27 00:00:00.000 2006-01-25 00:00:00.000
2 2006-01-08 00:00:00.000 2006-01-25 00:00:00.000
6 2006-01-05 00:00:00.000 2006-01-25 00:00:00.000
"Rich" wrote:
> I must be trying too hard. Your query works fine with the additional new
row
> for recID 2. Sorry bout that.
> "Rich" wrote:
>|||This gives the same result:
select recid,max(curDate) curDate, expDate
from #temp
where expDate='1/25/06'
group by recid,expDate
-oj
"Rich" <Rich@.discussions.microsoft.com> wrote in message
news:7634C35E-CD6B-4E14-8D6E-7DBAE3861BAC@.microsoft.com...
> so, taking this to the nth level (for posterity incase I need to refer to
> this again), I added yet one more row to recID 2. This additional row is
> a
> 2nd expDate of 1/25/06. So now recID 2 has 2 rows with expdate of
> 1/25/06.
> One of these rows contains a Max(curDate) for recID 2.
>
> create table #temp(
> recID int, curDate datetime, expDate datetime)
> insert into #temp
> select 1, '11/1/05', '1/5/06' union
> select 1, '11/5/05', '1/13/06' union
> select 1, '11/12/05', '1/25/06' union
> select 1, '11/28/05', '1/25/06' union
> select 2, '11/7/05', '1/7/06' union
> select 2, '11/13/05', '1/12/06' union
> select 2, '11/27/05', '1/15/06' union
> select 2, '1/7/06', '1/25/06' union
> select 2, '1/8/06', '1/25/06' union
> select 2, '12/1/05', '2/1/06' union
> select 3, '11/3/05', '1/7/06' union
> select 3, '11/8/05', '1/12/06' union
> select 3, '11/17/05', '1/23/06' union
> select 3, '12/1/05', '2/1/06' union
> select 4, '11/5/05', '1/3/06' union
> select 4, '11/9/05', '1/7/06' union
> select 4, '11/19/05', '1/14/06' union
> select 4, '11/23/05', '1/25/06' union
> select 5, '11/5/05', '1/3/06' union
> select 5, '11/9/05', '1/7/06' union
> select 5, '11/19/05', '1/25/06' union
> select 5, '11/27/05', '1/25/06' union
> select 6, '11/5/05', '1/3/06' union
> select 6, '11/9/05', '1/7/06' union
> select 6, '11/19/05', '1/25/06' union
> select 6, '1/5/06', '1/25/06' union
> select 6, '11/27/05', '2/1/06'
>
> The goal is to retrieve only the rows where Max(expDate) is 1/25/06, and
> to
> also retrieve rows that contain an expdate of 1/25/06 that is not the
> Max(expDate) but these additional rows contain a curdate in the same row
> as
> the expDate of 1/25/06 which is the Max(curDate) for that recID. In this
> sample, recID 2 contains 2 rows with expDate of 1/25/06 of which one of
> those
> 2 rows contains the Max(curDate) for recID 2, and dittor for recID 6. The
> goal is to retrieve rows for recID 1, 4, 5 which contain Max(expDate) of
> 1/25/06 and also rows for recID 2 and recID6 which contain a Max(curDate)
> for
> the rows that contain an expDate of 1/25/06.
> What I did was to modify Roy's query as follows:
> SELECT recid, Max(curDate) curDate, MAX(expDate) expDate
> FROM #temp
> GROUP BY recid
> HAVING MAX(expDate)='1/25/06'
> UNION ALL
> select recID, Max(curDate) m, expdate from --I added this line
> (SELECT * FROM #temp as X
> WHERE
> recid NOT IN --the list of recid from beginning
> (SELECT recid FROM #temp GROUP BY recid
> HAVING MAX(expDate)='1/25/06')
> AND
> expDate = '1/25/06'
> AND curDate >
> (select curDate from #temp as Y
> where X.recid = Y.recid
> and Y.expDate =
> (select max(expDate) from #temp as Z
> where X.recid = Z.recid))) t1
> Group By recid, expdate
> --this gives
> recID curDate expDate
> 1 2005-11-28 00:00:00.000 2006-01-25 00:00:00.000
> 4 2005-11-23 00:00:00.000 2006-01-25 00:00:00.000
> 5 2005-11-27 00:00:00.000 2006-01-25 00:00:00.000
> 2 2006-01-08 00:00:00.000 2006-01-25 00:00:00.000
> 6 2006-01-05 00:00:00.000 2006-01-25 00:00:00.000
>
> "Rich" wrote:
>|||On Thu, 2 Mar 2006 22:56:26 -0800, Rich
<Rich@.discussions.microsoft.com> wrote:

>so, taking this to the nth level (for posterity incase I need to refer to
>this again)....
Sounds like you have things well in hand. (I really don't have time
to get into the logic any further.) My only thought, in light of oj's
reply, is to add more variety to the test data. Make it a LOT more
complicated. I'm sure you know about the problem of programmers only
testing part of their logic because they use the application the say
they think it should be used. Well, us DB folks can suffer from the
same problem, testing with test cases that are only a subset of what
the real world might hit us with.
Good luck!
Roy Harvey
Beacon Falls, CT|||Thanks all for your replies. OJ's solution is less verbose than the one I
was working with, but at least we all got the idea what I was trying to do -
at least I am starting to get the idea. And Roy is correct on making the
data more complex to cover more twists. Believe me, the actual data is
quite a bit more complex, but at least now I am chiseling down the queries
that I need. Start out with verbose till I understand what I am looking for
and then drill down to the less verbose.
"Rich" wrote:
> so, taking this to the nth level (for posterity incase I need to refer to
> this again), I added yet one more row to recID 2. This additional row is
a
> 2nd expDate of 1/25/06. So now recID 2 has 2 rows with expdate of 1/25/06
.
> One of these rows contains a Max(curDate) for recID 2.
>
> create table #temp(
> recID int, curDate datetime, expDate datetime)
> insert into #temp
> select 1, '11/1/05', '1/5/06' union
> select 1, '11/5/05', '1/13/06' union
> select 1, '11/12/05', '1/25/06' union
> select 1, '11/28/05', '1/25/06' union
> select 2, '11/7/05', '1/7/06' union
> select 2, '11/13/05', '1/12/06' union
> select 2, '11/27/05', '1/15/06' union
> select 2, '1/7/06', '1/25/06' union
> select 2, '1/8/06', '1/25/06' union
> select 2, '12/1/05', '2/1/06' union
> select 3, '11/3/05', '1/7/06' union
> select 3, '11/8/05', '1/12/06' union
> select 3, '11/17/05', '1/23/06' union
> select 3, '12/1/05', '2/1/06' union
> select 4, '11/5/05', '1/3/06' union
> select 4, '11/9/05', '1/7/06' union
> select 4, '11/19/05', '1/14/06' union
> select 4, '11/23/05', '1/25/06' union
> select 5, '11/5/05', '1/3/06' union
> select 5, '11/9/05', '1/7/06' union
> select 5, '11/19/05', '1/25/06' union
> select 5, '11/27/05', '1/25/06' union
> select 6, '11/5/05', '1/3/06' union
> select 6, '11/9/05', '1/7/06' union
> select 6, '11/19/05', '1/25/06' union
> select 6, '1/5/06', '1/25/06' union
> select 6, '11/27/05', '2/1/06'
>
> The goal is to retrieve only the rows where Max(expDate) is 1/25/06, and t
o
> also retrieve rows that contain an expdate of 1/25/06 that is not the
> Max(expDate) but these additional rows contain a curdate in the same row a
s
> the expDate of 1/25/06 which is the Max(curDate) for that recID. In this
> sample, recID 2 contains 2 rows with expDate of 1/25/06 of which one of th
ose
> 2 rows contains the Max(curDate) for recID 2, and dittor for recID 6. The
> goal is to retrieve rows for recID 1, 4, 5 which contain Max(expDate) of
> 1/25/06 and also rows for recID 2 and recID6 which contain a Max(curDate)
for
> the rows that contain an expDate of 1/25/06.
> What I did was to modify Roy's query as follows:
> SELECT recid, Max(curDate) curDate, MAX(expDate) expDate
> FROM #temp
> GROUP BY recid
> HAVING MAX(expDate)='1/25/06'
> UNION ALL
> select recID, Max(curDate) m, expdate from --I added this line
> (SELECT * FROM #temp as X
> WHERE
> recid NOT IN --the list of recid from beginning
> (SELECT recid FROM #temp GROUP BY recid
> HAVING MAX(expDate)='1/25/06')
> AND
> expDate = '1/25/06'
> AND curDate >
> (select curDate from #temp as Y
> where X.recid = Y.recid
> and Y.expDate =
> (select max(expDate) from #temp as Z
> where X.recid = Z.recid))) t1
> Group By recid, expdate
> --this gives
> recID curDate expDate
> 1 2005-11-28 00:00:00.000 2006-01-25 00:00:00.000
> 4 2005-11-23 00:00:00.000 2006-01-25 00:00:00.000
> 5 2005-11-27 00:00:00.000 2006-01-25 00:00:00.000
> 2 2006-01-08 00:00:00.000 2006-01-25 00:00:00.000
> 6 2006-01-05 00:00:00.000 2006-01-25 00:00:00.000
>
> "Rich" wrote:
>|||Well, OJ's less verbose query isn't working on my actual data because I am
picking up rows with expDates that are not Max(expDate) for a given recID
where the curDate is not the Max(curDate) for the given recID and expDate.
I
need to pick up rows where the Max(expDate) is 1/25/06 or if a recID
contains a newer Max(expDate) than 1/25/06, I pick that row/recID up if the
corresponding curDate for the row with expdate = 1/25/06 is greater than th
e
curdate for the row containing the newer expDate for the given recID. So it
looks like I will have to go with the more verbose query. I was hoping it
would be a simple query, but no such luck. Oh well, at least one of the
solutions will work for my data.
"Rich" wrote:
> Thanks all for your replies. OJ's solution is less verbose than the one I
> was working with, but at least we all got the idea what I was trying to do
-
> at least I am starting to get the idea. And Roy is correct on making the
> data more complex to cover more twists. Believe me, the actual data is
> quite a bit more complex, but at least now I am chiseling down the queries
> that I need. Start out with verbose till I understand what I am looking f
or
> and then drill down to the less verbose.
> "Rich" wrote:
>

Wednesday, March 7, 2012

problem with workflow in dts designer...looping?

what im trying to do is:

1) ftp a file from a server to a local directory,

2) check the file for its lastmodified date

3) depending on a constraint, perform a data import.

However, if the file is not modified, this means that the server thats supposed to ftp and update the file hasnt done its job yet, in this case i would like dts to wait a few minutes and then again go to step 1. I want this to repeat for a whole hour until the data has finally imported OR if it hasnt imported at the end of the hour it will send me an email saying it failed.

The closest I have come to this type of functionality in DTS designer is picture 1.

In picture 1 i am using the WAITFOR DELAY '000:02:00' to wait 5 seconds between every step. However To do this would require me to create about 30 iterations to span the whole hour! The activeX portion works fine its just learning the flow in dts designer that is giving me the problems.

It would be so much easier to do what I did in picture 2, but nothing runs.

here are the pics:
http://www.geocities.com/samirrahan/picture1.gif
http://www.geocities.com/samirrahan/picture2.gif

I would appreciate any help you can offer.



Thank you.I would do it differently...

Each time I successfully import a file (eg I have found a new file and processed it) I would set the next run date.

I would then set a job up to run the dts package every X minutes (5 for example) between the hours that you expect the file to turn up.

The first step of the package would be to check if it is the run date is less then or equal to today, if it is then you continue processing, if not then you halt processing.

This will solve your problem. Yes, it will mean that the package will run more often then it technically needs to, but it will only do the actual processing once.

HTH|||here is the link to my workflow pictures:
http://www.geocities.com/samirrahan/index.html

ideally i would want the dts to stop running as soon as the file import is successful. i guess i could do that on a success by rescheduling the job.

by the way on another note? do u use the designer or just a vb exe. itself? can this be alot easier if i dont use the designer?

thanks.|||I use the designer...

There is a method to change the jobs schedule using sql. Using sp_add_jobschedule and sp_delete_jobschedule what you could do is...

step 1, check for new file if fail end DTS Pakcage else step 2

step 2, ftp file

step 3, process file

step 4, execute sql task - sp_delete_jobschedule

step 5, execute sql task - sp_add_jobschedule

Saturday, February 25, 2012

Problem with using BETWEEN for date range...

Hi all -

This problem has been driving me crazy, and I'm hoping the answer is
something stupid I am neglecting to see...

The procedure posted below is part of an Access/SQL database I have
developed. Basically, the user would input a beginning and ending date,
and the query goes and pulls records that meet the following criteria:

1. TradeDate is between beginning date and ending date
2. TradeTime is between beginning date and ending date
3. Trade's Match ID is equal to match IDs returned by the second select
statement - this is part of a ticket processing system, and tickets are
grouped using their match id. So, if one ticket has been updated and
now meets criteria #1 or #2 above, this is supposed to also return any
of the other tickets with the same match ID - so if one ticket in a
group changes, our acct. dept can look at the whole group on their
reports.

Anyway, the query below seems to work, but I am not happy with it. The
problem was I was using the BETWEEN function, and not converting all
the dates to varchar. This worked fine, unless the beginning date and
ending date were the same. For example, if I had a ticket with a
tradedate of 5/3/06 and I ran a beginning date of 5/3/06 and an ending
date of 5/3/06, the ticket should be returned. However, with the
BETWEEN statement, it would return no rows.

I changed the BETWEEN statements to statements like:

tradedate >= beginning date and tradedate <= ending date

but this also returned no rows.

It was only upon converting all the dates to varchar and using the <=
and >= operators that I started getting the results I need.

Can someone tell me why the heck BETWEEN wouldn't work? Tradedate and
Tradetime are both datetime values, and I was bringing in the beginning
and ending date variables in datetime form...is there a problem using
BETWEEN when the first and second variables used are the same?

Any light you can shed on this would be great, because having all these
convert statements and such makes me nervous...I'd rather get between
to work, but I have not been able to in my testing...

Thanks! -Jim

CREATE PROCEDURE dbo.spAcctExport(@.begindate datetime,
@.enddate datetime)
AS SELECT TOP 100 PERCENT dbo.tblTradeAccount.AccountingCode AS
TradeAccount, dbo.tblOrders.TicketNum, dbo.tblOrders.TradeDate,
dbo.tblOrders.SettleDate, NULL AS
ProductionMonth, dbo.tblOrders.RepID, dbo.tblOrders.AcctNum,
dbo.tblAccounts.Shortname, dbo.tblOrders.Quantity,
dbo.TBLCUSIP.Factor, dbo.tblOrders.BuySell,
dbo.tblOrders.CUSIP, dbo.TBLCUSIP.Issuer, dbo.TBLCUSIP.PoolNum,
dbo.TBLCUSIP.Coupon,
dbo.tblOrders.FixAdj, dbo.tblOrders.Price,
dbo.tblOrders.RepSC, '=(H:H*I:I*Q:Q)/100' AS Markup, 'PTMSA' AS
ProdType, dbo.tblOrders.DeskSC,
dbo.tblOrders.RepCarry, '=(H:H*I:I*T:T)/100' AS
DeskMarkup, dbo.tblOrders.MatchID,

'=IF(K:K="B",((I:I*H:H*P:P)/100)*-1,(I:I*H:H*P:P)/100)' AS
TotalPrincipal, dbo.tblOrders.CancelCorrect,
dbo.tblOrders.OriginalTrade,
dbo.tblOrders.TradeTime, dbo.tblOrders.Rep2ID,
dbo.tblOrders.Rep2SC
FROM dbo.tblOrders INNER JOIN
dbo.TBLCUSIP ON dbo.tblOrders.CUSIP =
dbo.TBLCUSIP.CUSIP INNER JOIN
dbo.tblAccounts ON dbo.tblOrders.AcctNum =
dbo.tblAccounts.AcctNum INNER JOIN
dbo.tblTradeAccount ON dbo.tblOrders.TradeAccount
= dbo.tblTradeAccount.TradeAccount
WHERE ((

(dbo.tblOrders.MatchID IS NOT NULL) AND (dbo.tblOrders.MatchID IN
(SELECT MatchID FROM dbo.tblOrders WHERE (

CONVERT(VARCHAR(10),dbo.tblOrders.TradeDate,101) >=
CONVERT(VARCHAR(10), @.begindate,101) AND
CONVERT(VARCHAR(10),DBO.TBLORDERS.TRADEDATE,101) <=
CONVERT(VARCHAR(10), @.enddate,101))

OR (

CONVERT(VARCHAR(10),dbo.tblOrders.TradeTime,101) >=
CONVERT(VARCHAR(10), @.begindate,101) AND
CONVERT(VARCHAR(10),DBO.TBLORDERS.TRADETIME,101) <=
CONVERT(VARCHAR(10), @.enddate,101)))))

OR ((

CONVERT(VARCHAR(10),dbo.tblOrders.TradeDate,101) >=
CONVERT(VARCHAR(10), @.begindate,101) and
CONVERT(VARCHAR(10),DBO.TBLORDERS.TRADEDATE,101) <=
CONVERT(VARCHAR(10), @.enddate,101))

OR (

CONVERT(VARCHAR(10),DBO.TBLORDERS.TradeTime,101) >=
CONVERT(VARCHAR(10), @.begindate,101) AND
CONVERT(VARCHAR(10),DBO.TBLORDERS.TRADETIME,101) <=
CONVERT(VARCHAR(10), @.enddate,101))))

ORDER BY dbo.tblOrders.CancelCorrect,
dbo.tblTradeAccount.AccountingCode, dbo.tblOrders.MatchID,
dbo.tblOrders.BuySell, dbo.tblOrders.TicketNum
GOIn SQL Server, there is not DATE data type or TIME data type -- but
there is a single DATETIME. Quit doing all that converting and work
with temporal expressions instead of COBOL-style strings. Think in
abstract terms, not how it is displayed on a piece of paper or a
screen.

WHERE trade_time BETWEEN start_time AND end_time

will work fine, if you bother to add constraints to start_time (i.e
00:00:00) and end_time (23:59:59.9999) columns. Another trick is to
allow end_time to be NULL when an event is still in progress and then
use COALESCE (end_time, CURRENT_TIMESTAMP)

If you look up Rick Snodgrass at University of Arizona, you can
download his out-of-print book on temporal SQL queries.|||OK - my post mentioned I originally used

WHERE tradedate BETWEEN begindate AND enddate

Begindate and enddate are not columns - they are parameters the user
supplies in the format mm/dd/yyyy.

My whole post was explaining that I would *prefer* to use the BETWEEN
AND statement - the converting was not my choice, but it is the only
way this query works when the user specifices the same date as
begindate and enddate.

So once again - can someone explain why this query returns no records
when begindate and enddate are specified as 5/3/2006 and there is
clearly trade data with that tradetime?|||My assumption is that the tradedate is a datetime datatype that
includes a time, while the begin and end dates are just dates. The
comparison is will be done on datetimes, which include both date and
time.

>WHERE tradedate BETWEEN begindate AND enddate

Consider what this can look like when we substitute actual values.

WHERE
'2006-05-03 17:12:51.420' BETWEEN
'2006-05-03 00:00:00.000' AND
'2006-05-03 00:00:00.000'

The time that is part of tradedate is putting it outside of the
"range" of the BETWEEN; in this case of course the BETWEEN has no
range, being two identical points in time. To match it needs a 24
hour range, but that is not what it has.

To use BETWEEN you would need toto reduce tradedate to a simple date
with no time for the test.

Roy Harvey
Beacon Falls, CT

On 3 May 2006 13:40:16 -0700, "Jim Armstrong"
<armstrongjc@.hotmail.com> wrote:

>OK - my post mentioned I originally used
>WHERE tradedate BETWEEN begindate AND enddate
>Begindate and enddate are not columns - they are parameters the user
>supplies in the format mm/dd/yyyy.
>My whole post was explaining that I would *prefer* to use the BETWEEN
>AND statement - the converting was not my choice, but it is the only
>way this query works when the user specifices the same date as
>begindate and enddate.
>So once again - can someone explain why this query returns no records
>when begindate and enddate are specified as 5/3/2006 and there is
>clearly trade data with that tradetime?|||Jim Armstrong (armstrongjc@.hotmail.com) writes:
> CONVERT(VARCHAR(10),dbo.tblOrders.TradeDate,101) >=
> CONVERT(VARCHAR(10), @.begindate,101) AND
> CONVERT(VARCHAR(10),DBO.TBLORDERS.TRADEDATE,101) <=
> CONVERT(VARCHAR(10), @.enddate,101))

But this does not make any sense. When I run

select convert(varchar(10), getdate(), 101)

I get

05/03/2006

So if the user enters 2006-05-03, you will also give him hits from
2005?

If you feel compelled to use string format, use format 112 which is
YYYYMMDD, which is possible to compare, and which also is unambiguosly
convertible back to date.

But it would make more sense to simple say:

O.TradeDate BETWEEN @.begindate AND @.enddate

Provided that is, that TradeDate always have a timeportion of midnight.
And the same provision applies to @.begindate and @.endate, but I got
the impression that the user enters date only.

If TradeDate also includes hours and seconds, you need to write:

O.TradeDate >= @.begindate AND
O.TradeDate < dateadd(DAY, 1, @.enddate)

There is one more important thing to observer here: you should avoid
putting TradeDate in an expression. This is because if there is an
index on TradeDate, that index cannot be used if you put TradeDate in
an expression, and performance will suffer.

> CONVERT(VARCHAR(10),dbo.tblOrders.TradeTime,101) >=
> CONVERT(VARCHAR(10), @.begindate,101) AND
> CONVERT(VARCHAR(10),DBO.TBLORDERS.TRADETIME,101) <=
> CONVERT(VARCHAR(10), @.enddate,101)))))

The same applies here, although I'm not really clear what is in TradeTime.
Is TradeDate 2006-05-03 00:00:00 and TradeTime is 2006-05-03 12:23:23?

In such case, I'm not sure why you need to check both.

> CONVERT(VARCHAR(10),dbo.tblOrders.TradeDate,101) >=
> CONVERT(VARCHAR(10), @.begindate,101) and
> CONVERT(VARCHAR(10),DBO.TBLORDERS.TRADEDATE,101) <=
> CONVERT(VARCHAR(10), @.enddate,101))
> OR (
> CONVERT(VARCHAR(10),DBO.TBLORDERS.TradeTime,101) >=
> CONVERT(VARCHAR(10), @.begindate,101) AND
> CONVERT(VARCHAR(10),DBO.TBLORDERS.TRADETIME,101) <=
> CONVERT(VARCHAR(10), @.enddate,101))))

And then you have it once more?

--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx|||>> So once again - can someone explain why this query returns no records [sic] when begindate and enddate are specified as 5/3/2006 and there is clearly trade data with that tradetime? <<

Once again, because there is a DATETIME column which has a time on it.
If you do not give the time, it defautls to 00:00:00.000 Hrs. This is
a single point in time. What you wanted to use was

WHERE tradedate BETWEEN '2006-05-03 00:00:00.000' AND '2006-05-03
23:59:59.99'

You might also want to learn ISO standards and use 'yyyy--mm-dd' for
date values.

And do not forget to add constraints to temporal columns in your base
tables.|||Yes but hang on a minute, what if you don't want a time, holding the time
component is misleading that indicates the data value may contain a time
between 00:00:00 and 23:59:59, being 00:00:00 means the trade was done at
midnight which may well not be true, consider settment date where the trade
needs to settle on a particular day, not by midnight but sometime on that
day.

In SQL Server because we don't have a DATE data type we need to store dates
as an integer in the form yyyymmdd which is unfortunate but the reality.

> You might also want to learn ISO standards and use 'yyyy--mm-dd' for
> date values.

Rich coming from the guy who doesn't use the standard format himself.

2006-05-03 00:00:00.000 should be written 2006-05-03T00:00:00.000

OR

20060503 if you are only specifying the date.

--
Tony Rogerson
SQL Server MVP
http://sqlserverfaq.com - free video tutorials

"--CELKO--" <jcelko212@.earthlink.net> wrote in message
news:1146749926.099160.51660@.i40g2000cwc.googlegro ups.com...
>>> So once again - can someone explain why this query returns no records
>>> [sic] when begindate and enddate are specified as 5/3/2006 and there is
>>> clearly trade data with that tradetime? <<
> Once again, because there is a DATETIME column which has a time on it.
> If you do not give the time, it defautls to 00:00:00.000 Hrs. This is
> a single point in time. What you wanted to use was
> WHERE tradedate BETWEEN '2006-05-03 00:00:00.000' AND '2006-05-03
> 23:59:59.99'
> You might also want to learn ISO standards and use 'yyyy--mm-dd' for
> date values.
> And do not forget to add constraints to temporal columns in your base
> tables.|||Tony Rogerson (tonyrogerson@.sqlserverfaq.com) writes:
> Yes but hang on a minute, what if you don't want a time, holding the
> time component is misleading that indicates the data value may contain a
> time between 00:00:00 and 23:59:59, being 00:00:00 means the trade was
> done at midnight which may well not be true, consider settment date
> where the trade needs to settle on a particular day, not by midnight but
> sometime on that day.
> In SQL Server because we don't have a DATE data type we need to store
> dates as an integer in the form yyyymmdd which is unfortunate but the
> reality.

Only if you like to throw out babies and bathtubs simultaneously.

All our settledate columns are datetime, or more precisely aba_date,
which is a datetime with a rule bound to that screams blue murder if
you try to use anything else than 00:00:00.000 for the time porttion.

And, no, our customers do not settle at midnight.

--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx|||> All our settledate columns are datetime, or more precisely aba_date,
> which is a datetime with a rule bound to that screams blue murder if
> you try to use anything else than 00:00:00.000 for the time porttion.
> And, no, our customers do not settle at midnight.

Presumeably you also have a computed column that strips away the time
component so only the date gets passed back to the client?

To quote celko, using datetime to hold a pure date is really a kludge ;),
seriously though, even though you have a check constraint to prevent times
over than midnight, it shouldn't have a time at all and any query result
should not contain a time component, it ensinuates that settment date is not
a date but actually a date with a time, so to the application developer or
third party vendor accessing your webservice etc... it might well appear
that trades should be settled by midnight.

All the trading systems I've worked with ALL use the integer data type to
hold pure dates.

--
Tony Rogerson
SQL Server MVP
http://sqlserverfaq.com - free video tutorials

"Erland Sommarskog" <esquel@.sommarskog.se> wrote in message
news:Xns97B9F36F1FE35Yazorman@.127.0.0.1...
> Tony Rogerson (tonyrogerson@.sqlserverfaq.com) writes:
>> Yes but hang on a minute, what if you don't want a time, holding the
>> time component is misleading that indicates the data value may contain a
>> time between 00:00:00 and 23:59:59, being 00:00:00 means the trade was
>> done at midnight which may well not be true, consider settment date
>> where the trade needs to settle on a particular day, not by midnight but
>> sometime on that day.
>>
>> In SQL Server because we don't have a DATE data type we need to store
>> dates as an integer in the form yyyymmdd which is unfortunate but the
>> reality.
> Only if you like to throw out babies and bathtubs simultaneously.
> All our settledate columns are datetime, or more precisely aba_date,
> which is a datetime with a rule bound to that screams blue murder if
> you try to use anything else than 00:00:00.000 for the time porttion.
> And, no, our customers do not settle at midnight.
>
> --
> Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
> Books Online for SQL Server 2005 at
> http://www.microsoft.com/technet/pr...oads/books.mspx
> Books Online for SQL Server 2000 at
> http://www.microsoft.com/sql/prodin...ions/books.mspx|||Tony Rogerson (tonyrogerson@.sqlserverfaq.com) writes:
> Presumeably you also have a computed column that strips away the time
> component so only the date gets passed back to the client?

Computed column? Hey, this system started its life in SQL Server 4.x. And
you cannot accuse for being too quick to leave SQL 6.5. I think we have
two computed columns in the system.

Yes, the time portion is a bit of noice in Query Analyzer. For the client,
it is a non-issue.

> All the trading systems I've worked with ALL use the integer data type to
> hold pure dates.

One of our competitors seems to go by char(8). (They're not using SQL
Server. Their system runs on AS 400, so I guess it's DB2.) We have taken
over several of their customers and converted their data to our system.
I've seen some completely hilarous dates there.

Using integer or char(8) for dates is in my opinion an even worse solution,
as you run the risk of getting dates like 20060230 and that in the system.
--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx|||> Using integer or char(8) for dates is in my opinion an even worse
> solution,
> as you run the risk of getting dates like 20060230 and that in the system.

Well not really, its the same argument i'd have with a time component of the
datetime, both are fixed with a check constraint.

I've not mentioned the space issue - 4 bytes ve 8 bytes which can amount to
a lot more IO for larger trading systems....

check( ISDATE( 20060230 ) = 1 ) which is simpler then the check constraint
needed for slicing off / checking for the time component!

Tony.

--
Tony Rogerson
SQL Server MVP
http://sqlserverfaq.com - free video tutorials

"Erland Sommarskog" <esquel@.sommarskog.se> wrote in message
news:Xns97BA66AF76B25Yazorman@.127.0.0.1...
> Tony Rogerson (tonyrogerson@.sqlserverfaq.com) writes:
>> Presumeably you also have a computed column that strips away the time
>> component so only the date gets passed back to the client?
> Computed column? Hey, this system started its life in SQL Server 4.x. And
> you cannot accuse for being too quick to leave SQL 6.5. I think we have
> two computed columns in the system.
> Yes, the time portion is a bit of noice in Query Analyzer. For the client,
> it is a non-issue.
>> All the trading systems I've worked with ALL use the integer data type to
>> hold pure dates.
> One of our competitors seems to go by char(8). (They're not using SQL
> Server. Their system runs on AS 400, so I guess it's DB2.) We have taken
> over several of their customers and converted their data to our system.
> I've seen some completely hilarous dates there.
> Using integer or char(8) for dates is in my opinion an even worse
> solution,
> as you run the risk of getting dates like 20060230 and that in the system.
> --
> Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
> Books Online for SQL Server 2005 at
> http://www.microsoft.com/technet/pr...oads/books.mspx
> Books Online for SQL Server 2000 at
> http://www.microsoft.com/sql/prodin...ions/books.mspx|||Tony Rogerson (tonyrogerson@.sqlserverfaq.com) writes:
> I've not mentioned the space issue - 4 bytes ve 8 bytes which can amount
> to a lot more IO for larger trading systems....

So that is an argument that carries some weight.

But integer data for dates is so much messier to deal with, not the
least for the client.

> check( ISDATE( 20060230 ) = 1 ) which is simpler then the check
> constraint needed for slicing off / checking for the time component!

Nah, I would write it as

CREATE RULE aba_date_rule (isdate(ltrim(str(@.x))) = 1

I hate to rely on implicit conversions from integer to string.

(And it is a rule, as I only want it one place.)

--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx|||Erland,

There is another approach, you can number days sequentially, such as
datediff(day, '20000101', your_original_date). In many cases it allows
to use a 2-byte integer, and that may be quite a difference. I use
dateadd to get dates from those integers, but but dates are needed for
display purposes only, not frequently. I also use a calendar table with
the the same integer as PK.|||Make sure @.x is nvarchar then, because thats what ISDATE takes.

> But integer data for dates is so much messier to deal with, not the
> least for the client.

I totally agree, but its more messy have the time component on queries in my
experience.

Having said that i always convert the integer to a smalldatetime when
creating a fact table purely because its easier for the time dimension on my
dimensional model....

--
Tony Rogerson
SQL Server MVP
http://sqlserverfaq.com - free video tutorials

"Erland Sommarskog" <esquel@.sommarskog.se> wrote in message
news:Xns97BAED6B51AAAYazorman@.127.0.0.1...
> Tony Rogerson (tonyrogerson@.sqlserverfaq.com) writes:
>> I've not mentioned the space issue - 4 bytes ve 8 bytes which can amount
>> to a lot more IO for larger trading systems....
> So that is an argument that carries some weight.
> But integer data for dates is so much messier to deal with, not the
> least for the client.
>> check( ISDATE( 20060230 ) = 1 ) which is simpler then the check
>> constraint needed for slicing off / checking for the time component!
> Nah, I would write it as
> CREATE RULE aba_date_rule (isdate(ltrim(str(@.x))) = 1
> I hate to rely on implicit conversions from integer to string.
> (And it is a rule, as I only want it one place.)
> --
> Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
> Books Online for SQL Server 2005 at
> http://www.microsoft.com/technet/pr...oads/books.mspx
> Books Online for SQL Server 2000 at
> http://www.microsoft.com/sql/prodin...ions/books.mspx

Monday, February 20, 2012

Problem with User Defined Function

I wrote a UDF to get a rolling average based upon a date passed to the UDF. The error I get is:

Server: Msg 102, Level 15, State 1, Procedure fn_RollAverage, Line 26
Incorrect syntax near ')'.

SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO

CREATE FUNCTION fn_RollAverage(@.CURVE_DATE AS SMALLDATETIME)
RETURNS @.TBLRESULT TABLE
(US0001M_Index AS FLOAT,
US0003M_INDEX AS FLOAT,
US0006M_INDEX AS FLOAT,
US0012M_INDEX AS FLOAT,
usswap2_index AS FLOAT,
usswap3_index AS FLOAT,
usswap4_index AS FLOAT,
usswap5_index AS FLOAT,
usswap6_index AS FLOAT,
usswap7_index AS FLOAT,
usswap8_index AS FLOAT,
usswap9_index AS FLOAT,
usswap10_index AS FLOAT,
usswap11_index AS FLOAT,
usswap12_index AS FLOAT,
usswap13_index AS FLOAT,
usswap14_index AS FLOAT,
usswap15_index AS FLOAT,
usswap20_index AS FLOAT,
usswap25_index AS FLOAT,
usswap30_index AS FLOAT)


AS BEGIN

DECLARE @.BEG_DATE AS SMALLDATETIME

SET @.BEG_DATE = (@.CURVE_DATE - 30)

INSERT @.TBLRESULT(
US0001M_Index,
US0003M_INDEX,
US0006M_INDEX,
US0012M_INDEX,
usswap2_index,
usswap3_index,
usswap4_index,
usswap5_index,
usswap6_index,
usswap7_index,
usswap8_index,
usswap9_index,
usswap10_index,
usswap11_index,
usswap12_index,
usswap13_index,
usswap14_index,
usswap15_index,
usswap20_index,
usswap25_index,
usswap30_index)

SELECT
AVG(US0001M_Index) AS US0001M_Index,
AVG(US0003M_INDEX) AS US0003M_INDEX,
AVG(US0006M_INDEX) AS US0006M_INDEX,
AVG(US0012M_INDEX) AS US0012M_INDEX,
AVG(usswap2_index) AS usswap2_index,
AVG(usswap3_index) AS usswap3_index,
AVG(usswap4_index) AS usswap4_index,
AVG(usswap5_index) AS usswap5_index,
AVG(usswap6_index) AS usswap6_index,
AVG(usswap7_index) AS usswap7_index,
AVG(usswap8_index) AS usswap8_index,
AVG(usswap9_index) AS usswap9_index,
AVG(usswap10_index) AS usswap10_index,
AVG(usswap11_index) AS usswap11_index,
AVG(usswap12_index) AS usswap12_index,
AVG(usswap13_index) AS usswap13_index,
AVG(usswap14_index) AS usswap14_index,
AVG(usswap15_index) AS usswap15_index,
AVG(usswap20_index) AS usswap20_index,
AVG(usswap25_index) AS usswap25_index,
AVG(usswap30_index) AS usswap30_index

FROM dbo.LiborSwap
WHERE CURVE_DATE BETWEEN @.BEG_DATE AND @.CURVE_DATE

RETURN
END

GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO

DECLARE @.TBLRESULT TABLE
(US0001M_Index FLOAT,
US0003M_INDEX FLOAT,
US0006M_INDEX FLOAT

...)

|||

The " AS " keyword is not valid in the @.TBLRESULT table declaration.

|||Thanks thanks a thousand thanks