Home All Groups Group Topic Archive Search About

Please I'm getting crazy, datetime question.

Author
15 Jul 2005 12:13 PM
Fabri
declare @datacontr datetime
declare @datafine datetime

select
    @datacontr = '20040615',
    @datafine = '20050514'



Assuming I have two dates I would like to query results if these
conditions are satisfied:


1) Year of @datafine must have at least year(getdate()) - 1 value

2) Month of @datafine must be 3 months back among @datafine and @datacontr

i.e: if month @datacontr is June I want result only if @datafine is at
least March

3) Finally I want result only if day(@datafine) < day(@datacontr)


Any help much appreciated.

Best regards.

--
Fabri
-Mandrake  "A Poma', c'hai 'na faccia...!"
-Pomata    "Sì, se ce n'avevo due già stavo all'università... sotto
spirito!"

Author
15 Jul 2005 1:00 PM
Alejandro Mesa
Can you post a couple of examples?


AMB

Show quote
"Fabri" wrote:

> declare @datacontr datetime
> declare @datafine datetime
>
> select
>     @datacontr = '20040615',
>     @datafine = '20050514'
>
>
>
> Assuming I have two dates I would like to query results if these
> conditions are satisfied:
>
>
> 1) Year of @datafine must have at least year(getdate()) - 1 value
>
> 2) Month of @datafine must be 3 months back among @datafine and @datacontr
>
> i.e: if month @datacontr is June I want result only if @datafine is at
> least March
>
> 3) Finally I want result only if day(@datafine) < day(@datacontr)
>
>
> Any help much appreciated.
>
> Best regards.
>
> --
> Fabri
> -Mandrake  "A Poma', c'hai 'na faccia...!"
> -Pomata    "Sì, se ce n'avevo due già stavo all'università... sotto
> spirito!"
>
Author
15 Jul 2005 3:45 PM
JosephPruiett
Here you go.


declare @datacontr datetime
declare @datafine datetime

select
    @datacontr = '20040615',
    @datafine = '20050514' -- change to 20050517 to verify error checking

SELECT CASE WHEN @datafine >= getdate() - 365
               and @datafine >= @datacontr - 90
               and day(@datafine) < day(@datacontr)
       THEN 'Good'
       ELSE 'Parameters Not Meet'
       END



Show quote
"Fabri" wrote:

> declare @datacontr datetime
> declare @datafine datetime
>
> select
>     @datacontr = '20040615',
>     @datafine = '20050514'
>
>
>
> Assuming I have two dates I would like to query results if these
> conditions are satisfied:
>
>
> 1) Year of @datafine must have at least year(getdate()) - 1 value
>
> 2) Month of @datafine must be 3 months back among @datafine and @datacontr
>
> i.e: if month @datacontr is June I want result only if @datafine is at
> least March
>
> 3) Finally I want result only if day(@datafine) < day(@datacontr)
>
>
> Any help much appreciated.
>
> Best regards.
>
> --
> Fabri
> -Mandrake  "A Poma', c'hai 'na faccia...!"
> -Pomata    "Sì, se ce n'avevo due già stavo all'università... sotto
> spirito!"
>
Author
15 Jul 2005 8:20 PM
Gert-Jan Strik
You almost wrote the construct yourself.

If  YEAR(@datefine) >= YEAR(CURRENT_TIMESTAMP) - 1
And MONTH(@datefine) IN (MONTH(DATEADD(month, -3, @datacontr))
                        ,MONTH(DATEADD(month, -2, @datacontr))
                        ,MONTH(DATEADD(month, -1, @datacontr))
                        ,MONTH(@datacontr)  )
And DAY(@datefine) < DAY(@datecontr)
  print 'Yes!'
Else
  print 'No'


Note that your second requirement is very strange and confusing, so I
wrote a strange solution for that.

I really have the feeling that you are not telling us your *real*
requirements, but only the solution you created for it. Chances are that
you real problem can be solved much simpler than my attempt or Joseph's,
but then you will have to explain the real requirements (and some sample
data and expected results would help too).

Gert-Jan



Fabri wrote:
Show quote
>
> declare @datacontr datetime
> declare @datafine datetime
>
> select
>         @datacontr = '20040615',
>         @datafine = '20050514'
>
> Assuming I have two dates I would like to query results if these
> conditions are satisfied:
>
> 1) Year of @datafine must have at least year(getdate()) - 1 value
>
> 2) Month of @datafine must be 3 months back among @datafine and @datacontr
>
> i.e: if month @datacontr is June I want result only if @datafine is at
> least March
>
> 3) Finally I want result only if day(@datafine) < day(@datacontr)
>
> Any help much appreciated.
>
> Best regards.
>
> --
> Fabri
> -Mandrake  "A Poma', c'hai 'na faccia...!"
> -Pomata    "Sì, se ce n'avevo due già stavo all'università... sotto
> spirito!"

AddThis Social Bookmark Button