Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Convert Text to Date DataType Expand / Collapse
Author
Message
Posted Tuesday, August 14, 2012 11:01 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, July 23, 2015 10:34 AM
Points: 27, Visits: 123
Hello Everyone,
I have an sql table which stores dates from some other system as text.

Eg.: DD/MM/YYYY ,31/12/2012.

I have made a query which fetches these dates and put them in a temp table and i have declared varchar(10) as Datatype for this coloumn.

But i want this coloumn to be in date data type.

I need this because i am using this query in SSRS and when i export the result to excel it needs to be in Date DT.

Thanks





Regards
Ravi T

Post #1344888
Posted Tuesday, August 14, 2012 11:03 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, July 20, 2015 7:09 AM
Points: 130, Visits: 503
Can't you change the data type of the column in your temp table to accomodate?

Mark



Post #1344891
Posted Tuesday, August 14, 2012 11:27 AM
Right there with Babe

Right there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with Babe

Group: General Forum Members
Last Login: Monday, July 20, 2015 10:48 AM
Points: 760, Visits: 6,365
Try something like this:

select CONVERT(date, mydate, 103)
from mytable

Post #1344907
Posted Tuesday, August 14, 2012 12:01 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, July 23, 2015 10:34 AM
Points: 27, Visits: 123
Hi Mark,
yes i can do that but,how will sql know that my text is in dd/mm/yyyy format and not in mm/dd/yyyy or smthing else.


rookie here


regards
Ravi T
Post #1344933
Posted Tuesday, August 14, 2012 12:04 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: 2 days ago @ 10:23 PM
Points: 21,921, Visits: 34,685
santa326 (8/14/2012)
Hi Mark,
yes i can do that but,how will sql know that my text is in dd/mm/yyyy for and not in mm/dd/yyyy or smthing else.



regards
Ravi T


The third value in the convert statement tells SQL Server the string value of the data/time is in dd/mm/yyyy format.

For more information, read this:

http://msdn.microsoft.com/en-us/library/ms187928.aspx



Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1344936
Posted Thursday, August 16, 2012 10:37 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, July 23, 2015 10:34 AM
Points: 27, Visits: 123
HI all,
Ive hit another wall!!!!!!!!.......i have another data set which has date stored as text and the format is dd/mm/yy.Now how do i go about converting this into date datatype?


I have gone through few date time system..n i dint find any number for this type of date.


Regards
Ravi T
Post #1346329
Posted Thursday, August 16, 2012 11:39 PM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Thursday, July 16, 2015 4:33 PM
Points: 1,032, Visits: 2,270

Instead of 103 use 03


DECLARE @D CHAR(10) = '17/08/12'
SELECT CONVERT(date, @D, 03)


Post #1346337
Posted Friday, August 17, 2012 3:01 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Tuesday, July 28, 2015 8:11 AM
Points: 2,926, Visits: 5,408
rVadim (8/16/2012)

Instead of 103 use 03


DECLARE @D CHAR(10) = '17/08/12'
SELECT CONVERT(date, @D, 03)




03? That is old ambulance number in Russia, you can use just 3 ...

Actually, SQL can convert '17/08/12' and '17/08/2012' into date without help of the third (dateformat) parameter. You are facing the issue due to the default format on your server is in American style (mm/dd/yyy). You can easily change it any time to whatever you like. The following example will explain it to you in a more visual way:

SET DATEFORMAT  DMY
DECLARE @D CHAR(10) = '17/08/12'
SELECT CONVERT(date, @D)
SET @D = '17/08/2012'
SELECT CONVERT(date, @D)
GO

SET DATEFORMAT MDY
DECLARE @D CHAR(10) = '8/17/12'
SELECT CONVERT(date, @D)
SET @D = '08/17/2012'
SELECT CONVERT(date, @D)
GO



_____________________________________________
"The only true wisdom is in knowing you know nothing"
"O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!"
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
Post #1346403
Posted Friday, August 17, 2012 9:14 PM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Thursday, July 16, 2015 4:33 PM
Points: 1,032, Visits: 2,270
Eugene Elutin (8/17/2012)[hr03? That is old ambulance number in Russia, you can use just 3 ...


Oh yea, 03 Скорая Помощь. Long time ago, in another life...
Post #1346797
Posted Saturday, August 18, 2012 9:23 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: 2 days ago @ 10:23 PM
Points: 21,921, Visits: 34,685
CELKO (8/18/2012)
CAST (SUBSTRING (IN_STR, 7, 4)+'-'
+SUBSTRING (IN_STR, 3, 2))+'-'
+SUBSTRING (IN_STR, 1, 2)
AS DATE)

cut up the string and put the Fields into ISO-8601 formatm then cast to DATE.


Really, why do all the string spliting and concatenation when the a) the text string is already in dd/mm/yy format, and b) you can use the format codes (3 or 103) in the convert statement anyway? A lot of extra work for no real value.



Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1346901
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse