Avatar of Michael Katz
Michael KatzFlag for United States of America

asked on 

SQL script to convert Calendar

I guess its really hard to explain what I am looiking at...

CalCentury  CalYear  CalMonth  CalDay1  CalDay2  CalDay3   CalDay4......CalDay31
20                 11            1            0                0             1                    1
20                 11            2            1                1             1                    1

These represent Days of the Week per month, per year, Per Century..

The 0 in each CalDay Column represent NonWorking Days while the 1 is a WorkingDay

How can I get my data to  look like this   Where 2011= Year, 01 = month ,01 = Day
Date         WorkDay
20110101     1
20110102     0

Hopefully this makes sense
Microsoft SQL Server 2005Microsoft SQL Server 2008SQL

Avatar of undefined
Last Comment
Kevin Cross
Avatar of Kevin Cross
Kevin Cross
Flag of United States of America image

Hi.

Firstly, you can use UNPIVOT (SQL 2005+) to get the days vertically.

Something like:
;with cal_upvt(CalCentury, CalYear, CalMonth, CalDay, WorkDay) AS (
   -- unpivot your original data
   SELECT CalCentury, CalYear, CalMonth
        , CONVERT(INT, REPLACE(CalDay, 'CalDay', '')), WorkDay
   FROM Your_Calendar
   UNPIVOT(WorkDay FOR CalDay IN (CalDay1, CalDay2, CalDay3, CalDay4, CalDay31)) upvt
)
-- put together final date and display
SELECT (CalCentury*1000000+CalYear*10000+CalMonth*100+CalDay) AS [Date], WorkDay
FROM cal_upvt
;

Open in new window

Avatar of Kevin Cross
Kevin Cross
Flag of United States of America image

OH, and if your [Date] column is ultimately to represent a *true* datetime, then just ensure that your data doesn't have any invalid options like CalMonth = 2 and CalDay31 <> null.  You can check for this by using ISDATE(CONVERT(CHAR(8), CalCentury*1000000+CalYear*10000+CalMonth*100+CalDay)) = 1 as a filter in WHERE of final select.

Avatar of Michael Katz
Michael Katz
Flag of United States of America image

ASKER

Getting error..
 ;WITH cal_upvt(CalCentury, CalYear, CalMonth, CalDay, WorkDay) AS (
   -- unpivot your original data
   SELECT CalCentury, CalYear, CalMonth
        , CONVERT(INT, REPLACE(CalDay, 'CalDay', '')), WorkDay
   FROM dbo.AdmCalendar
   UNPIVOT(WorkDay FOR CalDay IN (CalDay1, CalDay2, CalDay3,
   CalDay4,
   CalDay5,
   CalDay6,
   CalDay7,
   CalDay8,
   CalDay9,
   CalDay10,
   CalDay11,
   CalDay12,
   CalDay13,
   CalDay14,
   CalDay15,
   CalDay16,
   CalDay17,
   CalDay18,
   CalDay19,
   CalDay20,
   CalDay21,
   CalDay22,
   CalDay23,
   CalDay24,
   CalDay25,
   CalDay26,
   CalDay27,
   CalDay28,
   CalDay29,
   CalDay30,
   CalDay31) )

Msg 102, Level 15, State 1, Line 35
Incorrect syntax near ')'.


you are missing at least 1 ) at the end.
Avatar of Sharath S
Sharath S
Flag of United States of America image

On a quick look, you did not run the whole query provided by kevin. run this.
;WITH cal_upvt(CalCentury, CalYear, CalMonth, CalDay, WorkDay) AS (
   -- unpivot your original data
   SELECT CalCentury, CalYear, CalMonth
        , CONVERT(INT, REPLACE(CalDay, 'CalDay', '')), WorkDay
   FROM dbo.AdmCalendar
   UNPIVOT(WorkDay FOR CalDay IN (CalDay1, CalDay2, CalDay3,
   CalDay4,
   CalDay5,
   CalDay6,
   CalDay7,
   CalDay8,
   CalDay9,
   CalDay10,
   CalDay11,
   CalDay12,
   CalDay13,
   CalDay14,
   CalDay15,
   CalDay16,
   CalDay17,
   CalDay18,
   CalDay19,
   CalDay20,
   CalDay21,
   CalDay22,
   CalDay23,
   CalDay24,
   CalDay25,
   CalDay26,
   CalDay27,
   CalDay28,
   CalDay29,
   CalDay30,
   CalDay31)) upvt
)
-- put together final date and display
SELECT (CalCentury*1000000+CalYear*10000+CalMonth*100+CalDay) AS [Date], WorkDay
FROM cal_upvt;

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Kevin Cross
Kevin Cross
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Microsoft SQL Server 2005
Microsoft SQL Server 2005

Microsoft SQL Server 2005 is a suite of relational database management system (RDBMS) products providing multi-user database access functionality.Component services include integration (SSIS), reporting (SSRS), analysis (SSAS), data quality, master data, T-SQL and performance tuning. It includes support for managing XML data and allows a database server to be exposed over web services using Tabular Data Stream (TDS) packets encapsulated within SOAP (protocol) requests.

72K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo