Convert INT to VARCHAR SQL

前端 未结 6 1887
谎友^
谎友^ 2020-12-04 06:57

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 wi

相关标签:
6条回答
  • 2020-12-04 07:14

    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
    
    0 讨论(0)
  • 2020-12-04 07:16

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

    0 讨论(0)
  • 2020-12-04 07:22

    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

    0 讨论(0)
  • 2020-12-04 07:31

    Actually you don't need to use STR Or Convert. Just select 'xxx'+LTRIM(1) 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
    
    0 讨论(0)
  • 2020-12-04 07:37

    You can use CAST function:

    SELECT CAST(your_column_name AS varchar(10)) FROM your_table_name
    
    0 讨论(0)
  • 2020-12-04 07:38

    Use the convert function.

    SELECT CONVERT(varchar(10), field_name) FROM table_name
    
    0 讨论(0)
提交回复
热议问题