| Author |
Topic |
|
icw
Constraint Violating Yak Guru
378 Posts |
Posted - 2009-11-06 : 04:28:36
|
| Hi i have some date in a column (see below)A lot of the date are missing leading zeros and I need to add the leading zeros so that an APP can read them properly.but I can't figure out how to do the query. It might be soemthing simple but my mind had gone blank.The first one is 1999-4-1 this is yyyy-m-dI need to update it to yyyy-mm-ddalso I should point out the field is varchar (10)Hope that makes sense!1999-4-11999-4-11999-5-91999-4-61999-4-191999-11-281999-5-21999-04-241999-4-251999-3-101998-11-51999-4-251999-5-161999-4-281999-3-91999-5-271999-4-211998-11-51999-3-41999-3-241999-4-121999-5-91999-4-291999-3-241999-5-101999-5-251999-5-161999-5-271999-3-28 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-11-06 : 04:43:02
|
| What is the datatype of the date column?MadhivananFailing to plan is Planning to fail |
 |
|
|
icw
Constraint Violating Yak Guru
378 Posts |
Posted - 2009-11-06 : 04:44:34
|
| SOrry i just updated the original post with the data type varchar (10) |
 |
|
|
rajdaksha
Aged Yak Warrior
595 Posts |
Posted - 2009-11-06 : 04:51:59
|
| HiIS this fine for youSELECT CONVERT(VARCHAR(30),CONVERT(DATETIME,'1999-4-1'),111)-------------------------R... |
 |
|
|
icw
Constraint Violating Yak Guru
378 Posts |
Posted - 2009-11-06 : 04:58:41
|
| Thanks but I don't want to convert it to a datetime field.I still want it to be Varchar (10) as the application requires that.I just need to look for where a leading zero is missing and then add one. |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-11-06 : 04:59:56
|
quote: Originally posted by icw SOrry i just updated the original post with the data type varchar (10)
1 Always use proper DATETIME datatype to store dates2 Let Front end application do the formatation (ASP in your case)3 Use this in ASP PageYEAR(Date()) & "-" & Pd(Month(date()),2) & "-" & Pd(DAY(date()),2) MadhivananFailing to plan is Planning to fail |
 |
|
|
icw
Constraint Violating Yak Guru
378 Posts |
Posted - 2009-11-06 : 05:02:37
|
| I really appreciate your help many thanks to you.But its not my Application and is hard coded so I don't have any choice. |
 |
|
|
rajdaksha
Aged Yak Warrior
595 Posts |
Posted - 2009-11-06 : 05:05:19
|
quote: Originally posted by icw Thanks but I don't want to convert it to a datetime field.I still want it to be Varchar (10) as the application requires that.I just need to look for where a leading zero is missing and then add one.
To store the data always use proper datatype…formation we can do it in display part what madhi is said.-------------------------R... |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-11-06 : 05:23:38
|
quote: Originally posted by icw I really appreciate your help many thanks to you.But its not my Application and is hard coded so I don't have any choice.
So, you dont have any chance to modify the ASP page?Do you have access to modify the procedure?MadhivananFailing to plan is Planning to fail |
 |
|
|
icw
Constraint Violating Yak Guru
378 Posts |
Posted - 2009-11-06 : 05:23:55
|
| Thank you but I don't have a choice. I am not designing the software |
 |
|
|
icw
Constraint Violating Yak Guru
378 Posts |
Posted - 2009-11-06 : 05:25:02
|
| The software is a service-desk application called HEAT from Frontrange Solutions.It uses Varchar (10) for date fields. |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-11-06 : 05:28:33
|
quote: Originally posted by icw The software is a service-desk application called HEAT from Frontrange Solutions.It uses Varchar (10) for date fields.
The only way is updating your tableupdate your_tableset date_col=convert(varchar(10),cast(date_col as datetime),120)Now goto the page and seeMadhivananFailing to plan is Planning to fail |
 |
|
|
icw
Constraint Violating Yak Guru
378 Posts |
Posted - 2009-11-06 : 05:33:29
|
| I ran this update but the data hasn't changedThe table is called Profile the field is called tillsindateupdate profileset tillsindate = left(convert(varchar,tillsindate,120),10) |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-11-06 : 05:38:33
|
| I already edited my replyHere is the oneupdate your_tableset date_col=convert(varchar(10),cast(date_col as datetime),120)MadhivananFailing to plan is Planning to fail |
 |
|
|
rajdaksha
Aged Yak Warrior
595 Posts |
Posted - 2009-11-06 : 05:41:28
|
| Hiboth will workSELECT CONVERT(VARCHAR(10),CONVERT(DATETIME,'1999-4-1'),111)SELECT CONVERT(VARCHAR(10),CAST('1999-4-1' AS DATETIME),120)-------------------------R... |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-11-06 : 05:46:04
|
as what rajdaksha suggested, convert to datetime and then convert back to varchar but use style 121update your_tableset datecol = convert(varchar(10), convert(datetime, datecol, 121), 121) KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
icw
Constraint Violating Yak Guru
378 Posts |
Posted - 2009-11-06 : 06:07:43
|
| Thanks guys that works great!Have a nice weekend! |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2009-11-06 : 06:12:40
|
The last (or not the last) question could be:What is about data coming new into the table - is the problem still the same in new inserted records?Is it possible to have a trigger for that? No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
|