SQL decimal field containing date YYMMDD, add century for select's where() and order by

I have a C# program accessing an SQL table with a decimal field that contains dates in YYMMDD format. I want to select rows based on a date range (year 85 or later for example).  How can I add a century to the field or create another field so that, for example, I can select only certain years (85 - 12) and 990101 precedes 010101 in the results?
MikeBroderickAsked:
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.

Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
>SQL table with a decimal field that contains dates in YYMMDD format.
Give us a 'for example' of a decimal field value that contains a date in YYMMDD format.

In general T-SQL has the CONVERT function where if you scroll down it shows dates, note the 'with century' column.   Also check out PaulMaxwell's EE article SQL Server Date Styles (formats) using CONVERT() for a wompload of T-SQL date conversion functions.

Also adding C# zone to this question.
0
Brian CusterBI ArchitectCommented:
First, I wouldn't use a decimal data type to store dates.  If you need them in a numeric format I would use an integer data type because it is smaller (only 4 bytes) and in some cases easier to work with than a decimal since a decimal isn't a discrete number.  Second, if you want specific ages then I would create a column that has an age field that calculates the age of a person if they were born on that specific date.
0
Scott PletcherSenior DBACommented:
Here's how to create a column that adjusts the existing column to make it a full YYYY.  The new column could be added: 1) to each query where it's needed, 2) as part of a view, or 3) as a computed column.

SELECT CASE WHEN yymmdd_column / 10000 < 50 THEN 20000000 ELSE 19999999 END + yymmdd_column AS yyyymmdd_column
FROM table_name
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
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

AndyAinscowFreelance programmer / ConsultantCommented:
Sounds like BAD design of the database.  Store dates in date fields not as integer values in tables - then it would be much easier to manipulate them later.
0
MikeBroderickAuthor Commented:
Excellent. I think you meant to say ...ELSE 19000000 ... It works perfectly.  Below is the code I tested in SQL Server Mgt Studio.

Thanks.

WITH TEMP_W_COMACT as (Select *, CASE WHEN MDATE / 10000 < 50 THEN 20000000 ELSE 19000000 END + MDATE AS MDATEYMD FROM [AS400].[dbo].[W_COMACT])
SELECT *
  FROM TEMP_W_COMACT
  where (MMTRNO =15 and mflag = ' ')
  order by MMTRNO, mflag, mdate
0
Scott PletcherSenior DBACommented:
Yeah, sorry, I really thought I did post 19000000 but you got it anyway, nice!
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.