Solved

Time Function Issue

Posted on 2013-07-01
3
195 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
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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

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…
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.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

863 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

26 Experts available now in Live!

Get 1:1 Help Now