• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 58
  • Last Modified:

query Syntax

I have the following query that pulls distinct filedate from my table. I need to add a column DisplayDate to use in a dropdown so an example would be 1016-09-01 and in my matching column I want Sep 2016


SELECT DISTINCT [filedate]
FROM [dbo].[NC_ProviderInfo]
ORDER BY [filedate] DESC

Open in new window

0
mburk1968
Asked:
mburk1968
  • 2
1 Solution
 
Kelvin SparksCommented:
Try

SELECT DISTINCT [filedate], CONVERT(CHAR(3), DATENAME(MONTH, [filedate]) + ' ' + YEAR([filedate]) as DisplayDate
FROM [dbo].[NC_ProviderInfo]
ORDER BY [filedate] DESC

Kelvin
0
 
Pawan KumarDatabase ExpertCommented:
Try below, Format Function , Works with SQL 2012 & above !!

SELECT DISTINCT [filedate] , FORMAT([filedate], 'MMMM yyyy') DisplayDate
FROM [dbo].[NC_ProviderInfo]
ORDER BY [filedate] DESC

Open in new window

0
 
mburk1968Author Commented:
receiving the following  
Msg 245, Level 16, State 1, Procedure dbo.qry_ActiveMonths, Line 15 [Batch Start Line 0]
Conversion failed when converting the nvarchar value 'July ' to data type int.

SELECT DISTINCT [filedate], CONVERT(CHAR(3), DATENAME(MONTH, [filedate]) + ' ' + YEAR([filedate]) as DisplayDate
FROM [dbo].[NC_ProviderInfo]
ORDER BY [filedate] DESC
0
 
Pawan KumarDatabase ExpertCommented:
Try this... If you are using SQL below SQL 2012.

--

SELECT DISTINCT [filedate] , LEFT(DATENAME(MONTH, [filedate]),3) + ' ' + CAST(YEAR([filedate]) AS VARCHAR(4)) DisplayDate
FROM [dbo].[NC_ProviderInfo]
ORDER BY [filedate] DESC

--

Open in new window

0

Featured Post

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now