473,786 Members | 2,445 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

convert string to date

10 New Member
Hi,
I have a database in mysql i have column which is text and i need to convert it to date for query purposes.

the format for the string in the column is dd/mm/yyyy i need to convert this to date.

please help
Apr 7 '09 #1
6 5534
code green
1,726 Recognized Expert Top Contributor
MySql STR_TO_DATE
http://dev.mysql.com/doc/refman/5.0/...on_str-to-date
Apr 7 '09 #2
nady
10 New Member
thanks for the reply
but i want to search also and it does not work if i want to run a query with where clause.
Apr 7 '09 #3
Atli
5,058 Recognized Expert Expert
Hi.

If your date is in the format: "yyyy/mm/dd", you can simply alter the structure of the table, replacing the text type with Date.

For example, if you had this table:
Expand|Select|Wrap|Line Numbers
  1. CREATE TABLE txt(theDate varchar(10));
  2.  
  3. INSERT INTO txt(theDate) VALUES
  4. ('2009/04/01'), ('2009/04/02'), ('2009/04/03');
You could simply do:
Expand|Select|Wrap|Line Numbers
  1. ALTER TABLE txt CHANGE theDate theDate Date;
Which should automatically convert your text dates over to a MySQL recognized date format, meaning that MySQL can actually process the values as dates and use them for searches and sorting.
Expand|Select|Wrap|Line Numbers
  1. mysql> SELECT * FROM txt;
  2. +------------+
  3. | theDate    |
  4. +------------+
  5. | 2009-04-01 |
  6. | 2009-04-02 |
  7. | 2009-04-03 |
  8. +------------+
  9. 3 rows in set (0.00 sec)
  10.  
  11. mysql> SELECT * FROM txt WHERE DAY(theDate) > 1;
  12. +------------+
  13. | theDate    |
  14. +------------+
  15. | 2009-04-02 |
  16. | 2009-04-03 |
  17. +------------+
  18. 2 rows in set (0.01 sec)
Before you do this!
Remember to back up your data... just in case something doesn't go as expected.
Apr 8 '09 #4
nady
10 New Member
Hi,
thanks for the reply it works with yyyy/mm/dd
but how to apply the same for dd/mm/yyyy

thanks
nadir
Apr 12 '09 #5
gregerly
192 Recognized Expert New Member
You will probably be better off using the proper format instead of hacking it to get it working a way it's not supposed to. I would use whatever language your using to interact with the database to alter the format and insert the data as yyyy-mm-dd. I use php, so I'll show you a little example:

Expand|Select|Wrap|Line Numbers
  1. //date your working with
  2. $date = '09/22/1979';
  3.  
  4. //create an array of the date pieces
  5. $temp = explode('/',$date);
  6.  
  7. //reformat the date to work with mysql
  8. $formatted_date = $temp[2] . '-' . $temp[1] . '-' . $temp[0];
  9.  
  10. //now do something with your new formatted date
Again, I don't know what language your using to interact with the database, but I imagine you can accomplish the same with just about any language.

Good luck!
Apr 12 '09 #6
mwasif
802 Recognized Expert Contributor
@nady
MySQL does not support this date format. You must have to use date in yyyy/mm/dd format.
Apr 14 '09 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

13
9305
by: perplexed | last post by:
How do you convert a user inputted date to a unix timestamp before insterting it into your database? I have a form, with a textfield for a date that the user inputs in the format mm-dd-yyyy and three dropdow boxes for hours, minutes, and AM/PM. All of these need to be considered together and converted to one Unix Timestamp and then inserted to the MYSQL date field. The type of field is INT (11) so that I can instead of the standard...
2
39809
by: Hector A | last post by:
Hi I'm trying to convert a string that already looks like a date to a date that I can use when I pass it from java to the database. I receive the date in format yyyy-mm-dd and I need it to be a date variable in 'mm/dd/yyyy' or 'm/dd/yyyy' format. My code is shown below. Any suggestions? code: String sailYear = (String)sail_date.toString().substring(0,4);
2
5089
by: Franck | last post by:
Hi, 'm gettin mad about date conversion. Here is the point. Got and add-in for Excel which call functions from a web service (on a remote server) The remote server has regional settings set to "en-UK" and date to
12
3862
by: DC Gringo | last post by:
How can I convert this pubLatest to a date with format "m/d/yyyy"? Dim pubLatest As New Date pubLatest = Me.SqlSelectCommand1.Parameters("@pubLatest").Value -- _____ DC G
1
4607
by: abcabcabc | last post by:
I write an application which can let user define own date format to input, How to convert the date string to date value with end-user defined date format? Example, User Defined Date Format as "dd/MM/yyyy" input as "01082003" convert to date value as, 01 Aug 2003 Example, User Defined Date Format as "yyyy,dd,MM"
4
39345
by: perryclisbee via AccessMonster.com | last post by:
I have dates of service for several people that range all over each month. ie: patient had dates of service of: 7/3/2006, 7/24/2006 and 7/25/2006. I need to create a new field via a query that will convert each of the records of these service dates to the first date of that month, with results showing: 7/1/2006, 7/1/2006, 7/1/2006. How would you place an expression on a query that will convert any given date to the first day of the month...
6
21206
by: vunet.us | last post by:
How can I convert UTC time such as 1173451235415 to a local time I can read? Thank you
8
6057
by: deepak_kamath_n | last post by:
Hello, I have the following scenario: 1. My application receives the date from another application as a string 2. The other application is running in a different time zone as compared to my app. 3. I need to convert the received date string in to a date w.r.t my local time zone.
4
30267
by: Ashraf Ansari | last post by:
Hi, How Can I convert MM/dd/yyyy format into dd/MM/yyyy and the date which is converted into dd/MM/yyyy should be in DateTime format. I do not want to store it as a string. Please help Thanks in advance
4
3020
by: =?Utf-8?B?YW5kcmV3?= | last post by:
I am running an ASP.net program written in VB. At one point I try to convert a date string into a date time object... this string is from a central dev server and the code works on many other machines. Convert.ToDateTime("") I get the string in the format MM/DD/YYYY (06/26/2007) The code breaks and I get the "String was not recognized as a valid DateTime" error message unless I manually change the input string to
0
9492
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10108
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9960
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8988
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7510
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5397
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5532
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3668
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.