String to MMM YYYY

Hello

I have this column called dates with string value, and I want to see how I can convert them to MMM YYYY, can anyone help?

DATES.JPG
barkomeAsked:
Who is Participating?
 
Scott PletcherConnect With a Mentor Senior DBACommented:
I'd parse out what you can while leaving the original value as a string,  until you determine the month and year, if available/determinable.  I use CROSS APPLYs to move the computations out of the main SELECT and to allow subsequent computations to use previous ones without having to recompute them.
For example:


SELECT dates,
    SUBSTRING('__JanFebMarAprMayJunJulAugSepOctNovDec', ISNULL(dates_month, dates_month2) * 3, 3) AS month,
    COALESCE(dates_year, dates_year2, YEAR(GETDATE())) AS year
FROM #TEMP_DATES d
CROSS APPLY (
    SELECT CASE
        WHEN dates LIKE '%Jan%' THEN 1
        WHEN dates LIKE '%Feb%' THEN 2
        WHEN dates LIKE '%Mar%' THEN 3
        WHEN dates LIKE '%Apr%' THEN 4
        WHEN dates LIKE '%May%' THEN 5
        WHEN dates LIKE '%Jun%' THEN 6
        WHEN dates LIKE '%Jul%' THEN 7
        WHEN dates LIKE '%Aug%' THEN 8
        WHEN dates LIKE '%Sep%' THEN 9
        WHEN dates LIKE '%Oct%' THEN 10
        WHEN dates LIKE '%Nov%' THEN 11
        WHEN dates LIKE '%Dec%' THEN 12
        ELSE NULL END AS dates_month
) AS ca1
CROSS APPLY (
    SELECT CASE WHEN dates_month > 0 THEN NULL ELSE REPLACE(LEFT(dates, 2), '/', '') END AS dates_month_string
) AS ca2
CROSS APPLY (
    SELECT CASE
        WHEN dates_month_string LIKE '%[^0-9]%' THEN NULL
        WHEN dates_month_string < 1 OR dates_month_string > 12 THEN NULL
        ELSE dates_month_string END AS dates_month2
) AS ca3
CROSS APPLY (
    SELECT SUBSTRING(dates, NULLIF(PATINDEX('%[2][012][0-9][0-9]%', dates), 0), 4) AS dates_year
) AS ca4
CROSS APPLY (
    SELECT CASE WHEN dates_year > 0 THEN NULL
        ELSE '20' + SUBSTRING(dates, LEN(dates) - NULLIF(PATINDEX('%[0-9][0-9]%', REVERSE(dates)), 0), 2) END AS dates_year2
) AS ca5
1
 
PortletPaulfreelancerCommented:
If you have a recent version of MS SQL you could use TRY_CAST() like this

select try_cast('Apr-16' as date)

it returns NULL if it cannot complete the conversion (e.g. it returns NULL from that example)

But I suspect what you want will simply not be easy.
0
 
barkomeAuthor Commented:
This was the output...

DATES_1.JPG
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
PortletPaulfreelancerCommented:
why not provide a set of data from that column we can use? (sample data, not an image)
and, also what value you want from each (the expected result)

e.g. what do you want from "February"?

but you do realize miracles don't occur in SQL I trust
0
 
barkomeAuthor Commented:
Sure...

IF OBJECT_ID('tempdb..#TEMP_DATES') IS NOT NULL   DROP TABLE #TEMP_DATES;
CREATE TABLE #TEMP_DATES (DATES VARCHAR(100)) 
INSERT INTO #TEMP_DATES  (DATES)
VALUES ('July 7, 2016'), ('April-16'),('June 2016'),('Febuary'),('Apr-16'),('4/30/2016'),('02/29/2016'),('16-Apr')

SELECT * FROM #TEMP_DATES

Open in new window


Not expecting any miracles but trying to see the feasibility with limited options
0
 
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
You're going to have to write a series of TRY_CAST and UPDATE statements to test each acceptable format to pull this off, including a column like 'FormatID' to identify each value and the format that successfully converted it to a date, and make assumptions.
  • Does 'February' mean the first day of February in the current year, or not a date?
  • Does 'April-16' mean 4/1/2016, or some other day, or not a date?

... and even then it might not be wise to do so ...
  • Does 16-Apr mean 4/16/current year, or 4/?/2016

Looking at the image I'm guessing that the source of data is Excel.  A far better idea would be to contact the source of this data and have them export the file as a .csv with the date values in a standard format.  Excel is great for editing stuff, but in your case Excel is terrible for editing stuff as any little change will cause an ETL process to fail as there is a 'contract' between source and destination, and Excel allows for too many ways to break the contract.
0
 
barkomeAuthor Commented:
Sorry, I forgot to add, I'm only looking at converting the dates field to MMM YYYY
0
 
PortletPaulfreelancerCommented:
What year is to be assumed when one isn't provided?

e.g. the current year? or some other column?

this is why I asked for a column of the expected result e.g.

input_col   output_col
Februaury ??????????
0
 
barkomeAuthor Commented:
its '02/29/2016' for an output of February 2016,
0
 
Daniel JonesData Research AnalystCommented:
select right(convert(varchar, getdate(), 103), 7)
or
SELECT DATENAME(MONTH, @thedate) + '/' + DATENAME(YEAR, @thedate)
or
Select SubString(Convert(Varchar(Max), GetDate(),0), 1, 3) + '/' + Cast(Year(GetDate()) As Varchar(Max))
0
 
barkomeAuthor Commented:
@ScottPletcher, is there a way to have this as a function?
0
 
Scott PletcherSenior DBACommented:
Sure.  Do you want a scalar function or an inline table-valued functon?  The itvf will perform better if you do this across a lot of rows.
0
 
barkomeAuthor Commented:
Great, scalar will be okay, as the rows are not many in number, thanks.
0
 
Scott PletcherSenior DBACommented:
CREATE FUNCTION dbo.Parse_Date (
    @date_string varchar(100)
)
RETURNS varchar(30)
AS
BEGIN
RETURN (
    SELECT SUBSTRING('__JanFebMarAprMayJunJulAugSepOctNovDec', ISNULL(date_month, date_month2) * 3, 3) + ' ' +
        CAST(COALESCE(date_year, date_year2, YEAR(GETDATE())) AS varchar(4)) AS date_mmm_yyyy
    FROM (
        SELECT @date_string AS date
    ) AS input_param    
    CROSS APPLY (
        SELECT CASE
            WHEN date LIKE '%Jan%' THEN 1
            WHEN date LIKE '%Feb%' THEN 2
            WHEN date LIKE '%Mar%' THEN 3
            WHEN date LIKE '%Apr%' THEN 4
            WHEN date LIKE '%May%' THEN 5
            WHEN date LIKE '%Jun%' THEN 6
            WHEN date LIKE '%Jul%' THEN 7
            WHEN date LIKE '%Aug%' THEN 8
            WHEN date LIKE '%Sep%' THEN 9
            WHEN date LIKE '%Oct%' THEN 10
            WHEN date LIKE '%Nov%' THEN 11
            WHEN date LIKE '%Dec%' THEN 12
            ELSE NULL END AS date_month
    ) AS ca1
    CROSS APPLY (
        SELECT CASE WHEN date_month > 0 THEN NULL ELSE REPLACE(LEFT(date, 2), '/', '') END AS date_month_string
    ) AS ca2
    CROSS APPLY (
        SELECT CASE
            WHEN date_month_string LIKE '%[^0-9]%' THEN NULL
            WHEN date_month_string < 1 OR date_month_string > 12 THEN NULL
            ELSE date_month_string END AS date_month2
    ) AS ca3
    CROSS APPLY (
        SELECT SUBSTRING(date, NULLIF(PATINDEX('%[2][012][0-9][0-9]%', date), 0), 4) AS date_year
    ) AS ca4
    CROSS APPLY (
        SELECT CASE WHEN date_year > 0 THEN NULL
            ELSE '20' + SUBSTRING(date, LEN(date) - NULLIF(PATINDEX('%[0-9][0-9]%', REVERSE(date)), 0), 2) END AS date_year2
    ) AS ca5
)
END /*FUNCTION*/
GO
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.