Solved

Casting a datetime field to a smalldatetime field not working

Posted on 2003-10-21
7
3,034 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
  • 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
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
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.
Viewers will learn how the fundamental information of how to create a table.

910 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

19 Experts available now in Live!

Get 1:1 Help Now