Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Convert Year string to date

Posted on 2012-12-28
9
Medium Priority
?
328 Views
Last Modified: 2012-12-28
I have a table that has a birthdate field inside of it.  The vast majority of these records are just the year (example:  "1975")  There are a few records that have an actual date stamp (example:  "04/04/1980")

Here is my issue.

I am running this through ETL.  I need to either trim the date to just have the year marker or add "01/01" to the existing years to standardize.  I have tried script components to append the 01/01 and I have tried data conversion and derived columns, none of which are working.  Is there something else I may be able to do to approach this?  

Thanks
0
Comment
Question by:gwarcher
[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
9 Comments
 
LVL 71

Assisted Solution

by:Qlemo
Qlemo earned 1000 total points
ID: 38727605
An expression like this should allow to get a proper date all time, as long as the date delimiters are not mixed up (dots, slashes etc. mixed):
  case when birthdate like '%/%/%' then birthdate else '01/01/'+birthdate end
0
 
LVL 70

Accepted Solution

by:
Scott Pletcher earned 1000 total points
ID: 38727627
I'd allow for dates of mm-dd-yyyy also, like so:

CASE WHEN LEN(birthdate) = 4 THEN '01/01/' + birthdate ELSE birthdate END

Btw, if you're going to store a date as varchar, the best format is 'YYYYMMDD' -- NO delimiters.  That allows sorting and guarantees that SQL can always correctly interpret the date, regardless of any SQL settings.  [It also saves a little space, but that's not nearly as significant.]
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 38727643
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 70

Expert Comment

by:Scott Pletcher
ID: 38727678
Except that article tells you to use mm/dd/yyyy format, which can cause errors (either because you forgot to include a conversion code or used the wrong one).

Instead, always use 'YYYYMMDD' and you NEVER have to specify a format code for a date, EVER.
0
 

Author Closing Comment

by:gwarcher
ID: 38727744
Thank you, It worked as a case statement.
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 38727951
Scott,
where does my article tell to use mm/dd/yyyy format?
given, it does show examples using that format, but I don't tell that you HAVE to use that format?
0
 
LVL 70

Expert Comment

by:Scott Pletcher
ID: 38728062
Here's the quote from your aricle:

"
Enough playing; let's see "how to do it the right way".
in MS SQL Server, use the CONVERT function:

INSERT INTO orders ( order_date , ordering_client_id )
VALUES ( CONVERT(DATETIME, '08/31/2009', 101 ), 1 )
"


"How to do it the right way" is telling them to use that format, isn't it?
You NEVER mention using 'YYYYMMDD' instead.

We have offices around the world, as do other companies now I'm sure.
Try this on your local SQL:

SET LANGUAGE german

SELECT CAST('20120117' AS datetime)

SELECT CAST('2012-01-17' AS datetime)

Yes, you can use CONVERT on the second one, but then everyone who needs to use the code has to memorize or look up what style "101" -- or whatever other code is the default for that particular location -- means.  That is, if you get code from, say, Europe, it will NOT use "101", it will use some code you're not familiar with and have to look up, and vice versa.

'YYYYMMDD' is universal and will never have to be changed to work anywhere in the world and no one ever has to waste time looking up date format codes.
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 38728178
I agree with that on a pure engineering level 100%.
and I won't debate long about the format etc.

the main point of the article is:
* make sure you convert/cast your "string" into date at the right time, and avoid implicit conversions.
* it's for beginners, not for "advanced" people like you and me, to get them aware of what issues you can run into if you don't handle dates correctly.
0
 
LVL 70

Expert Comment

by:Scott Pletcher
ID: 38728357
Even more vital for beginners to learn correctly from the start -- poor coding is much harder to correct later than teaching good coding to start with.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

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.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

664 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