Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help with a date function in mysql

Status
Not open for further replies.

AnnetteB1305

Technical User
Aug 17, 2011
166
GB
Hello,

I am trying to create a command from mysql database to get a report completed and am having issues changing a string field to a date. I have tried str_to_date and date and cdate and various others and nothing is working. There is an error in the sql syntax everytime. Can anyone help please.

Thanks,

Annette
 
Hi

Another syntax error : in the isnull() function's parameter the closing backtick is missing after the field name. ( Personally I hate them. )

If still get error, let use try to reduce the amount of code. Is this cut off version still throwing the error ?
SQL:
[b]SELECT[/b] [teal]*[/teal]

[b]FROM[/b] opencall1

[b]WHERE[/b] opencall1[teal].[/teal]priority[teal]<>[/teal][green][i]'NL-MDIS SALES OFF SEND'[/i][/green]  
[b]and[/b] isnull[teal]([/teal]opencall1[teal].[/teal]closedate[teal])[/teal]
[b]or[/b] str_to_date[teal]([/teal]opencall1[teal].[/teal]closedate[teal],[/teal][green][i]'%d/%m/%Y %H:%i:%S'[/i][/green][teal])[/teal] [teal]>[/teal] [teal]([/teal]date_add[teal]([/teal]curdate[teal](),[/teal]interval [teal]-[/teal][purple]8[/purple] day[teal]))[/teal]


Feherke.
 
Hello,

Ok I've stripped each part out into a query analyser. I've amended the isnull as couldn't get that to work to (`opencall1`.`closedate='') and all seems to work apart from the string to date section. I have tried this in the query analyser:
select str_to_date (closedate,'%d/%m/%Y %H:%i:%S') from opencall

and getting this error:

[Err] 1064 - You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near '(closedate,'%d/%m/%Y %H:%i:%S') from opencall' at line 1

Bizarre!!!



 
Well it is in crystal for some reason it has named the database opencall1 in crystal however the actual database name is opencall.

 
Hi

You are not confusing the database and the table name right ?

Sorry, I have no more idea. That last query posted by you works for me in MySQL 5.1.41 on Linux :
Code:
[blue]mysql>[/blue] select closedate as original_string, str_to_date(closedate,'%d/%m/%Y %H:%i:%S') as converted_to_date, str_to_date(closedate,'%d/%m/%Y %H:%i:%S')>(date_add(curdate(),interval -8 day)) as compared from opencall;
+---------------------+---------------------+----------+
| original_string     | converted_to_date   | compared |
+---------------------+---------------------+----------+
| 01/02/0003 04:05:06 | 0003-02-01 04:05:06 |        0 |
+---------------------+---------------------+----------+
1 row in set (0.00 sec)

Feherke.
 
Hi

Just one more thing to check : what is the [tt]describe[/tt] command saying ? For my test table :
Code:
[blue]mysql>[/blue] describe opencall closedate;
+-----------+--------------+------+-----+---------+-------+
| Field     | Type         | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+-------+
| closedate | varchar(100) | YES  |     | NULL    |       |
+-----------+--------------+------+-----+---------+-------+
1 row in set (0.01 sec)


Feherke.
 
+-----------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+-------+
| closedate | varchar(20 ) | YES | | NULL | |
+-----------+--------------+------+-----+---------+-------+
 
what about a convert function would that work? and do you know the convert function in mysql
 
Hi

Convert for what ? For converting from varchar to datetime there is the str_to_date() function you are already using in that query.

By the way, have you tried that query outside of that Crystal ?


Feherke.
 
Yes tried it in query analyser that was the last error I sent.

I meant the Convert( ) function Convert(closedate,date) or the cast function.

Just thinking it isn't because the closedate may be empty, is it?
 
04/11/2009 07:15:41 this is how closedate looks, so I've got enough expressions now haven't I.....don't get it
 
As feherke said, you need to start breaking down the query. If you remove the date comparison part does it work? Or does it still throw an error?

I have a feeling the error is not in that section but some place before.

Start removing sections, doesn't matter if the results are not what you want, once you get to a point where the query runs add a portion back in and check again, do that until the error comes back.

You can also try your date conversion alone to make sure its right.
Code:
SELKCT str_to_date(closedate,"%d/%m/%Y") as convertedDate from opencall1;



----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
Hello,

I've already done this as stated above. The error is occurring with SELECT str_to_date(closedate,"%d/%m/%Y") as convertedDate from opencall

and

SELECT str_to_date(closedate,"%d/%m/%Y %h:%i:%s") as convertedDate from opencall

 
O.k, so what version of MYSQL do you have? Is it at all possible it doesn't have the functions required for the conversion and so is throwing an error?



----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
Pardon my ignorance but how do I find out the version, I'm afraid I'm more SQL than MYSQL so struggling a bit with this one. Thanks in advance.
 
Code:
SELECT version();



----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
Someone is definitely getting at me....getting an error with that:-

[Err] 1064 - You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near '()' at line 1
 
If your mysql can;t even run th verion fcuntion there's somethng serisouly wrong with it.

O.k try:

Code:
SHOW variables LIKE "%version%";

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top