[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Casting a datetime field to a smalldatetime field not working

Posted on 2003-10-21
7
Medium Priority
?
3,057 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 500 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 200 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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
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
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

650 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