|
database
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Please I'm getting crazy, datetime question.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!" 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!" > 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!" > 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!" |
|||||||||||||||||||||||