SQL Date Time Bands SSAS Table

Hello, i am trying to create a time band calendar.

example:-
band 1: Time 00:00 - 00:59
band 2: Time 01:00 - 01:59

band 24: 23:00 - 23:59

thanks.
aneilgAsked:
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.

Vadim RappCommented:
in what form? a table in sql server database? print on paper? something else?
0
aneilgAuthor Commented:
I need it in a table.

we already have something like this.

      -- bandings
      SET @band2Hours = 'Between ' + CAST(@inHours - (@inHours % 2) AS VARCHAR) + ' and ' + CAST(1 + @inHours - (@inHours % 2) AS VARCHAR) + ' hours'
      SET @band3Hours = 'Between ' + CAST(@inHours - (@inHours % 3) AS VARCHAR) + ' and ' + CAST(2 + @inHours - (@inHours % 3) AS VARCHAR) + ' hours'
      SET @band4Hours = 'Between ' + CAST(@inHours - (@inHours % 4) AS VARCHAR) + ' and ' + CAST(3 + @inHours - (@inHours % 4) AS VARCHAR) + ' hours'
      SET @band5Hours = 'Between ' + CAST(@inHours - (@inHours % 5) AS VARCHAR) + ' and ' + CAST(4 + @inHours - (@inHours % 5) AS VARCHAR) + ' hours'
      SET @band10Hours = 'Between ' + CAST(@inHours - (@inHours % 10) AS VARCHAR) + ' and ' + CAST(9 + @inHours - (@inHours % 10) AS VARCHAR) + ' hours'
      SET @band12Hours = 'Between ' + CAST(@inHours - (@inHours % 12) AS VARCHAR) + ' and ' + CAST(11 + @inHours - (@inHours % 12) AS VARCHAR) + ' hours'
      SET @band24Hours = 'Between ' + CAST(@inHours - (@inHours % 24) AS VARCHAR) + ' and ' + CAST(23 + @inHours - (@inHours % 24) AS VARCHAR) + ' hours'
      SET @band36Hours = 'Between ' + CAST(@inHours - (@inHours % 36) AS VARCHAR) + ' and ' + CAST(35 + @inHours - (@inHours % 36) AS VARCHAR) + ' hours'
      SET @band48Hours = 'Between ' + CAST(@inHours - (@inHours % 48) AS VARCHAR) + ' and ' + CAST(47 + @inHours - (@inHours % 48) AS VARCHAR) + ' hours'
      SET @band72Hours = 'Between ' + CAST(@inHours - (@inHours % 72) AS VARCHAR) + ' and ' + CAST(71 + @inHours - (@inHours % 72) AS VARCHAR) + ' hours'

      INSERT INTO
      DimTimeBandHours (
0
Vadim RappCommented:
INSERT INTO      DimTimeBandHours (<whatever-column-you-have>) select  'band 1: Time 00:00 - 00:59'
 INSERT INTO      DimTimeBandHours (<whatever-column-you-have>) select 'band 2: Time 01:00 - 01:59'
...
 INSERT INTO      DimTimeBandHours (<whatever-column-you-have>) select 'band 24: Time 23:00 - 23:59'


This will take 2-3  minutes of your time; any other way will take more.
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

aneilgAuthor Commented:
thanks but its not what i needed.

I need to create a table so i can create a time calendar in SSAS.

I need to dynamically populate a table.
0
aneilgAuthor Commented:
i have found this on google, but not quite correct.

CREATE TABLE [dbo].[DimTime](
 [TimeSK] [int] IDENTITY(1,1) NOT NULL,
 [Time] [char](8) NOT NULL,
 [Hour] [char](2) NOT NULL,
 [MilitaryHour] [char](2) NOT NULL,
 [Minute] [char](2) NOT NULL,
 [Second] [char](2) NOT NULL,
 [AmPm] [char](2) NOT NULL,
 [StandardTime] [char](11) NULL,
 CONSTRAINT [PK_DimTime] PRIMARY KEY CLUSTERED 
 (
 [TimeSK] ASC
 )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
 ) ON [PRIMARY]

GO
SET ANSI_PADDING OFF

PRINT CONVERT(VARCHAR,GETDATE(),113)--USED FOR CHECKING RUN TIME.

--Load time data for every second of a day
DECLARE @Time DATETIME

SET @TIME = CONVERT(VARCHAR,'12:00:00 AM',108)

TRUNCATE TABLE DimTime

WHILE @TIME <= '11:59:59 PM'
 BEGIN
 INSERT INTO dbo.DimTime([Time], [Hour], [MilitaryHour], [Minute], [Second], [AmPm])
 SELECT CONVERT(VARCHAR,@TIME,108) [Time]
 , CASE 
 WHEN DATEPART(HOUR,@Time) > 12 THEN DATEPART(HOUR,@Time) - 12
 ELSE DATEPART(HOUR,@Time) 
 END AS [Hour]
 , CAST(SUBSTRING(CONVERT(VARCHAR,@TIME,108),1,2) AS INT) [MilitaryHour]
 , DATEPART(MINUTE,@Time) [Minute]
 , DATEPART(SECOND,@Time) [Second]
 , CASE 
 WHEN DATEPART(HOUR,@Time) >= 12 THEN 'PM'
 ELSE 'AM'
 END AS [AmPm]

 SELECT @TIME = DATEADD(second,1,@Time)
 END

UPDATE DimTime
SET [HOUR] = '0' + [HOUR]
WHERE LEN([HOUR]) = 1

UPDATE DimTime
SET [MINUTE] = '0' + [MINUTE]
WHERE LEN([MINUTE]) = 1

UPDATE DimTime
SET [SECOND] = '0' + [SECOND]
WHERE LEN([SECOND]) = 1

UPDATE DimTime
SET [MilitaryHour] = '0' + [MilitaryHour]
WHERE LEN([MilitaryHour]) = 1

UPDATE DimTime
SET StandardTime = [Hour] + ':' + [Minute] + ':' + [Second] + ' ' + AmPm
WHERE StandardTime is null
AND HOUR <> '00'

UPDATE DimTime
SET StandardTime = '12' + ':' + [Minute] + ':' + [Second] + ' ' + AmPm
WHERE [HOUR] = '00'

Open in new window

0
Vadim RappCommented:
If I understand correctly, this table is static table that you'd create and fill just once. You want to write a program for one-time filling 24 lines?
0
aneilgAuthor Commented:
hello,

Yes that correct,

I need a static table to i can join my fact table to it.
0
Vitor MontalvãoMSSQL Senior EngineerCommented:
This script will create the 24 bands in a new table called TimeBand. You can change the script for a better table name if you wish:
SELECT TOP (24) RIGHT('00'+CAST(ROW_NUMBER() OVER (ORDER BY object_id)-1 AS VARCHAR(2)),2)+':00'
		+ '-' + RIGHT('00'+CAST(ROW_NUMBER() OVER (ORDER BY object_id)-1 AS VARCHAR(2)),2)+':59' AS Band
INTO TimeBand
FROM sys.all_objects

SELECT *
FROM TimeBand

Open in new window

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
Vadim RappCommented:
The optimal way to create static table with 24 lines is to type them, or to use a trivial script like in my first comment.

If this is an academic assignment, the idea is that you have to do it by yourself. We can offer guidance, but we shouldn't be doing it for you.
0
aneilgAuthor Commented:
No its not an assignment.

I left Uni many moons ago.
Since then i have Acquired a wife a few kids and a mortgage.

thanks vitor.
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
Query Syntax

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.