Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Text To Varchar

Tags:

postgresql

In Postgresql how do you convert a text field to a varchar? I have tried both of the below, but neither convert my text field to varchar.

Cast(Iamtextfield As Varchar)
Char(Iamtextfield)
like image 827
PinkSmelly BlueSocks Avatar asked Aug 12 '16 12:08

PinkSmelly BlueSocks


People also ask

Can I change varchar to text MySQL?

There is no a big problem to change varchar to text because text supports more data length than varchar , but if the field has a index it must be drop and create new index with prefix col_name(length) (see CREATE INDEX syntax).

How do I convert a text to number in SQL?

TO_NUMBER converts a string to a number of data type NUMERIC. TO_CHAR performs the reverse operation; it converts a number to a string. CAST and CONVERT can be used to convert a string to a number of any data type. For example, you can convert a string to a number of data type INTEGER.

Is varchar a string?

VARCHAR is a variable length string data type, so it holds only the characters you assign to it. VARCHAR takes up 1 byte per character, + 2 bytes to hold length information.

How do I convert text to date in SQL?

In SQL Server, converting a string to date explicitly can be achieved using CONVERT(). CAST() and PARSE() functions.


1 Answers

text is just another term for unlimited varchar in PostgreSQL.

But if you want to make sure the type is set correctly in the return output, simply:

 iamtextfield::varchar

Or if it is case sensitive

 "Iamtextfield"::varchar

If you want to truncate you can do something like:

 iamtextfield::varchar(5)
like image 147
Chris Travers Avatar answered Sep 30 '22 19:09

Chris Travers