Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Validation

Author  Topic 

sent_sara
Constraint Violating Yak Guru

377 Posts

Posted - 2008-09-22 : 04:38:59
if the given input is ytd or
for a month logic should work else error to be handled

can any one correct the below code:


Declare @st_date varchar(20)
Declare @end_date varchar(20)
set @st_date='01 apr 2008'
set @end_date='31 mar 2009'

--select month('31 mar 2009')

if
(month(convert(datetime,@st_date)))<>(month(convert(datetime,@end_date))) or
(month(convert(datetime,@st_date))<> 4 and month(convert(datetime,@end_date))<>3)
print 'Not correct range'
else
print 'correct range'

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-09-22 : 04:52:24
why are you using varchar fields to store date values? you can use datetime fields and avoid explicit convertion.
Go to Top of Page

sent_sara
Constraint Violating Yak Guru

377 Posts

Posted - 2008-09-22 : 04:55:21
aS OF now our product accepts only varchar so that purpose only..

quote:
Originally posted by visakh16

why are you using varchar fields to store date values? you can use datetime fields and avoid explicit convertion.

Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-09-22 : 04:56:13
IF DATEDIFF(MONTH, @st_date, @end_date) = 0 AND DATEPART(MONTH, @end_date) IN (3, 4)
PRINT 'Ok'
ELSE
PRINT 'Error'



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-09-22 : 04:57:26
IF DATEDIFF(MONTH, @st_date, @end_date) = 0 AND DATEPART(MONTH, @end_date) <= DATEPART(MONTH, GETDATE())
PRINT 'Ok'
ELSE
PRINT 'Error'



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

sent_sara
Constraint Violating Yak Guru

377 Posts

Posted - 2008-09-22 : 05:54:02
hi peso,
txs for your reply
Actually the input should be standard.either for a month (ie)
01 apr 2008 to 30 apr 2008 (or) 01 may 2008 To 31 may 2008,etc..
orelse
01 apr 2008 To 31 mar 2009 (std for a year)

Any condtn fails above should throw a error..

quote:
Originally posted by Peso

IF DATEDIFF(MONTH, @st_date, @end_date) = 0 AND DATEPART(MONTH, @end_date) <= DATEPART(MONTH, GETDATE())
PRINT 'Ok'
ELSE
PRINT 'Error'



E 12°55'05.63"
N 56°04'39.26"


Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-09-22 : 05:58:58
quote:
Originally posted by sent_sara

hi peso,
txs for your reply
Actually the input should be standard.either for a month (ie)
01 apr 2008 to 30 apr 2008 (or) 01 may 2008 To 31 may 2008,etc..
orelse
01 apr 2008 To 31 mar 2009 (std for a year)

Any condtn fails above should throw a error..

quote:
Originally posted by Peso

IF DATEDIFF(MONTH, @st_date, @end_date) = 0 AND DATEPART(MONTH, @end_date) <= DATEPART(MONTH, GETDATE())
PRINT 'Ok'
ELSE
PRINT 'Error'



E 12°55'05.63"
N 56°04'39.26"





IF DATEADD(dd,-1,DATEADD(mm,DATEDIFF(mm,0,@Start)+1,0))=@end_date
OR DATEADD(dd,-1,DATEADD(yy,DATEDIFF(yy,0,@Start)+1,0))=@end_date
PRINT 'Ok'
ELSE
PRINT 'Error'

Go to Top of Page
   

- Advertisement -