user3369060
user3369060

Reputation: 81

Datepart in store procedure

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

Answers (2)

b.runyon
b.runyon

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

M.Ali
M.Ali

Reputation: 69524

IF (MONTH(GETDATE()-1) = MONTH(GETDATE()))
 BEGIN
   /* DO something here */
 END
ELSE
 BEGIN
  /* Else do something here*/
 END

Upvotes: 1

Related Questions