Solved

Convert Columns with Day1, Day2, Day3... Day31 converted to Dates

Posted on 2011-03-23
8
418 Views
Last Modified: 2012-05-11
My current table looks like this
WorkCentre  Century  Year   Month  Day1  Day2  Day3  Day4... Day31
A1000             20         11      3        25.1    25.2    30    15.8      14.9
A1000             20         11      4        30.1    30.2    30.2  30.1      15.8

I need to reorganize to look like this
WorkCentre Date         Available
A1000        03/01/2011   25.1
A1000        03/02/2011   25.2
A1000        03/03/2011   30

All the way to the last day of the month...

Really need help with this badly
0
Comment
Question by:Mikeyman_01
  • 4
  • 3
8 Comments
 
LVL 33

Expert Comment

by:knightEknight
ID: 35201890
Create the new table first, with the second column being of DATETIME or SMALLDATETIME type.  Then do this in 31 separate queries:

insert into myNewTable
select WorkCenter, convert(varchar,[Century]) + convert(varchar,[Year]) +'-'+ convert(varchar,[month]) +'-01', Day1 as Available
from myCurrentTable

insert into myNewTable
select WorkCenter, convert(varchar,[Century]) + convert(varchar,[Year]) +'-'+ convert(varchar,[month]) +'-02', Day2 as Available
from myCurrentTable

insert into myNewTable
select WorkCenter, convert(varchar,[Century]) + convert(varchar,[Year]) +'-'+ convert(varchar,[month]) +'-03', Day3 as Available
from myCurrentTable

-- ... etc


insert into myNewTable
select WorkCenter, convert(varchar,[Century]) + convert(varchar,[Year]) +'-'+ convert(varchar,[month]) +'-31', Day31 as Available
from myCurrentTable


0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 35201967
If you want this in a single SQL statement, then try to use UNPIVOT:

select WorkCentre
     , [Date] = convert(date, convert(char(8), Century * 1000000 + [Year] * 10000 + Month * 100 + convert(int, replace([Day], 'Day', ''))), 112)
	 , Available
from your_table
unpivot (
   Available for [Day] in (Day1, Day2, Day3, Day4, Day31)
) upvt
;

Open in new window


One caveat to note is that your data needs to be clean to start with.  In other words, for April you should have NULL value for Day31 as there is not a 31st day of April.  UNPIVOT will handle the nulls correctly since you won't get a row for that in the result.

Note also, my solution is converting the date parts into a true date.  I tested on a SQL 2008 system, so went with DATE data type.  If you want the results displayed as MM/DD/YYYY, you can use CONVERT or instead of casting to a date, simply build each part of the string as above Expert showed.
0
 

Author Comment

by:Mikeyman_01
ID: 35202580
Here is my query...
 select WorkCentre
     , [Date] = convert(date, convert(char(8), CapacityCentury * 1000000 + [CapacityYear] * 10000 + CapacityMonth * 100 + convert(int, replace([Day], 'Day', ''))), 112)
       , Available
from dbo.BomCapacity
unpivot (
    Available for [Day] in (CapacityAvail1,
CapacityAvail2,
CapacityAvail3,
CapacityAvail4,
CapacityAvail5,
CapacityAvail6,
CapacityAvail7,
CapacityAvail8,
CapacityAvail9,
CapacityAvail10,
CapacityAvail11,
CapacityAvail12,
CapacityAvail13,
CapacityAvail14,
CapacityAvail15,
CapacityAvail16,
CapacityAvail17,
CapacityAvail18,
CapacityAvail19,
CapacityAvail20,
CapacityAvail21,
CapacityAvail22,
CapacityAvail23,
CapacityAvail24,
CapacityAvail25,
CapacityAvail26,
CapacityAvail27,
CapacityAvail28,
CapacityAvail29,
CapacityAvail30,
CapacityAvail31)
) upvt
;


This is the message i get

Msg 245, Level 16, State 1, Line 1
Conversion failed when converting the nvarchar value 'CapacityAvail1' to data type int.
0
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 
LVL 59

Expert Comment

by:Kevin Cross
ID: 35202692
That is because your values aren't really Day1, Day2, ... Day31.  Therefore, replacing 'Day' doesn't work.  You will need to replace 'CapacityAvail'.  Remember the point there is to get the number isolated so you can convert to an integer.
select WorkCentre
     , [Date] = convert(date, convert(char(8), CapacityCentury * 1000000 + [CapacityYear] * 10000 + CapacityMonth * 100 + convert(int, replace([Day], 'CapacityAvail', ''))), 112)
       , Available
from dbo.BomCapacity
unpivot (
    Available for [Day] in (CapacityAvail1, 
    CapacityAvail2,
    CapacityAvail3,
    CapacityAvail4,
    CapacityAvail5,
    CapacityAvail6,
    CapacityAvail7,
    CapacityAvail8,
    CapacityAvail9,
    CapacityAvail10,
    CapacityAvail11,
    CapacityAvail12,
    CapacityAvail13,
    CapacityAvail14,
    CapacityAvail15,
    CapacityAvail16,
    CapacityAvail17,
    CapacityAvail18,
    CapacityAvail19,
    CapacityAvail20,
    CapacityAvail21,
    CapacityAvail22,
    CapacityAvail23,
    CapacityAvail24,
    CapacityAvail25,
    CapacityAvail26,
    CapacityAvail27,
    CapacityAvail28,
    CapacityAvail29,
    CapacityAvail30,
    CapacityAvail31)
) upvt
;

Open in new window

0
 

Author Comment

by:Mikeyman_01
ID: 35231890
mwvisa1,

I am still getting the error.. What I am I not doing correctly??..

Msg 241, Level 16, State 1, Line 1
Conversion failed when converting date and/or time from character string.
0
 

Author Comment

by:Mikeyman_01
ID: 35232388
knightEknight,

This Table you showed me doesnt appear to be run everyday...Did I miss something?
0
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 500 total points
ID: 35234473
@Mikeyman_01:

That would suggest you have values that don't equate to a proper date.  It is probably what I was talking about with dates like 4/31 that don't really exist.  Let's try like this with a test first.

+Get all the data without converting to actual DATE
+Using the below as a view, derived table or common table expression, select only those rows where ISDATE([DATE]) = 1.

select WorkCentre
     , [Date] = convert(char(8), CapacityCentury * 1000000 + [CapacityYear] * 10000 + CapacityMonth * 100 + convert(int, replace([Day], 'CapacityAvail', '')))
     , Available
from dbo.BomCapacity
unpivot (
    Available for [Day] in (CapacityAvail1, 
    CapacityAvail2,
    CapacityAvail3,
    CapacityAvail4,
    CapacityAvail5,
    CapacityAvail6,
    CapacityAvail7,
    CapacityAvail8,
    CapacityAvail9,
    CapacityAvail10,
    CapacityAvail11,
    CapacityAvail12,
    CapacityAvail13,
    CapacityAvail14,
    CapacityAvail15,
    CapacityAvail16,
    CapacityAvail17,
    CapacityAvail18,
    CapacityAvail19,
    CapacityAvail20,
    CapacityAvail21,
    CapacityAvail22,
    CapacityAvail23,
    CapacityAvail24,
    CapacityAvail25,
    CapacityAvail26,
    CapacityAvail27,
    CapacityAvail28,
    CapacityAvail29,
    CapacityAvail30,
    CapacityAvail31)
) upvt
;

Open in new window

0
 

Author Comment

by:Mikeyman_01
ID: 35237298
mwvisa1,

Excellent Job!! Thank you very much...

You helped me tremendously..

Michael
0

Featured Post

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
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.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

813 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now