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
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, November 30, 2012 3:46 AM
Points: 18, Visits: 100
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: 2 days ago @ 11:38 AM
Points: 128, Visits: 486
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
Say Hey Kid

Say Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey Kid

Group: General Forum Members
Last Login: Yesterday @ 2:58 PM
Points: 687, Visits: 5,883
Try something like this:

select CONVERT(date, mydate, 103)
from mytable

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

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, November 30, 2012 3:46 AM
Points: 18, Visits: 100
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: Yesterday @ 9:47 PM
Points: 22,472, Visits: 30,138
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
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, November 30, 2012 3:46 AM
Points: 18, Visits: 100
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
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Yesterday @ 4:02 PM
Points: 984, Visits: 2,187

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: Thursday, April 03, 2014 10:10 AM
Points: 2,792, Visits: 4,874
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
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Yesterday @ 4:02 PM
Points: 984, Visits: 2,187
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 8:55 PM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Tuesday, January 15, 2013 11:11 AM
Points: 1,945, Visits: 2,782
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.


Books in Celko Series for Morgan-Kaufmann Publishing
Analytics and OLAP in SQL
Data and Databases: Concepts in Practice
Data, Measurements and Standards in SQL
SQL for Smarties
SQL Programming Style
SQL Puzzles and Answers
Thinking in Sets
Trees and Hierarchies in SQL
Post #1346898
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse