SQL Server中的子串命令在两个不同的SQL Server数据库上提供不同的输出 [英] Substring command in SQL server is giving different output on 2 different SQL server databases

查看:60
本文介绍了SQL Server中的子串命令在两个不同的SQL Server数据库上提供不同的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我在一个Sql server数据库中使用Substring函数输出错误但在另一个sql server数据库中输出正确,但没有得到原因至于为什么会这样。



在下面的内容中,对于EG: - Point 1在某些sql数据库服务器上输出错误,而Point 2在另一个sql server数据库上提供正确的输出。你能帮忙吗?







1.

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.

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)    





日期 - > 2016-10-26 21:54:03.140

输出 - > 20161026_1:4:3:



2.



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

2.

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)







日期 - > 2016-10-26 05:43:06.257

输出 - > 20161026_054306



我尝试了什么:



我试过检查这个但无法找到解决方案




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

推荐答案

试试这个:

Try this:
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.


这篇关于SQL Server中的子串命令在两个不同的SQL Server数据库上提供不同的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆