Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
Hello there,

Need your help again.
I am curre tly trying to convert an nvarchar value in correct datetime format. There is a column in SQL Local_time it is nvarchar and it has values like 1160119104041, if i break this down it will be like '1' 'yy' 'mm' 'dd' 'hh' 'mm' ss'.
What i want to display is like 2016-01-01 10:40:41.

Note that in nvarchar value an extra 1 is available i want to remove it when displaying the correct datetime.

I would be really thankful for your help and looking forward to it.

What I have tried:

Searched the web and google but i have found solutions like how yo convert nvarchar to datetime but not with formatting
Posted
Updated 21-Oct-16 0:12am
v2

You can use LEFT() and RIGHT() to do so.
Example:
SQL
declare @test varchar(30)
set @test='1160119104041'

select '20'+left(right(@test,LEN(@test)-1),2)+'-'+left(right(@test,LEN(@test)-3),2)+'-'+left(right(@test,LEN(@test)-5),2)+' '+left(right(@test,LEN(@test)-7),2)+':'+left(right(@test,LEN(@test)-9),2)+':'+left(right(@test,LEN(@test)-11),2)


You may want to cast it to DATETIME using CAST(expressio AS DATETIME).

Note: this is just a way. You may still look for better options or suggestions.
You may also need to think, what if year is 98 and you want it to be 1998!

Hope, it helps :)


Update
You can create a function like -
SQL
CREATE FUNCTION GetDateFromVarchar
(
	@InputValue VARCHAR(30)
)
RETURNS DATETIME
AS
BEGIN
	DECLARE @ReturnValue VARCHAR(30)

	SELECT @ReturnValue= '20'+LEFT(RIGHT(@InputValue,LEN(@InputValue)-1),2)+'-'+LEFT(RIGHT(@InputValue,LEN(@InputValue)-3),2)+'-'+LEFT(RIGHT(@InputValue,LEN(@InputValue)-5),2)+' '+LEFT(RIGHT(@InputValue,LEN(@InputValue)-7),2)+':'+LEFT(RIGHT(@InputValue,LEN(@InputValue)-9),2)+':'+LEFT(RIGHT(@InputValue,LEN(@InputValue)-11),2)

	RETURN CAST(@ReturnValue AS DATETIME)

END
GO

Note: It not advised to copy paste code. Check and modify according to your need. I haven't tested this yet but should work.
 
Share this answer
 
v4
Comments
Faran Saleem 21-Oct-16 4:25am    
Thanks for your reply..:)

But a question this will only convert a specific date from that column. What i want is to perform this thing on every record of that column so i can not use parameters and set value each time.
Suvendu Shekhar Giri 21-Oct-16 4:29am    
You just need to replace @test with the coulmn name in your SELECT query. You can also create a FUNCTION and call it where ever is required.
Let me know if you want help in creating the FUNCTION.
Faran Saleem 21-Oct-16 4:32am    
Yes sure that would be ally helpful..:) and also if you can tell me the syntax of how can i cast it after converting in the correct format..:)
Faran Saleem 21-Oct-16 4:45am    
And also i have just ran your query.. i have replaced @test with my column name that is local_time.. but it is giving an error "Invalid length parameter passed to the right function"
Suvendu Shekhar Giri 21-Oct-16 4:53am    
Please check the updated solution
0) What kind of idiot designs a SQL database to store a datetime as a nvarchar?

1) Which "1" is the one you want to strip? At the beginning, or at the end? Do you even know what the actual purpose of the extra "1" is (why it's there)?

2) The way your nvarchar string is shown, there's no way to parse the string into a datetime with any kind of confidence that the year is accurate because it's not a 4-digit year.
 
Share this answer
 
v2

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