?
Solved

query Syntax

Posted on 2016-10-11
4
Medium Priority
?
52 Views
Last Modified: 2016-10-11
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
Comment
Question by:mburk1968
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 22

Expert Comment

by:Kelvin Sparks
ID: 41839406
Try

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

Kelvin
0
 
LVL 29

Expert Comment

by:Pawan Kumar
ID: 41839426
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
 

Author Comment

by:mburk1968
ID: 41839428
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
 
LVL 29

Accepted Solution

by:
Pawan Kumar earned 2000 total points
ID: 41839434
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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question