Will
Will

Reputation: 1164

SQL Syntax question

I'm trying to remember the syntax to change a date field into a string. I know I'm close but not 100% correct. Here is what I'm using so far: TO_CHAR(FIELD_NAME). I'm using an Access database. The error I'm getting is: undefined expression. Any help would be very much appreciated.

Upvotes: 1

Views: 5126

Answers (5)

adamcodes
adamcodes

Reputation: 1606

Use either CStr(dateField) or Format(dateField) to convert. Additionally you can add parameters to Format() to show it in a different format, such as:

Format(dateField, "general date") 9/12/2010
Format(dateField, "long date") Monday, September 12, 2011

Upvotes: 3

Conrad Frix
Conrad Frix

Reputation: 52645

Given that you're using MS Access and its a date field you're probably not just looking to convert to string but to also format the Date. If that is indeed the case you'll want the Format function

SELECT Format ([DateCreate], "yyyy/mm/dd") AS Foo
FROM MSysObjects;

Upvotes: 1

Ryan Ternier
Ryan Ternier

Reputation: 8804

I assume SQL Server as you're questions in the past are .NET Questions.

Use CONVERT http://msdn.microsoft.com/en-us/library/ms187928.aspx

Upvotes: -1

James Johnson
James Johnson

Reputation: 46047

You can use the CONVERT function, like this:

CONVERT(VARCHAR, DateField, 100)

Here's a link that shows the different date formats you can use:

http://www.sql-server-helper.com/tips/date-formats.aspx

Upvotes: -1

Anthony
Anthony

Reputation: 744

If you're using SQL Server, try out CAST or CONVERT

Upvotes: -1

Related Questions