Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
In SQL SERVER 2005/2008 I am trying to retrieve a certain range of dates. I tried to convert the DateTime datatype to a numeric and a decimal using CONVERT, but this did not work. It gave me a ficticous number. How would you manipulate the DateTime datatype to focus in on a desired range of dates using T-SQL????
Posted
Comments
_Zorro_ 26-Jul-11 10:54am    
I probably am missing something, but why would you want to convert a DateTime to Decimal ?!
SQL Ed 26-Jul-11 12:00pm    
The question clearly states more than one time that I am trying to retrieve a desired range of dates.

Hi,

When ever checking date time column, U need to convert "112" format. check the following example.

SQL
DECLARE @DateTime TABLE(ID INT IDENTITY(1,1),EmpName VARCHAR(100),DOJ DATETIME )
DECLARE @FDate DATETIME,@TDate DATETIME

INSERT INTO @DateTime(EmpName,DOJ)
SELECT 'Kumar',GETDATE()
UNION ALL
SELECT 'Raja',GETDATE()-10
UNION ALL
SELECT 'Sive',GETDATE()-50
UNION ALL
SELECT 'Venkat',GETDATE()-100


SELECT @FDate=GETDATE()-30,@TDate=GETDATE()
SELECT @FDate,@TDate
SELECT * FROM @DateTime 
WHERE CONVERT(VARCHAR(10),DOJ,112) BETWEEN CONVERT(VARCHAR(10),@FDate,112) AND CONVERT(VARCHAR(10),@TDate,112)


In this example the DateTime Column values like "2012-01-23 18:32:59.960" is Converted to "20120123" (We will do this as Number or Varchar).
this format 112 is gives you as 2012 year , 01 month, 23 day (20120123).

With Regards,
GVPrabu
 
Share this answer
 
Why convert it at all? If you have a DateTime, use it as a DateTime!
SELECT * FROM myTable WHERE dateEntered < '2011-05-01'
 
Share this answer
 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900