Click here to Skip to main content
15,908,581 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Can anyone help me on this
I Have a Column in table

Column1
2014-11-02 00:00:00.000
2015-11-02 00:00:00.000
2015-12-03 00:00:00.000

I want Output as

Column1
NOV-2014
NOV-2015
Dec-2015

How to write a SQL Query?
Posted
Updated 20-Nov-15 0:29am
v2

SQL
SELECT LEFT(Datename(Month, Column1),3) + '-' + CAST(YEAR(Column1) as Varchar) FROM Your Table
 
Share this answer
 
It depends on what datatype your column is.
If it's VARCHAR or NVARCHAR, then it's a problem. If it's DATETIME, then it's pretty simple - but probably not advisable.

If you have it stored as a string based column, then you need to change that and convert the data to DATETIME before somehow a "bad" value gets in there.

Even as a DATETIME value, it's not a good idea to do this in SQL: it should be done in your presentation software, not the DB. Why? Because for an English user you need JAN, FEB, ... but for a French user it should be JAN, FEV, ... and SQL server doesn't have access to your user culture, unlike your presentation software.

But...in SQL it's pretty easy:
SQL
SELECT SUBSTRING(CONVERT(VARCHAR(11), MyDateColumn, 6), 4, 6)
 
Share this answer
 
Comments
Herman<T>.Instance 20-Nov-15 10:08am    
And the YEAR part ;) ?
OriginalGriff 20-Nov-15 10:28am    
What about the year part? Style 6 generates 20 NOV 15 - and the SUBSTRING discards the first 3 characters.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900