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
Solved

SQL Getting date part of a DateTime field error

Posted on 2014-10-17
3
187 Views
Last Modified: 2014-10-18
Hi
I am getting the following error in trying to run the SQL statement below. The column in a client's database
is a DateTime field
'DATE' is not a recognized built-in function name.

Select Phone, [First Name], [Machine operator],[Drilled To]-[Drilled From] As [Drilled m], DATE([Date]) as TheDate From Performance Inner Join People On [Machine operator] = [Operator COY] Where [Drilled To]-[Drilled From] > 10
0
Comment
Question by:murbro
3 Comments
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 250 total points
ID: 40387993
you can use CAST:
CAST([Date] as DATE) as TheDate

Open in new window

0
 
LVL 48

Assisted Solution

by:PortletPaul
PortletPaul earned 250 total points
ID: 40388065
Depending on your SQL Server version you can output just the date without the time portion using CONVERT() or FORMAT()

SQL Server 2012 on:

FORMAT([date],'yyyy-mm-dd')

with the format() function you can alter the output to 'mm-dd-yyyy' or whatever you prefer


Without the format() function use CONVERT(varchar(length),[Date_field],style_number)

e.g.
CONVERT(varchar(10),[Date_field],120)

Here you need to know what the style numbers refer to, refer to a list of those is here. Note that in the above example the output is truncated at 10 characters hence you will not see any time portion.

If you use CAST([date_field] as date) then the default display format is applied, which might be, or might not be, the desired outcome
0
 

Author Closing Comment

by:murbro
ID: 40388648
Thanks
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

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.
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
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.

860 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