Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PostgreSQL CAST(x AS typename) or x::typename

Tags:

postgresql

Should I use cast(x as typename) or x::typename in PostgreSQL?

I was not able to find any documentation for x::typename version of cast except one sentence in documentation ("By default, a cast can be invoked only by an explicit cast request, that is an explicit CAST(x AS typename) or x::typename construct."

Is the only difference that cast(x as typename) is in sql-standard while x::typename is not?

I do prefer x::typename all the time as it is shorter.

like image 495
Tomas Greif Avatar asked Dec 05 '25 04:12

Tomas Greif


1 Answers

x::typename is postgresql specific. If you prefer it and it doesn't have to work on non postgresql databases you can use it.

The manual for casting is here. As you will see there is a third style but it is not recommended.

like image 167
Eelke Avatar answered Dec 07 '25 22:12

Eelke



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!