?
Solved

Time Function Issue

Posted on 2013-07-01
3
Medium Priority
?
212 Views
Last Modified: 2013-07-08
I currently have a column in my fact table which is an int in the format of 20130701 for example

I need to be able to subtract 30 days from this date or whatever the date is

what is the best way to achieve this since I cannot change the datatype from Int to Datetime


Thanks in Advance
0
Comment
Question by:VitaminD
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 40

Accepted Solution

by:
Kyle Abrahams earned 2000 total points
ID: 39291688
declare @test int
set @test = 20130701

select dateadd(d, -30, cast(cast(@test as varchar(10)) as datetime))
0
 
LVL 20

Expert Comment

by:dsacker
ID: 39291700
Assuming this needs to be returned back to an integer, this would work (complete with demonstration from a temp table):

DECLARE @Table TABLE (
    seq     smallint    NOT NULL IDENTITY(1, 1),
    intdate int         NOT NULL )

INSERT INTO @Table (intdate)
SELECT  20130701 UNION ALL
SELECT  20130515

SELECT  *,
        CONVERT(int, CONVERT(varchar(10), CONVERT(datetime, CONVERT(varchar(8), intdate)) - 30, 112))
            AS NewIntDate
FROM    @Table

Open in new window

0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 39292387
'fact table' indicates warehouse and likely data volume, plus use of integer also suggests desire for efficiency, so I'd caution you to avoid changing the data to suit your queries so that you preserve the advantages of indexes.

e.g.
DECLARE @from int, @until int
SET @from  = 20130601
SET @until = CONVERT(int, CONVERT(varchar,getdate(),112) )

;WITH
sample AS (
            SELECT 20140101 AS theInt UNION ALL
            SELECT 20130701 AS theInt UNION ALL
            SELECT 20130630 AS theInt UNION ALL
            SELECT 20130629 AS theInt
          )

SELECT
theInt, @from, @until
FROM sample
WHERE theInt >= CONVERT(int, CONVERT(varchar,dateadd(d, -30, CONVERT(varchar,@from,112) ) ,112) )
  AND theInt  < @until
;

Open in new window

0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how the fundamental information of how to create a table.
Suggested Courses

801 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