How to find what will be the inbuild datatype of this column - MonthYear once we format Getdate() to 'MMM-yy'
select FORMAT(getdate(), 'MMM-yy') AS MonthYear
How to find what will be the inbuild datatype of this column - MonthYear once we format Getdate() to 'MMM-yy'
select FORMAT(getdate(), 'MMM-yy') AS MonthYear
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Tuhin PaulPosted Jan 30, 2025, 10:53 AM
To determine the inbuilt datatype of the column
MonthYearwhen you formatGETDATE()to'MMM-yy', you can use theSQL_VARIANT_PROPERTYfunction in SQL Server. This function returns the base datatype of a SQL Server expression.Query to Find the Datatype
FORMAT(GETDATE(), 'MMM-yy'):Formats the current date to a string in the format
MMM-yy(e.g.,Oct-23).The
FORMATfunction always returns a string (NVARCHAR).SQL_VARIANT_PROPERTY:Returns metadata about a SQL Server expression.
The
'BaseType'property returns the base datatype of the expression.Result:
The result will be
NVARCHAR, as theFORMATfunction returns a string.Example Output
Why is it NVARCHAR?
The
FORMATfunction in SQL Server always returns a Unicode string (NVARCHAR), regardless of the input.The length of the output depends on the format string. For
'MMM-yy', the length is 6 characters (e.g.,Oct-23).If you want to explicitly define the datatype of the
MonthYearcolumn in a table, you can useNVARCHAR(6)orVARCHAR(6)(if Unicode is not required).Example:
The
FORMATfunction returns a string (NVARCHAR).Use
SQL_VARIANT_PROPERTYto determine the datatype of any SQL Server expression.For
'MMM-yy', the datatype isNVARCHAR(6).Jignesh KumarPosted Jan 30, 2025, 9:08 AM
It will be a is
VARCHAR(5).