Solved

Convert SQL Column DataType Int to Date Field

Posted on 2010-11-15
11
551 Views
Last Modified: 2012-05-10
I have Column Field :- FillDate
DataType of this Column is Integer

Value is stored in the Table as Integer.
for which i already written a query to represent YYYY-MM-DD Format.

SELECT
case when len(Rx.FillDate) = 7 then dateadd(d, (cast(RIGHT(Rx.FillDate,3) as int) -1), cast(LEFT(Rx.FillDate,4) as DATE))else null end as [FillDate]
FROM Rx

Being Int DataType in the Table, I am not able to sort or write a Date based filteration.

If i need records between 10/01/2010 to 11/01/2010 .. some thing like date filteration , how should i do this ?
0
Comment
Question by:chokka
11 Comments
 
LVL 28

Expert Comment

by:sammySeltzer
ID: 34140436
You should have left it like it you had - mmddyyyy and then you would do something like this:

between convert(varchar, startdate, 101) and convert(varchar, enddate, 101)

I am sure some of the gurus can come up with something better.
0
 
LVL 58

Accepted Solution

by:
cyberkiwi earned 500 total points
ID: 34140459
Between year('2010-10-01') * 1000 + DATEPART(dy, '2010-10-01')
and year('2010-11-01') * 1000 + DATEPART(dy, '2010-11-01')
0
 
LVL 69

Expert Comment

by:ScottPletcher
ID: 34140506
It looks as if your data is in format yyyyddd (right?).

You really shouldn't adjust a table column, you should adjust the data outside the column.  Otherwise SQL can't use an index even if one exists.

That doesn't mean you can't specify your ranges as normal dates.  Just let code adjust it to the right table column format for you.

For example:
DECLARE @startdate datetime

DECLARE @enddate datetime



SET @startdate = '20101001'

SET @enddate = '20101101'



SELECT ...

FROM Rx

WHERE FillDate >= YEAR(@startdate) * 1000 + DATEPART(DAYOFYEAR, @startdate) 

    AND FillDate < YEAR(@enddate) * 1000 + DATEPART(DAYOFYEAR, @enddate)

Open in new window

0
 

Author Comment

by:chokka
ID: 34140531
Scott, Thank  you.

Cyberkiwi solution also worked. I have not noticed your syntax.
0
 
LVL 69

Expert Comment

by:ScottPletcher
ID: 34140908
Never pays to actually explain an answer here :-) .
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 69

Expert Comment

by:ScottPletcher
ID: 34140922
Btw, always use:
yyyymmdd
instead of:
yyyy-mm-dd
whenever you can, because SQL *always* interprets yyyymmdd correctly, but yyyy-mm-dd can cause errors with certain SQL settings.
0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 34141029
whenever you can, because SQL *always* interprets yyyymmdd correctly, but yyyy-mm-dd can cause errors with certain SQL settings.
Scott, I don't know where you come from, but that is not true, as far as I know.
0
 
LVL 69

Expert Comment

by:ScottPletcher
ID: 34141130
What makes you say that?

YYYY-MM-DD will clearly not work with "SET DATEFORMAT ydm".

But I think there are a couple of other settings that cause it problems.

Also, it's potentially ambiguous and could give you bad data if the setting is actually ydm and you think it's ymd:

SELECT '2010-03-07'

Is that Mar 7 or Jul 3??  It depends on what country you're in :-) .

20100307 is *always* Mar 7.
0
 
LVL 69

Expert Comment

by:ScottPletcher
ID: 34141137
I swear I once saw some kind of odd errors people got with yyyy-mm-dd even with a (default) ymd setting.

It's my understanding SQL always treats yyyymmdd as exactly that.

Is there some type of setting I'm overlooking on that?
0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 34141209
sold
0
 
LVL 69

Expert Comment

by:ScottPletcher
ID: 34141336
CORRECTION:

I swear I meant to put a smiley face after this:

YYYY-MM-DD will clearly not work with "SET DATEFORMAT ydm". :-) :-)
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

758 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

23 Experts available now in Live!

Get 1:1 Help Now