쿠키 기본 설정 선택

당사는 사이트와 서비스를 제공하는 데 필요한 필수 쿠키 및 유사한 도구를 사용합니다. 고객이 사이트를 어떻게 사용하는지 파악하고 개선할 수 있도록 성능 쿠키를 사용해 익명의 통계를 수집합니다. 필수 쿠키는 비활성화할 수 없지만 '사용자 지정' 또는 ‘거부’를 클릭하여 성능 쿠키를 거부할 수 있습니다.

사용자가 동의하는 경우 AWS와 승인된 제3자도 쿠키를 사용하여 유용한 사이트 기능을 제공하고, 사용자의 기본 설정을 기억하고, 관련 광고를 비롯한 관련 콘텐츠를 표시합니다. 필수가 아닌 모든 쿠키를 수락하거나 거부하려면 ‘수락’ 또는 ‘거부’를 클릭하세요. 더 자세한 내용을 선택하려면 ‘사용자 정의’를 클릭하세요.

SQL Server cast and convert for T-SQL

포커스 모드
SQL Server cast and convert for T-SQL - SQL Server to Aurora PostgreSQL Migration Playbook
이 페이지는 귀하의 언어로 번역되지 않았습니다. 번역 요청

This topic provides reference information about data type conversion and casting in Amazon Aurora PostgreSQL compared to Microsoft SQL Server. You can understand the similarities and differences between the CAST and CONVERT functions in both database systems. The topic explains how Aurora PostgreSQL supports the CAST function similarly to SQL Server, while also offering additional flexibility through custom casts and the CREATE CAST command.

Feature compatibility AWS SCT / AWS DMS automation level AWS SCT action code index Key differences

Three star feature compatibility

Four star automation level

N/A

CONVERT is used only to convert between collations. CAST uses different syntax.

SQL Server Usage

The CAST and CONVERT functions are commonly used to convert one data type to another. CAST and CONVERT behave mostly the same and share the same topic in MSDN. They have the following differences:

  • CAST is part of the ANSI-SQL specification, but CONVERT isn’t.

  • CONVERT accepts an optional style parameter used for formatting.

For more information, see Date and Time styles in the SQL Server documentation.

Conversion Matrix

For a list of available conversion data types, see Implicit conversions in the SQL Server documentation.

Syntax

-- CAST Syntax:
CAST ( expression AS data_type [ ( length ) ] )

-- CONVERT Syntax:
CONVERT ( data_type [ ( length ) ] , expression [ , style ] )

Examples

The following example casts a string to int and int to decimal.

SELECT CAST('23.7' AS varchar) AS int, CAST(23.7 AS int) AS decimal;

The following example converts string to int and int to decimal.

SELECT CONVERT(VARCHAR, '23.7') AS int, CONVERT(int, 23.7) AS decimal;

For these two preceding examples, the result looks as shown following.

int   decimal
23.7  23

The following example converts a date with option style input (109 - mon dd yyyy hh:mi:ss:mmmAM (or PM)).

SELECT CONVERT(nvarchar(30), GETDATE(), 109);

Jul 25 2018 5:20:10.8975085PM

For more information, see CAST and CONVERT (Transact-SQL) in the SQL Server documentation.

PostgreSQL Usage

Amazon Aurora PostgreSQL-Compatible Edition (Aurora PostgreSQL) provides the same CAST function as SQL Server for conversion between data types. It also provides a CONVERSION function, but it isn’t equivalent to SQL Server CONVERT. PostgreSQL CONVERSION is used to convert between character set encoding.

CREATE A CAST defines a new cast on how to convert between two data types.

Cast can be EXPLICITLY or IMPLICIT.

The behavior is similar to SQL Server’s casting, but in PostgreSQL, you can also create your own casts to change the default behavior. For example, checking if a string is a valid credit card number by creating the CAST with the WITHOUT FUNCTION clause.

CREATE CONVERSION is used to convert between encoding such as UTF8 and LATIN. If CONVERT is currently in use in SQL Server code, rewrite it to use CAST instead.

Note

Not all SQL Server data types are supported on Aurora PostgreSQL, besides changing the CAST or CONVERT commands, you might need to also change the source of the target data type. For more information, see Data Types.

Another way to convert between data types in PostgreSQL will be to use the :: characters. This option is useful and can make your PL/pgSQL code look cleaner and simpler, see the following examples.

Syntax

CREATE CAST (source_type AS target_type)
WITH FUNCTION function_name (argument_type [, ...]) [ AS ASSIGNMENT | AS IMPLICIT ]

CREATE CAST (source_type AS target_type)
WITHOUT FUNCTION [ AS ASSIGNMENT | AS IMPLICIT ]

CREATE CAST (source_type AS target_type)
WITH INOUT [ AS ASSIGNMENT | AS IMPLICIT ]

Examples

The following example converts a numeric value to float.

SELECT 23 + 2.0;

or

SELECT CAST ( 23 AS numeric ) + 2.0;

The following example converts a date with format input ('mon dd yyyy hh:mi:ss:mmmAM (or PM)').

SELECT TO_CHAR(NOW(),'Mon DD YYYY HH:MI:SS:MSAM');

Jul 25 2018 5:20:10.8975085PM

The following example uses the :: characters.

SELECT '2.35'::DECIMAL + 4.5 AS results;

results
6.85

Summary

Option SQL Server Aurora PostgreSQL

Explicit CAST

SELECT CAST('23.7' AS varchar) AS int

SELECT CAST('23.7' AS varchar) AS int

Explicit CONVERT

SELECT CONVERT (VARCHAR, '23.7')

Need to use CAST:

SELECT CAST('23.7' AS varchar) AS int

Implicit casting

SELECT 23 + 2.0 SELECT 23 + 2.0

Convert to a specific date format: 'mon dd yyyy hh:mi:ss:mmmAM'

SELECT CONVERT(nvarchar (30), GETDATE(), 109)

SELECT TO_CHAR(NOW(),'Mon DD YYYY HH:MI:SS:MSAM')

For more information, see CREATE CAST, Type Conversion, and CREATE CONVERSION in the PostgreSQL documentation.

이 페이지에서

프라이버시사이트 이용 약관쿠키 기본 설정
© 2025, Amazon Web Services, Inc. 또는 계열사. All rights reserved.