2

I wan to convert data in table SQL from int to string or text. I have code to convert it but it does not work.

The data still be int not convert to sting/text

My code

DECLARE @U0 int
SET @U0=1905152

SELECT CAST(@U0 as varchar(10))

screenshot of the output

Can anyone help me?

1
  • 3
    Output is converted to varchar. What exactly is the expected output? Commented Sep 5, 2015 at 7:01

1 Answer 1

1

The type of your select is a varchar. Here is the proof.

USE TEMPDB

DECLARE @U0 int
SET @U0=1905152

SELECT test = CAST(@U0 as varchar(8))
into #temp

SELECT c.name, [type] = t.name, c.max_length, c.[precision], c.scale
  FROM sys.columns AS c
  INNER JOIN sys.types AS t
  ON c.system_type_id = t.system_type_id
  AND c.user_type_id = t.user_type_id
  WHERE c.[object_id] = OBJECT_ID('tempdb..#temp');

Result :

name type    max_length precision scale
---- ------- ---------- --------- -----
test varchar 8          0         0
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.