Question

How do I convert a date to the following format? MM_YYYY with an underscore instead of forward slashes or full stops?

I want to change this query to underscores:

SELECT RIGHT(CONVERT(VARCHAR(10), GETDATE(), 105), 7) AS [MM-YYYY]

Thanks,

Jenny

Was it helpful?

Solution

Use your very same command with a replace:

SELECT replace(RIGHT(CONVERT(VARCHAR(10), GETDATE(), 105), 7),'-','_')

See it here at fiddle: http://sqlfiddle.com/#!3/d41d8/29554

OTHER TIPS

In SQL Server you could do something like

SELECT CONVERT(VARCHAR, DATEPART(MM, GETDATE())) + '_' + CONVERT(VARCHAR, DATEPART(YYYY, GETDATE()))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top