如何将一个数字转换为在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
, andn>=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
最满意答案
此问题是由构造函数中的隐式转换引起的。 字符串文字 (例如代码中的字符串文字)存储为
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 tobool
easier thatQString
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 typesbool, bool
(did all your constructors take aQString
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 ...
-
重载的布尔/字符串歧义(Overloaded Bool/String Ambiguity)[2023-10-05]
如果你有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而不是bool(Keep BOOL instead of bool)[2024-03-02]
由于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 ... -
具有bool属性的Angular指令传递字符串而不是bool(Angular directive with bool attribute passing string instead of bool)[2023-09-23]
正如我在评论中所建议的那样,我会切换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: ...