Convert INT to VARCHAR SQL

SqlSelectType ConversionSybase

Sql Problem Overview


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 ?

Sql Solutions


Solution 1 - Sql

Use the convert function.

SELECT CONVERT(varchar(10), field_name) FROM table_name

Solution 2 - Sql

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

Solution 3 - Sql

You can use CAST function:

SELECT CAST(your_column_name AS varchar(10)) FROM your_table_name

Solution 4 - Sql

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. It seems to be working for integer or float without worry of truncation.

SELECT LTRIM(ColumnName) FROM TableName

also, LTRIM is better than STR as

SELECT STR(1234567890.123) 

gives 1234567890 whereas

SELECT LTRIM(1234567890.123) 

gives 1234567890.123

Solution 5 - Sql

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

Solution 6 - Sql

SELECT Cast(Cast([field_name] AS BIGINT) AS NVARCHAR(255))
FROM   table_name  

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMuriloView Question on Stackoverflow
Solution 1 - SqlTobberothView Answer on Stackoverflow
Solution 2 - SqlRei SalazarView Answer on Stackoverflow
Solution 3 - SqlHamid HeydarianView Answer on Stackoverflow
Solution 4 - SqlPASView Answer on Stackoverflow
Solution 5 - SqlTharukaView Answer on Stackoverflow
Solution 6 - SqlZahid HasanView Answer on Stackoverflow