Link to home
Create AccountLog in
Avatar of arthurh88
arthurh88

asked on

Quick Help with MSSQL Select comparing dates

I have a table named "users" with these fields:
username
password
expirationdate
hostnames

In my stored procedure I need to verify that the current date is not past the expiration date.  If it is, then it should return "expired"
What chunk of code could I use?  I'm not sure how to compare dates.
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

what about this:

create function dbo.GetUserStatus(@Username varchar(200))
returns varchar(20)
as
begin
  return ( select case when expirationdate < getdate() then 'expired' else 'ok' end from users where username = @username )
end

Avatar of arthurh88
arthurh88

ASKER

Hi there.  I don't really want a function, I'd rather have an inline code inside my SP, I only want to return in case of expired.  I came up with this but I'm getting an error.  A fix to this would be ideal.

DECLARE @currentDateTime datetime
            If @currentdatetime > select expiration  
                  from Users
                  where (username = @username)
                        BEGIN
                        set @responsestring = "Expired"
                        RETURN
                        END
ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
excellent!  

thanks