Solved

Convert varchar Mon-yyyy  to date

Posted on 2012-04-02
8
550 Views
Last Modified: 2012-04-03
My date field is DATE (varchar(8))  and is in the format Mon-YYYY (ex: Apr-2010).  How do I convert to a date using the 1st of the month.  I tried several solutions I found here and on the web but couldn't get any to work.
0
Comment
Question by:CarenC
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 18

Expert Comment

by:lludden
ID: 37796697
DECLARE @V VARCHAR(8) = 'APR-2012'

SELECT cast('01-' + @v AS DATETIME)
0
 

Author Comment

by:CarenC
ID: 37796759
I don't know how or where to use the DECLARE statement.  All I know to do is run a SELECT.
0
 
LVL 18

Accepted Solution

by:
lludden earned 500 total points
ID: 37796871
I was just doing that to show the syntax.  If the field in your table is MyDate then use

SELECT ELECT cast('01-' + MyDate AS DATETIME) AS MyDate FROM myTable
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 6

Expert Comment

by:rkannan2000
ID: 37797307
Hi lludden,
I think the following might help.

SELECT STR_TO_DATE(concat('01-',myDate) , '%d-%M-%Y')   FROM myTable

Regards
Kannan
0
 

Author Closing Comment

by:CarenC
ID: 37801457
lludden's solution worked.  rkannan2000's didn't work . . . the error was str_to_date is not a valide function.
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 37801468
YYYYMMDD is a 100% safe format *and* will fit in the existing space if you want to permanently change the column's data.

SELECT
    RIGHT(date, 4) + RIGHT('0' + CAST((CHARINDEX(LEFT(date, 3),
        'JanFebMarAprMayJunJulAugSepOctNovDec') + 2) / 3 AS varchar(2)), 2) + '01' AS date
FROM dbo.tablename
WHERE
    ...


UPDATE dbo.tablename
SET date = RIGHT(date, 4) + RIGHT('0' + CAST((CHARINDEX(LEFT(date, 3),
        'JanFebMarAprMayJunJulAugSepOctNovDec') + 2) / 3 AS varchar(2)), 2) + '01'
0
 

Author Comment

by:CarenC
ID: 37801490
Scott . . . good suggestion but can't make that change in this situation.
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 37801579
Would still work as a SELECT, of course :-) :

SELECT
    CAST(RIGHT(date, 4) + RIGHT('0' + CAST((CHARINDEX(LEFT(date, 3),
        'JanFebMarAprMayJunJulAugSepOctNovDec') + 2) / 3 AS varchar(2)), 2) + '01' AS date) AS date
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Server How-To Show Notes In First Row of Results 4 31
SQL Log size 3 20
SQL 2012 clustering 9 13
SQL Find Carriage Return and Delete it. 3 16
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

820 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