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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Tuhin PaulRank #7 · 71.9K reputationPosted Jan 30, 2025, 5:53 AM EST
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
SELECT SQL_VARIANT_PROPERTY(FORMAT(GETDATE(), 'MMM-yy'), 'BaseType') AS 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:
CREATE TABLE ExampleTable ( MonthYear NVARCHAR(6) );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 KumarRank #91 · 22.9K reputationPosted Jan 30, 2025, 4:08 AM EST
It will be a is
VARCHAR(5).