CAST Function - SQL Server to PostgreSQL Migration

In SQL Server you can use CAST function to convert an expression to another data type. In PostgreSQL you can also use CAST function but data types can be different.

SQL Server:

  -- Convert number to string
  SELECT CAST(123.45 AS VARCHAR); 
 
  -- Convert string to datetime
  SELECT CAST('2024-10-21 14:54:27.123' AS DATETIME);

PostgreSQL:

  -- Convert number to string
  SELECT CAST(123.45 AS VARCHAR); 
 
  -- Convert string to datetime (TIMESTAMP data type is used)
  SELECT CAST('2024-10-21 14:54:27.123' AS TIMESTAMP(3));

For more information, see SQL Server to PostgreSQL Migration.