Share via


How to get first 3 letters and year name from a column in SQL

Question

Friday, August 23, 2013 3:55 PM

Hi,

I have a column which has the month names something like 'JANUARY - 2013' but i want the output to be like 'JAN _ 2013' which is the first 3 letters of the month and year.

Can someone help me with this

Thanks.

All replies (4)

Friday, August 23, 2013 4:24 PM ✅Answered

SELECT SUBSTRING ('JANUARY - 2013',1,3)+'_'+SUBSTRING('JANUARY - 2013',LEN('JANUARY - 2013')-4,5)

or if you running on date column

select convert(char(3), GETDATE(), 0)+'_'+CAST(DATEPART(YEAR,GETDATE()) AS VARCHAR(4))


Friday, August 23, 2013 4:34 PM | 1 vote

select left(Col,3) + '_' + right(col,4) 

if your data is always in this format.

For every expert, there is an equal and opposite expert. - Becker's Law

My blog

My TechNet articles


Friday, August 23, 2013 4:39 PM

It worked fine.Thanks


Friday, August 23, 2013 5:53 PM

declare @s varchar(15)='JANUARY - 2013'SELECT Left(Datename(m, Cast ('1 '+ Replace(@s,'-','') as datetime)),3) + '_'+ Datename(yy,(Cast ('1 '+ Replace(@s,'-','') as datetime)))  ,Stuff(convert(char(11),Cast ('1 '+ Replace(@s,'-','') as datetime), 109),4,4,'_')