Click here to Skip to main content
15,896,423 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi

I want to run a query to replace the values in my date fields.
Currently it is as 2013/12/02 01:12:49:856 and is stored as varchar in the database
I want to replacce the / with a - on all the records
I have tried the following script
SQL
UPDATE AuditResults
SET lastupdated = REPLACE(lastupdated, N'/', N'-')

and
SQL
UPDATE AuditResults
SET lastupdated = REPLACE(lastupdated, '/', '-')

But this does not work.
Any other suggestions?

Thank you in advance
Posted
Comments
phil.o 5-Dec-13 6:34am    
Just my 2 cents: datetimes should be stored in the database with the appropriate datatype. Using varchar/nvarchar to hold datetime values is a quite bad pratice.
Member 9374423 5-Dec-13 6:38am    
I was not part of the initial developer team, I know this,but there's nothing i can do about that now, haha
phil.o 5-Dec-13 7:43am    
As I said, this was just my 2 cents about that; you are going to meet numerous kinds of troubles when you will have to do comparisons; and I'm even not talking about performance issues. Good luck with this inherited project :)

You can convert and update the records,

SQL
SELECT CONVERT(VARCHAR(23), GETDATE(), 121)


SQL
UPDATE AuditResults
SET lastupdated =CONVERT(VARCHAR(23), lastupdated , 121)




Reference:
 
Share this answer
 
v2
Comments
Member 9374423 5-Dec-13 6:46am    
I need to update the fields, not run a select
Ganesh Raja 5-Dec-13 6:48am    
Can u pls check conversion in my code
Member 9374423 5-Dec-13 7:48am    
HiThis is perfect thank you!!!!!!!!!!!
phil.o is right - storing datetime values as string is a spectacularly stupid decision, and should be changed (and the person who decided on it thumped round the head with something solid).

But...I just checked here and both versions worked fine, so I suspect your code rather than anything else. Did you add a WHERE clause that you didn't tell us about?
 
Share this answer
 
Comments
Member 9374423 5-Dec-13 6:49am    
Hi, I do realize this, but the change to that is too significant.
Then the where clause, I have tried with and without a where clause. Both do not work.
Please can you add the code you tried?
Kind regards
OriginalGriff 5-Dec-13 6:53am    
It's the same as your, with a different table and column name:
UPDATE Product SET Product_Name = REPLACE(Product_Name, N'/', N'-')
and
UPDATE Product SET Product_Name = REPLACE(Product_Name, '/', '-')
both work fine in SSMS...
Member 9374423 5-Dec-13 7:03am    
This does not work, are there any other solutions for this?
OriginalGriff 5-Dec-13 7:10am    
As I said - it does here.
Show us exactly what you are doing. Copy and paste from your code.
Member 9374423 5-Dec-13 7:11am    
I did in the initial file, that's what I'm using

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900