Basic SQL - Date

I have a numeric field containing a date formated as CYYMMDD(C = Century, if C=1 it is 2000, if C=0 it is 1900). Some dates are :-
1040915
1040915
1040915
1040917
1040917
1040428

I want t display it as MM-DD-YYYY.
 This works for me but I feel the formating is too long.

select CONVERT(CHAR(10), CONVERT(DATETIME, SUBSTRING(LTRIM(STR(A.DateCreated)),2,6), 112), 110) 'Date Created'
from Order A

some result
09-15-2004
09-15-2004
09-15-2004
09-15-2004
09-15-2004
04-28-2004
08-18-2004
08-18-2004
08-18-2004
09-15-2004
09-15-2004


If you have a better Approach let me. If you think I am doing the right thing let me know too. I will award the points either way.

Thanks
yassin092898Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SashPCommented:
Forget the formatting.

The issue is performance.

If you are happy with the performance and the code works stick with it.  Do not over engineer a solution as you may be taking time away from other important issues.

If it works (performance issues not withstanding) do not fix it.

Cheers Sash
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
HilaireCommented:
Personnally i'd create a new computed column on the table, in true datetime format

alter table Order add DateTime_Created as cast(cast(19000000 + @day as varchar) as datetime)

then i'd use this column to make all sorts of things that can't be achieved with your above format :

- retrieve records that are less than 10 days old
select * from Order where datediff(day, DateTime_Created, getdate()) < 10

- calculate a difference in days between two records
- retrieve week numbern, month name, ....
date functions are quite powerful in SQL Server, it's really a shame not to use datetime format

As for the formatting, it should be handled on the front-end side.

HTH

Hilaire

0
SashPCommented:
Your function does not work when the century = 0.

So "if it aint broke don't fix it" does not seem to apply here.

Have a look at what happens when you get dates in the 1900s


declare @a table (adate int)

insert into @a values('1040915')
insert into @a values('1990915')
insert into @a values('1040915')
insert into @a values('1040917')
insert into @a values('0000917')
insert into @a values('0990421')

select * from @a

select STR(adate) from @a

0
SashPCommented:
Then look at

declare @a table (adate int)

insert into @a values('1040915')
insert into @a values('1990915')
insert into @a values('1040915')
insert into @a values('1040917')
insert into @a values('0000917')
insert into @a values('0990421')

select * from @a

select case when adate > 999999 then '20' else '19' end +  right('000' + ltrim(STR(adate)),6) from @a
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

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.