Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Manipulating DateTime Strings

Posted on 2009-07-03
9
278 Views
Last Modified: 2012-05-07
Hi

How do I efficiently convert a string being supplied to my SQL of the form:

"2009-07-29:13:25"

into the SQL datetime format:

"07/29/2009 1:25:00 PM" ?

My second issue:

I have been reading about UTC in the forum, and can see it is complex if dealing with different systems, ie MAC, Linux etc. But, at the end of the day, if I am just using Windows XP with SQL 2005 Server Express, is it true to say that the UTC datetime in SQL will be the same as the UTC datetime in another application on the same computer?

In other words: for the above datetime will the UTC value be the same regardless of the software application using it - ie. so long as I convert the datetime to a UTC value this will then be 'portable', at least on the same PC?
0
Comment
Question by:silchester
  • 5
  • 4
9 Comments
 
LVL 31

Expert Comment

by:RiteshShah
ID: 24770860
if you have not mistaken in giving date than look at this one





declare @str varchar(50)
set @str='2009-07-29 13:25'
 
select convert(varchar(50),CONVERT(datetime,@str,120),22)

Open in new window

0
 
LVL 31

Accepted Solution

by:
RiteshShah earned 500 total points
ID: 24770868
if you really have ":" between date and time than consider this one



declare @str varchar(50)
set @str='2009-07-29:13:25'
 
select convert(varchar(50),CONVERT(datetime,left(@str,10)+' '+right(@str,5),120),22)

Open in new window

0
 
LVL 31

Expert Comment

by:RiteshShah
ID: 24770883
>>I have been reading about UTC in the forum, and can see it is complex if dealing with different systems, ie MAC, Linux etc. But, at the end of the day, if I am just using Windows XP with SQL 2005 Server Express, is it true to say that the UTC datetime in SQL will be the same as the UTC datetime in another application on the same computer?<<


I guess in same computer, it will remain same.
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

Author Comment

by:silchester
ID: 24770949
Incredible! Fantastic, after thinking I was going to have to get another app to manipulate it all etc etc. Good one.

Am just having trouble using it in a computed column for a table:

convert(varchar(50),CONVERT(datetime,left([DATETIME TO CONVERT],10)+' '+right([DATETIME TO CONVERT,5),120),22)

Or am I going to have to put this into an after insert trigger?

Cheers
0
 

Author Comment

by:silchester
ID: 24770959
PS. Can I be cheeky and ask if there is a function that wil then take this converted datetime and give me the UTC value ?

Thanks
0
 
LVL 31

Expert Comment

by:RiteshShah
ID: 24770964
well you can put it in trigger for sure but don't understand what you want to say by "Computed Column for a table" can you please elaborate more?
0
 

Author Comment

by:silchester
ID: 24771057
Well, in the SQL 2005 table designer a column can be made a 'computer column' by putting a formula into its column properties. So, I tried inserting the convert code into it, but it does not like it - so i guess it does not see this code line as a 'formula'.

I have just tired it in a trigger, and it works. I just thought that the computed column would have been a neater way of automatically filling in a colum with the converted value (another column is holding the unconverted value).
0
 
LVL 31

Expert Comment

by:RiteshShah
ID: 24771078
first computed column is not a physical column and it used to compute the value as and when demanded so directly insert in computed column will not be possible.
0
 

Author Closing Comment

by:silchester
ID: 31599491
Thanks Ritesh - really helped me there.
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Compare data between two databases 16 109
Why is the output of this function is like this? 4 38
How to import SQL 2000 database to SQL 2014 5 138
Analysis of table use 7 54
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question