2

I've got a table setup which has populated data. In column "date", I have dates in the following format:

yyyymmdd i.e. 20131110

I have created a new field and called it newdate with the text format.

Then, I open up the SQL window and put the following in

UPDATE wl_daily
SET
   newdate = UNIX_TIMESTAMP(date)

For some reason, it is running correctly, however it only outputs NULL to all the rows. Also, the column name is blank for some reason

Any suggestions?

2 Answers 2

2

That's because your field in a string and you're trying to add timestamp to it which is not a string. You need to use a valid datetime field like timestamp for this to work.

Advice: don't store dates and times as strings. Store them in their native format. It makes working with dates and times much easier.

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

1 Comment

So I've set the date field to "date" however I still seem to be getting NULL in the newdate field. Any suggestions on what the 2 fields should be?
1

While John Cronde's answer is correct - it doesnt help your situtation

 UNIX_TIMESTAMP(STR_TO_DATE(`date`, '%Y%m%d'))

will do the conversion for example

SELECT UNIX_TIMESTAMP(STR_TO_DATE('20131111', '%Y%m%d')) 

returns

unix_timestamp(STR_TO_DATE('20131111', '%Y%m%d'))  
---------------------------------------------------
                                         1384128000   

You should only use this to convert your columns to the date specific columns. Converting each time you need a number will add load and slow down the query if used in production

8 Comments

I tried the following: UPDATE wl_daily SET newdate = UNIX_TIMESTAMP (STR_TO_DATE(date, '%Y%m%d')) however once again returned NULL
what is date should be the column containing the text ? quote it with backticks because date is a reserved word
date is the existing information, ie 20131110 and newdate will be the the timestamp version of date. I've just added backticks and still have the same issue.
can you set up a jsfiddle or try the select statement above ?
select statement about returned 1384128000
|

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.