Solved

Time Function Issue

Posted on 2013-07-01
3
204 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 500 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 48

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

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.

Question has a verified solution.

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

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how the fundamental information of how to create a table.

696 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