Michael Traveler
Michael Traveler

Reputation: 25

Variable type conversion for a view | varchar (max) to (250)

Is it possible to convert varchar(max) in a table to varchar(250) in a view?

Id type int
Textxxx type varchar(max)

dbo.Table:

Id | textxxx
—--+------
 1 | aaaa
 2 | bbbb

Query

CREATE VIEW dbo.view
    SELECT ALL [Textxxx] 
    FROM DBO.Table
go

And I want to convert this text (varchar(max)) to varchar(250)

Totally I don't know how to do it, should I use:

CONVERT([Text], varchar(250))

???

Upvotes: 1

Views: 2939

Answers (1)

Gabriele Franco
Gabriele Franco

Reputation: 899

You can use CAST or CONVERT function.

Below you can find the example

CREATE OR ALTER VIEW dbo.ViewsText
AS
    SELECT 
                ID
                ,TextWithCast = CAST(textxxx AS varchar(250)
                ,TextWithConvert = CONVERT (varchar(250), textxxx)
FROM 
         dbo.Table
                

Conversion from large-value data types, such as varchar(max), to a smaller counterpart data type, such as varchar, is an implicit conversion, but truncation occurs if the size of the large value exceeds the specified length of the smaller data type.

Upvotes: 2

Related Questions