1

In a table I have a stored string column "MM/DD/YYYY HH:MM:SS" and I'm looking for a query to return just the "MM/DD/YYYY" part.

Any ideas?

2
  • 1
    @user693121 - Is this MS Sql Server? Commented Apr 6, 2011 at 14:35
  • Yes. And it turns out this is a DATETIME field NOT a string Commented Apr 6, 2011 at 14:54

4 Answers 4

4

If the column type is char or varchar, then

SELECT LEFT(colname, 10)

will suffice. If it's a datetime type, then try

SELECT DATE_FORMAT(colname , "%d/%m/%Y")

Sign up to request clarification or add additional context in comments.

3 Comments

DATE_FORMAT' is not a recognized built-in function name :(
DATE_FORMAT is indeed a MySQL function. You must have added an extra quote or something.
DATE_FORMAT is not compatible with Microsoft SQL 2005. Trying to use a variation of "CONVERT(VARCHAR(10), column, 101) AS [MM/DD/YYYY]" but VARCHAR and CHAR are not recognized either.
1

You should be using a MySQL DATETIME field instead of a string field, really. That would allow you to apply date and time functions, which help tremendously when dealing with temporal data.

In your case, since your data is a string type, and not in MySQL Isodate format (YYYY-MM-DD), you can work only using string functions like SUBSTRING() and specialisations thereof (LEFT, ...).

http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html Date and Time functions overview

http://dev.mysql.com/doc/refman/5.5/en/string-functions.html String functions

1 Comment

Turns out they are DATETIME fields. What would I apply?
0

i think you can use substring !!!

SELECT SUBSTRING(TimeStamp,7,10);

Comments

0

if it is a string use SUBSRT() or LEFT() to extract required part, however it would be wise to have it stored as datatime type

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.