365

I am using Sybase and I am doing a select which returns me a column called "iftype", but its type is int and I need to convert into varchar. When I try to do the select without the convert function I get this error:

Error code 257, SQL state 37000: Implicit conversion from datatype 'VARCHAR' to 'INT' is not allowed. Use the CONVERT function to run this query.

I dont know how to implement the function CONVERT. Can anyone help me, please ?

potashin
  • 42,140
  • 11
  • 76
  • 100
Murilo
  • 4,081
  • 5
  • 17
  • 25

6 Answers6

624

Use the convert function.

SELECT CONVERT(varchar(10), field_name) FROM table_name
Taylor Brown
  • 1,480
  • 1
  • 16
  • 29
Tobberoth
  • 8,570
  • 2
  • 17
  • 17
  • 8
    According to the error, it's `VARCHAR` to `INT` but I'm answering his question, not the error message. – Tobberoth Nov 14 '13 at 14:07
  • 2
    Thanks. But now I got another error. When I try to do this kind of Select : SELECT CONVERT(varchar(10), field_name) FROM table_name. Or even the normal one like: SELECT field_name FROM table_name. Both are correct. I dont know why. But when I try to but a "where" at the end of the select, using the convert or not, I get the same error: Implicit conversion from datatype 'VARCHAR' to 'INT' is not allowed. Use the CONVERT function to run this query – Murilo Nov 14 '13 at 14:09
  • 3
    @Murilo That's because the error is not from what you think. The error tells you that your code is trying to use a `varchar` where an `int` is needed. You need write your actual SQL statement for us to help you. – Tobberoth Nov 14 '13 at 14:10
  • Sorry guys. I found the error. In a part of the "where" I was trying to do id = '4' and the id`s type is int and not varchar. Sorry. And thanks for helping me ! – Murilo Nov 14 '13 at 14:13
  • 14
    @Tobberoth, for what it's worth, I just landed here by googling. Part of SO's value is getting answers to almost any question, even basic ones. – KyleMit Apr 08 '14 at 19:58
  • 7
    I think it should be `varchar(11)` in case the number is a large negative number. – Trisped Aug 25 '14 at 22:36
  • this just solved a huge bug on my code, the 200 users using it thank you. – dizad87 Mar 29 '18 at 02:03
113

Use the STR function:

SELECT STR(field_name) FROM table_name

Arguments

float_expression

Is an expression of approximate numeric (float) data type with a decimal point.

length

Is the total length. This includes decimal point, sign, digits, and spaces. The default is 10.

decimal

Is the number of places to the right of the decimal point. decimal must be less than or equal to 16. If decimal is more than 16 then the result is truncated to sixteen places to the right of the decimal point.

source: https://msdn.microsoft.com/en-us/library/ms189527.aspx

Mahdi
  • 1,646
  • 19
  • 30
Rei Salazar
  • 91
  • 1
  • 3
  • 3
26

You can use CAST function:

SELECT CAST(your_column_name AS varchar(10)) FROM your_table_name
Hamid Heydarian
  • 650
  • 8
  • 15
6

Actually you don't need to use STR Or Convert. Just select 'xxx'+LTRIM(ColumnName) does the job. Possibly, LTRIM uses Convert or STR under the hood.

LTRIM also removes need for providing length and usually default 10 is good enough for integer to string conversion.

SELECT LTRIM(ColumnName) FROM TableName
PAS
  • 445
  • 4
  • 12
0

CONVERT(DATA_TYPE , Your_Column) is the syntax for CONVERT method in SQL. From this convert function we can convert the data of the Column which is on the right side of the comma (,) to the data type in the left side of the comma (,) Please see below example.

SELECT CONVERT (VARCHAR(10), ColumnName) FROM TableName
Tharuka
  • 139
  • 5
0

SELECT cast(CAST([field_name] AS bigint) as nvarchar(255)) FROM table_name

Zahid Hasan
  • 235
  • 2
  • 5