首页 \ 问答 \ 如何将一个数字转换为在sql server中使用0前缀的字符串(How to cast a number to string with 0 prefix in sql server)

如何将一个数字转换为在sql server中使用0前缀的字符串(How to cast a number to string with 0 prefix in sql server)

在sql server中,如果我有一个数字n ,并且n>=0 and n<=100 ,如何将它转换为一个带有sql的字符串?

1 => '01'
2 => '02'
...
10 => '10'
99 => '99'

它就像printf一样。

printf ('%2d", n);

In sql server, if I have a number n, and n>=0 and n<=100, how to cast it into a string with sql?

1 => '01'
2 => '02'
...
10 => '10'
99 => '99'

It is something like printf.

printf ('%2d", n);

原文:https://stackoverflow.com/questions/15674236
更新时间:2023-01-12 22:01

最满意答案

此问题是由构造函数中的隐式转换引起的。 字符串文字 (例如代码中的字符串文字)存储为const char类型。 因为您没有采用此类型的构造函数,所以编译器会尝试查找转换为可在其中一个构造函数中找到的类型。

在这种情况下, const char*转换为bool QString更容易,所以当你这样做时:

DB_Variable foo("some_name"," ");

构造函数

DB_Variable(QString name, bool newValue):

叫做。

请注意,您看到的行为不是因为" "得到的处理方式与任何其他字符串文字不同,只是因为您很可能没有类型为bool, bool的构造函数(所有构造函数都将QString作为第一个论点?)。 如果你有一个如下构造函数,你可能会:

DB_Variable(bool test1, bool newValue):

当你做了一些像DB_Variable foo("some_name"," ");这样的事情时,就会调用它DB_Variable foo("some_name"," ");

要获得您想要的结果,您可以传递QStrings如下所示:

DB_Variable foo(QString("some_name"), QString());

或者也许定义一个构造函数,它将const char*作为第二个参数。


This problem results from implicit conversions going on in the constructor. String literals, such as the one in your code, are stored as const char types. Because you didn't have a constructor taking this type the compiler tries to find the conversion to a type that it can find in one of your constructors.

In this case const char* converts to bool easier that QString so when you do:

DB_Variable foo("some_name"," ");

The constructor

DB_Variable(QString name, bool newValue):

Is called.

Note that the behavior you are seeing isn't due to " " getting treated differently than any other string literal, it's just that you most likely didn't have a constructor with the types bool, bool (did all your constructors take a QString as the first argument?). Chances are if you had a constructor such as the following:

DB_Variable(bool test1, bool newValue):

Then this would have been called instead when you did something such as DB_Variable foo("some_name"," ");

To get the results you wanted you could pass in QStrings like so:

DB_Variable foo(QString("some_name"), QString());

Or perhaps define a constructor that takes const char* for the second parameter.

相关问答

更多
  • 来自MDN电话() : thisArg 这为调用fun提供了值。 请注意,这可能不是该方法看到的实际值:如果该方法是非严格模式代码中的函数,则null和undefined将被全局对象替换,并且原始值将被装箱 。 原语[又名数字/字符串]被放置在一个容器对象中,所以它就像你看到它一样工作。 所以它基本上是在做什么 > var x = "string"; > typeof x "string" > var temp = new String(x); > typeof temp "object" From MD ...
  • 如果你有C ++ 11,你可以使用委托构造函数: A(char const* s) : A(std::string(s)) { } 布尔转换构造函数被选为std::string的原因是因为从char const*到bool的转换是标准转换,而std::string是用户定义的转换。 标准转换的排名高于用户定义的转化。 If you have C++11 you can use a delegating constructor: A(char const* s) : A(std::string(s)) { ...
  • 而不是仅使用单引号,使用NOWDOC会更好。 在这种情况下,你根本不需要进行任何转义,即使你的字符串包含' - 并且第一行和最后一行更容易阅读。 $string = <<<'END' $#!+ - shit $$ - money $46 - money for sex $h!t - shit $xy - sexy /. - Slashdot *4u - Kiss for you *67 - unknown *eg* - evil grin 07734 ...
  • 此问题是由构造函数中的隐式转换引起的。 字符串文字 (例如代码中的字符串文字)存储为const char类型。 因为您没有采用此类型的构造函数,所以编译器会尝试查找转换为可在其中一个构造函数中找到的类型。 在这种情况下, const char*转换为bool QString更容易,所以当你这样做时: DB_Variable foo("some_name"," "); 构造函数 DB_Variable(QString name, bool newValue): 叫做。 请注意,您看到的行为不是因为" "得 ...
  • 你误解了教程。 它会说类型签名应该是 is_prime :: (Integral a) => a -> Bool -- NOT Integer a 这些是不同的类型: Integer -> Bool 这是一个函数,它接受Integer类型的值并返回一个Bool类型的值。 Integral a => a -> Bool 这是一个函数,它接受a类型的值并返回一个Bool类型的值。 什么是? 它可以是实现Integral类型类的任何类型的调用者的选择,例如Integer或Int 。 ( Int和In ...
  • 由于BOOL被定义为int ,所以你不知道没有检查你移植的代码是否依赖于它是一个int 。 因此,我会去做类似的事情: #if NOT_MICROSOFT typedef int BOOL; #define TRUE 1 #define FALSE 0 #endif 如果你有机会检查你正在移植的代码是否能够正常工作,无论BOOL类型是int还是bool ,那么很可能你有机会改变它来使用bool (或者使用int8_t ,你比较喜欢)。 Since BOOL is defined a ...
  • 正如我在评论中所建议的那样,我会切换attrs.$observe scope.$watch 。 根据个人偏好,我还会使用函数表达式而不是字符串,因为如果您正在使用typescript(或将使用),您将在属性更改时收到通知,其中字符串可能会保留为是: scope.$watch(function () { return scope.bdDisabled; }, function (newVal, oldVal) { ... } ); As ...
  • 要将XML节点中的属性作为字符串获取,可以使用getAttribute() element.getAttribute('formattednumber') To get the attribute from an XML node as a string, you can use getAttribute() element.getAttribute('formattednumber')
  • @boblail是正确的 - 将dataType更改为ajax将成为它。 但是,您有时会使用不会修改的第三方库。 在我的案例中,它是JQuery UI。 发送Content-Type: application/xhtml+xml会导致JQuery从响应中构建DOM Document。 将您的Content-Type设置为text/html ,你没事。 @boblail is right - changing dataType to ajax will make it. However, you someti ...
  • 我并不完全确定你的期望,但请注意!==的优先级高于&因此你在1和true之间进行按位AND。 你或许是这样的意思: if( ($flags & self::ALL) !== $flags ) I am not exactly sure what you expect, but note that !== has a higher precedence than & so you are doing a bitwise AND between 1 and true. Do you perhaps mean: ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。