• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4767
  • Last Modified:

Converting datetime to shortdatetime

I am trying to convert a date field so I can yes it in a query

Original data is CrDate with data type of datetime. 2007-04-02 23:00:15.397
I would like to convert it to smalldatetime so I can run queries against it i.e. WHERE (CrDate = '4/2/2007')

I see a function CONVERT ( data_type [ ( length ) ] , expression [ , style ] ), but I'm not sure what to substitute such as style.

Environment SQL Server 2000

I would appreciate any suggstions.  Thanks.  DC900

0
dc900
Asked:
dc900
5 Solutions
 
lahousdenCommented:
Just as an aside - if the only reason for changing the type is for being able to find rows in the database then datetime will work pretty much the same as smalldatetime.  I.e. If none of your Invoices were created at exactly midnight, then

select *
from Invoices
where created = '4/1/2007'

will find no rows whether created is datetime or smalldatetime.  The "gotcha" for new SQL developers is to query by range, e.g.

select *
from Invoices
where created >= '4/1/2007' and created < '4/2/2007'

will find all the Invoices for 4/1/2007.
0
 
lahousdenCommented:
Incidentally, if you still feel you want to change the type then you can use Enterprise Manager to change the column type in the table - it will complain about losing information but you can choose to ignore this and go ahead with the change.
0
 
nakul_vachhrajaniCommented:
I believe that even without conversion, your query should perform fine enough.
Even then if you are using stored procedures, you can try using SET DATEFORMAT (Refer SQL BOL for more details on this)
0
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
YZlatCommented:
SELECT CONVERT(VARCHAR(10), datetimefield, 101) AS formatted_date
0
 
folderolCommented:
Datetime is stored in SQL as a float value, the integer portion is days and the fractional portion is time. To strip off the time from a datetime value, you only need to convert it to an integer, the following are all popular formulas for doing this.  Copy / paste these lines to Query Analyzer to see the results.

select
-- returns string
convert(char(10),getdate(),101),  
-- returns datetime
convert(datetime,convert(char(10),getdate(),101)),
cast(datediff(day,0,getdate()) as datetime),
cast(floor(cast(getdate() as float)) as datetime)

Tom
0
 
mkennonCommented:
YZlat's Comment is the correct solution.
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now