Click here to Skip to main content
15,878,871 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
Hi All,

I am getting wrong output by using Substring function in one Sql server database but correct Output in another sql server database, but not getting the reason as to why this is happening.

In the below thing, For EG : - Point 1 is giving wrong output on some sql database server while Point 2 is giving correct output on another sql server database. Can you please help on this?



1.
C#
select Convert(nvarchar(8),Getdate(),112) + '_' + Substring(Convert(nvarchar(24),Getdate(),113),13,2) + Substring(Convert(nvarchar(24),Getdate(),113),16,2) + Substring(Convert(nvarchar(24),Getdate(),113),19,2)    


Date --> 2016-10-26 21:54:03.140
Output --> 20161026_1:4:3:

2.
C#
select getdate()


select Convert(nvarchar(8),Getdate(),112) + '_' + Substring(Convert(nvarchar(24),Getdate(),113),13,2) + Substring(Convert

(nvarchar(24),Getdate(),113),16,2) + Substring(Convert(nvarchar(24),Getdate(),113),19,2)



Date --> 2016-10-26 05:43:06.257
Output --> 20161026_054306

What I have tried:

I have tried checking this but not able to find the solution
Posted
Updated 26-Oct-16 7:11am
v2

1 solution

Try this:
SQL
select convert(nvarchar(8),getdate(),112)+'_'+
REPLACE(convert(nvarchar(8),getdate(),108),':','')


And the difference you are observing could be because of Collation, locale specific settings etc.
 
Share this answer
 

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