Re: SQL Question for Conversion

From: Dan Guzman (guzmanda_at_nospam-online.sbcglobal.net)
Date: 09/10/04


Date: Thu, 9 Sep 2004 20:17:49 -0500

A correction to my previous response:

For example, this query will also return Emp_ID '1040' due to character
comparison rules.

-- 
Hope this helps.
Dan Guzman
SQL Server MVP
"Dan Guzman" <guzmanda@nospam-online.sbcglobal.net> wrote in message 
news:%23Yp5ZItlEHA.3156@TK2MSFTNGP12.phx.gbl...
> Try enclosing the literal in quotes since you are comparing with a 
> varchar. If you specify a numeric integer literal. SQL Server will try to 
> convert the varchar column to an integer because integer has a higher data 
> type precedence than varchar.
>
> SELECT *
> FROM Employees
> WHERE Emp_ID BETWEEN '104' and '110'
>
> Also, note that BETWEEN might not provide the results you expect.  For 
> example, this query will also return Emp_ID '1001' due to character 
> comparison rules.
>
>
> -- 
> Hope this helps.
>
> Dan Guzman
> SQL Server MVP
>
> "Steve" <anonymous@discussions.microsoft.com> wrote in message 
> news:94b001c496d0$86ae7280$a301280a@phx.gbl...
>> Hi,
>> I have a conversion issue in my SQL statement.  I have a
>> table Employees(using example here)
>> Emp_ID is Primary Key & has a data type of varchar
>>
>>
>> Emp_ID    Emp_Dept
>> 100       Acct
>> 101       Acct
>> 102       FIN
>> 103-A     HR
>> 103-B     HR
>> 104       Tax
>> 104-A     Tax
>> 104-B     Tax
>> 105       Acct
>> 106       FIN
>> 107       FIN
>> 108-A     Mgmt
>> 109       Mgmt
>> 110       Mgmt
>>
>> Datatype of Emp_ID is varchar.  I have to write a select
>> statemnt to get all the values for employees who are in
>> between 104 & 100
>>
>> When I wrtie the following
>>
>>
>> Select * from Employees
>> where Emp_ID between 104 and 110
>>
>> I get the error
>>
>> Syntax error converting the varchar value  to a column of
>> data type int.
>>
>> I tried following also
>>
>> Select * from Employees
>> where cast('Emp_ID' as int)  between 104 and 110
>>
>> But I got the error
>>
>> Error converting data type varchar to int.
>>
>> Can someone please help me with above
>>
>> Thanks
>>
>> Steve
>
> 


Relevant Pages

  • Re: SQL Question for Conversion
    ... Try enclosing the literal in quotes since you are comparing with a varchar. ... SQL Server will try to convert the ... FROM Employees ... > Syntax error converting the varchar value to a column of> data type int. ...
    (microsoft.public.sqlserver.programming)
  • Re: Conversion error
    ... You canimport both types of data into a varchar column now. ... Allan Mitchell MCSE,MCDBA, (Microsoft SQL Server MVP) ... www.SQLDTS.com - The site for all your DTS needs. ... >>character data has to be one or the other. ...
    (microsoft.public.sqlserver.dts)
  • Re: MSDE database too big
    ... It seems strange that I have so much data ... MsgId varchar 200 ... >> Tibor Karaszi, SQL Server MVP ... >>> Guoqi Zheng ...
    (microsoft.public.sqlserver.server)
  • Re: Very slow distinct select
    ... > smalldatetime TIME_STAMP ... quite something to win by changing char to varchar. ... Erland Sommarskog, SQL Server MVP, esquel@xxxxxxxxxxxxx ...
    (comp.databases.ms-sqlserver)
  • Re: Maintaining Field Length in .txt format
    ... If the types are VARCHAR, ... converts them to CHAR types. ... >are on a SQL Server 2000 environment and I can create ... The header information for the feed to be ...
    (microsoft.public.sqlserver.programming)