Reputation: 81
For the following query, I have to compare mm part of each @currentdate and @workingdatekey. How to compare only mm part from yyyymmdd format? If the Month of @workingdatekey is same as the month of the @currentdate continue else exit
Declare @currentdate int
set @currentdate = CONVERT(int,CONVERT(varchar(20),GETDATE(),112))
print @currentdate
Declare @workingdatekey int
set @workingdatekey = CONVERT(int,CONVERT(varchar(20),GETDATE()-1,112))
print @workingdatekey
Upvotes: 0
Views: 233
Reputation: 154
Is there a reason you convert those values to a varchar? If not, you can do datepart(mm, "your date column"). That will return the month of the date. You can then do:
if datepart(mm, "currentdatekey") = datepart(mm, "workingdatekey")
print 'They matched'
else
print 'They are different'
Upvotes: 0
Reputation: 69524
IF (MONTH(GETDATE()-1) = MONTH(GETDATE()))
BEGIN
/* DO something here */
END
ELSE
BEGIN
/* Else do something here*/
END
Upvotes: 1