Solved

Casting a datetime field to a smalldatetime field not working

Posted on 2003-10-21
7
3,043 Views
Last Modified: 2012-06-27
This just seems silly, but I simply cannot get a cast to work. When a record is saved to the db, an entry date (in datetime format) is saved to entry_date field. In a query, I need to get this date, but as a smalldatetime (mm-dd-yyyy) value.

I've tried:

1) CONVERT(smalldatetime, Entry_Date, 101) and still get hours:minutes:seconds
2) CAST(Entry_Date as smalldatetime(101)) as EntryDate and get the error CAST or CONVERT: invalid attributes specified for type 'smalldatetime'
3) cast(Entry_Date as smalldatetime) as EntryDate and get hours:minutes:seconds
4) cast(Entry_Date as smalldatetime, 101) and get the error Incorrect syntax near ','.

This can't be that hard. What am I doing wrong???
0
Comment
Question by:MovnOn
[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
  • 4
  • 2
7 Comments
 
LVL 18

Accepted Solution

by:
lludden earned 125 total points
ID: 9592831
Convert(varchar(10),Entry_Date,101)
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 9593097
SmallDate includes the time, so you should convert to a varchar as has been suggested.

Anthony
0
 
LVL 75

Assisted Solution

by:Anthony Perkins
Anthony Perkins earned 50 total points
ID: 9593110
Since you need it in the format mm-dd-yyyy use the style = 110, as in:

Select Convert(varchar(10), getdate(), 110)

Anthony
0
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 9593131
Also, if all you want to do is save to a datetime column without the time, than use something like this:
Select Cast(Floor(Cast(GetDate() As float)) as datetime)

Sorry for the multiple posts.
Anthony
0
 

Author Comment

by:MovnOn
ID: 9593294
I'm adding 75 points to the question and will split them: lludden gets 125 for the first correct answer and 50 to Anthony for the additional info.

Fair enough?
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 9593470
That is fine with me.

Anthony
0
 
LVL 18

Expert Comment

by:lludden
ID: 9593720
Works for me.  Glad to be of help
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Star schema daily updates 2 38
query analyser in sql server 2016 express edition 2 26
Need help with a query 14 39
SQL Query Returns Records in SSMS but not Classic ASP 5 26
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

730 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