Click here to Skip to main content
13,596,716 members
Rate this:
Please Sign up or sign in to 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?

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:

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 26-Oct-16 3:00am
Updated 26-Oct-16 7:11am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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

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

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Cookies | Terms of Service
Web04-2016 | 2.8.180621.3 | Last Updated 26 Oct 2016
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100