How to check 2 variable values in if condition using vbscript ?

snehalj 0 Tallied Votes 580 Views Share

above code not working, y is 2010 and year0 is also 2010, as per my knowledge Equal should be displayed as output, but the condition does not get equal.
Pls help me for this.

year1 =rs.fields("FinYear") 
year0 = Mid(year1,1,4)'first year
year1= Mid(year1,5,4)'second year 
y = Year(Now())
if y = year0 then 
Response.Write "Equal"
Response.End 						
end if

Dani AI

Generated

Brief summary: the symptom (both sides printing as "2010" but the equality failing) usually comes from a formatting or type mismatch rather than a logic bug. reached the correct solution path by normalizing types; 's point about pulling the year from a true date is also a good alternative if your source can store a date instead of a concatenated string.

Quick, reliable pattern to use when the DB gives a concatenated text like "20102011": normalize the raw value, strip invisible characters, take the first four characters and compare as a number. Example approach:

raw = rs.Fields("FinYear")
If Not IsNull(raw) Then
  raw = Replace(raw, vbCr, "")
  raw = Replace(raw, vbLf, "")
  raw = Replace(raw, vbTab, "")
  raw = Replace(raw, Chr(160), " ")
  raw = Trim(raw)

  firstYearText = Left(raw, 4)
  If IsNumeric(firstYearText) Then
    If CLng(firstYearText) = Year(Date) Then Response.Write "Equal"
  End If
End If

Troubleshooting tips if the problem resurfaces: output the raw string and its length to spot hidden characters (for example: print the value in quotes and Len()), look for Chr(160) (non-breaking space) or vbNullChar, and use Replace to remove them before comparing. Always guard numeric casts with IsNumeric to avoid runtime errors.

Best practices: use Option Explicit and explicit conversions, store years as numeric fields or two separate year columns (or a proper date) so you avoid string parsing, and validate/clean input on write so reads are predictable.

Baradaran 1 Junior Poster in Training

Hi,
what is rs.fields("FinYear")? Is it a date? Is it the year?

I don't understand why you are using mid. If you want to extract the year from a date, simply use datepart, as following:

Response.Write DatePart("YYYY", Date())

This would result in 2010.

If this is not what you want, please let me know the value of rs.fields("FinYear").

snehalj 0 Light Poster

hi
sorry i forget to write that,
"20102011" is get stored in FinYear or any other year in this format

year1 =rs.fields("FinYear") 
year0 = Mid(year1,1,4)'first year
year1= Mid(year1,5,4)'second year 
mon =rs.fields("iMonth")
y = Year(Now())
m = Month(Now())
if  cStr(y)= year0 then

the problem is solved using cStr function.
n by using Mid function "20102011" gets seperated as 2010 and 2011

Thank u

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.