Click here to Skip to main content
15,887,350 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
I have a date of birth field which have values like 180897, 150399 etc and i want to convert them as like 18/aug/1997 , 15/mar/1999 ..
Posted

Try This:

SQL
DECLARE @input varchar(8), @newDate varchar(10);
SET @input  = '180897'
 
SET @newDate   = LEFT(@input  , 2) + '/' + SUBSTRING(@input, 3,2) + '/' + RIGHT(@input, 2)
SELECT convert(nvarchar,convert(datetime, @newDate , 5),106)
 
Share this answer
 
v2
First get the values as dates:

SQL
DECLARE @input_string varchar(8)
SET @input_string = '180897' -- here goes your date

DECLARE @new_string varchar(10)
SET @new_string = LEFT(@input_string, 2) + '/' + SUBSTRING(@input_string, 3,2) + '/' + RIGHT(@input_string, 2)

DECLARE @new_date DATETIME

SET @new_date = CONVERT(DATETIME, @new_string, 3) -- 3 represents dd/mm/yy, you can adapt if you change "." to some other separator
- here is the link[^].

Then you re-cast back to string you want:
SQL
SET @new_string = DAY(@new_date) + '/' + LEFT(DATENAME(MONTH,@new_date),3) +'/' + YEAR(@new_date) -- you might need to cast day and year into varchar again.


Slight simplyfication is to save varchar values while parsing instead of creating datetime object and use DATENAME LEFT(MONTH, middle_part_from_above), 3)


If this helps please take time to accept the solution.
 
Share this answer
 
Comments
aarif moh shaikh 11-Nov-14 4:26am    
i need '18/aug/1997' types of output ..
i have more than 61000 records..
King Fisher 11-Nov-14 4:44am    
what have you Tried?
Sinisa Hajnal 11-Nov-14 5:59am    
What the number of records has to do with anything? Make a function and call it in a select on the table. Or do all that parsing inline, I just broke it into variables for clarity.

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