Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6959153
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:14:06+00:00 2026-05-27T15:14:06+00:00

Given: SQL Server Table called TEST_TABLE Column in TEST_TABLE called TEST_FIELD of type VARCHAR(50)

  • 0

Given:

  • SQL Server
  • Table called TEST_TABLE
  • Column in TEST_TABLE called TEST_FIELD of type VARCHAR(50) NOT NULL
  • Row 1: 10YR3/6
  • Row 2: 10YR3/2
  • Query: SELECT TEST_FIELD FROM TEST_TABLE WHERE ...

Question:

In my where condition I need to test for values in the last character of the string. I notice the same behavior doing the following in the Where clause.

  1. RIGHT(TEST_FIELD,1) > 3
  2. CAST(RIGHT(TEST_FIELD,1) AS INT) > 3

Are they behaving the same through some inferred cast in case 1? Is case 1 deterministic?

Thanks in advance.

Matt

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T15:14:07+00:00Added an answer on May 27, 2026 at 3:14 pm

    A conversion is done when you check the value for instance:

    DECLARE @t varchar(100)
    
    SET @t = (SELECT 'ABCA2')
    
    SELECT @t    
    
    IF RIGHT(@t, 2) > 10
      SELECT 'Hi'
    ELSE
      SELECT 'Bye'
    

    Will throw an error because SQL cannot convert A2 to an integer without throwing an error.

    However if you replace @t with:

    SET @t = (SELECT 'ABC12')

    The above code will work as a conversion is successful and a comparison can be made. The right function itself does not convert your value. MSDN states the return type of RIGHT() explicitly:

    Returns varchar when character_expression is a non-Unicode character
    data type.

    Returns nvarchar when character_expression is a Unicode character data
    type.

    To make it easier on yourself, eliminate the function RIGHT() altogether, when a comparison is done with text for instance:

    DECLARE @t varchar(100)
    
    SET @t = (SELECT '1')
    
    SELECT @t
    
    IF @t < 10
      SELECT 'Hi'
    ELSE
      SELECT 'Bye'
    

    Notice I did not make a call to Right(). The result of the above is the display of 1 and then of the text Hi.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a nullable DateTime column in my SQL Server 2005 table called DateTimeDeleted.
I have a SQL Server CE table with a date column. Any given date
Given the following table in SQL Server 2005: ID Col1 Col2 Col3 -- ----
I have a single row of data in a SQL Server table that I'm
I have a SQL Server 2005 table called Users: UserID | Date | Name
I have a table with a column called status of type smallint and accepts
Possible Duplicate: dynamic sql pivot in sql server I have a table called Col_values
I am using SQL Server 2005. I have a table called Product. Now I
In SQL Server 2008 R2 given a table structure of three (or more) one
I have a (Sql Server 2008) table called Courses with course_id,course_name and course_description as

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.