我有一个表达:
Select (2345789 * 39.456) / 100
Run Code Online (Sandbox Code Playgroud)
输出是925554.5078400.
我想显示925554.50小数点后的2个值.
如果我使用圆形
Select round((2345789 * 39.456)/100, 1)
Run Code Online (Sandbox Code Playgroud)
输出是925554.5000000.但我想要小数点后正好2位数.怎么做?
我有一个动态查询,如下所示
Alter PROCEDURE dbo.mySP
-- Add the parameters for the stored procedure here
(
@DBName varchar(50),
@tblName varchar(50)
)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
declare @string as varchar(50)
declare @string1 as varchar(50)
set @string1 = '[' + @DBName + ']' + '.[dbo].' + '[' + @tblName + ']'
set @string = 'select * from ' + @string1
exec …Run Code Online (Sandbox Code Playgroud) 当我这样做:
select col1,case when [pivot1]=1 then '-' else '' end [pivot1],
case when [pivot2]=1 then '-' else '' end [pivot2]
from
(select col1,col2,col3 from tbl) as c
pivot
(sum(col3) for col2 in
([pivot1],[pivot2]))as pvt
Run Code Online (Sandbox Code Playgroud)
一切正常.
当我这样做:
select col1,[pivot1],[pivot2]
from
(select col1,col2,col3 from tbl) as c
pivot
(sum(case col3 when '-' then 1 else 0 end) for col2 in
([pivot1],[pivot2]))as pvt
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
"Msg 156, Level 15, State 1, Line 31
Incorrect syntax near the keyword 'case'."
Run Code Online (Sandbox Code Playgroud)
我的目的是为这个转换编写一个case语句而不是多个case语句.
我究竟做错了什么?
我正在尝试创建一个匿名类型列表,如下所示,但我在某处犯了错误
for (int i = 0; i < 10; i++)
{
var list = new[]
{
new { Number = i, Name = string.Concat("name",i) }
};
}
Run Code Online (Sandbox Code Playgroud)
例如
var o1 = new { Id = 1, Name = "Name1" };
var o2 = new { Id = 2, Name = "Name2" };
var list = new[] { o1, o2 }.ToList();
Run Code Online (Sandbox Code Playgroud)
如何在运行时做同样的事情?
需要帮助.
我有以下程序将图像转换为字节数组
public byte[] ReadImageFile(string imageLocation)
{
byte[] imageData = null;
FileInfo fileInfo = new FileInfo(imageLocation);
long imageFileLength = fileInfo.Length;
FileStream fs = new FileStream(imageLocation, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
imageData = br.ReadBytes((int)imageFileLength);
return imageData;
}
Run Code Online (Sandbox Code Playgroud)
如果我将值传递给"D:\\ENSource\\1.png"它,它就可以正常工作.
但是,如果我将值发送为" http://i.stack.imgur.com/PShuQ.png ",则会抛出异常
不支持URI格式
我怎样才能做到这一点?
我有一个字符串值,需要将其转换为十进制.
var str = null;
decimal number;
decimal d = Decimal.TryParse(str, out number) ? number : 0M;
Run Code Online (Sandbox Code Playgroud)
它工作正常.
现在我试图通过使用C#6.0 的新Null条件运算符来实现相同的功能 .我怎么能这样做?
我知道这是一次错误的尝试.
var str = null;
decimal d = str?.Convert.ToDecimal(str);
Run Code Online (Sandbox Code Playgroud) 我有一个字符串形式的二进制数
string input = "10110101101";
Run Code Online (Sandbox Code Playgroud)
现在我需要翻转(0 to 1和1 to 0)它的前3位.
结果输出为010 10101101
如何在C#中这样做?
假设我们有一组数字,如{16,17,4,3,5,2}.现在的目标是找到那些大于集合中其余数字的数字,同时与它们的正确元素进行比较.
表示16与17相比较少,不能考虑.虽然17比较4,3 5和2总是更大,因此将被考虑.同样地,将丢弃小于5的大于3的邻接.但5比2更大.并且因为2是最右边的元素将始终被考虑.我已经编写了以下程序,它可以工作.
using System;
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
var intCollection = new List<int>() { 16,17,4,3,5,2 };
var discardedElements = new List<int>();
for(int i=0;i< intCollection.Count;i++)
{
for(int j=i+1;j< intCollection.Count; j++)
{
if (intCollection[i] < intCollection[j])
{
discardedElements.Add(intCollection[i]);
}
}
}
Console.WriteLine("Successful elements are");
intCollection.Except(discardedElements).ToList().ForEach(i => Console.WriteLine("{0}", i));
Console.ReadKey();
}
}
}
Run Code Online (Sandbox Code Playgroud)
结果
Successful elements are
17
5
2
Run Code Online (Sandbox Code Playgroud)
但是这个程序不是优化程序.针对同样问题的任何更好的算法?
NB~该程序虽然显然没有任何实时使用,但它将有助于改进算法.
我有一个列为nvarchar(max),其值为
'1.7925e+006'
Run Code Online (Sandbox Code Playgroud)
如何将其转换为十进制?我尝试了如下
declare @pd as nvarchar(max) = '1.7925e+006'
Select convert(decimal(18, 2), @pd)
Run Code Online (Sandbox Code Playgroud)
但我得到了一个错误:
消息8114,级别16,状态5,第4
行将数据类型nvarchar转换为数值时出错。
请提出建议。