sql-server-2008-r2

Sum of data in varchar column

两盒软妹~` 提交于 2019-12-02 04:26:17
i have to sum element in a column(SCENARIO1) that is varchar and contain data like (1,920, 270.00, 0, NULL) but when i try to convert data into int or decimal i get this error : "he command is wrong when converting value "4582014,00" to int type" here is my request : select sum( convert( int, SCENARIO1) ) from Mnt_Scenario_Exercice where code_pere='00000000' any help please try this select sum(cast(replace(SCENARIO1, ',', '.') as decimal(29, 10))) from Mnt_Scenario_Exercice where code_pere = '00000000'; If you couldn't convert your '4582014,00' into decimal, there's a chance you have different

SQL Server — how to shred one multielement XML to single-element XML values inserted into table?

好久不见. 提交于 2019-12-02 03:39:30
问题 I am fighting with @x.nodes('...') as I am new to XQuery. I do have the XML variable @x constructed the following way: CREATE TABLE tab (a int, b int, c int); GO INSERT INTO tab (a, b, c) VALUES (1, 11, 111); INSERT INTO tab (a, b, c) VALUES (2, 22, 222); INSERT INTO tab (a, b, c) VALUES (3, 33, 333); INSERT INTO tab (a, b, c) VALUES (4, 44, 444); GO DECLARE @x XML = (SELECT * FROM tab FOR XML RAW, TYPE); When its content is displayed, it looks like: <row a="1" b="11" c="111" /> <row a="2" b=

loop through all tables and delete records

久未见 提交于 2019-12-02 03:23:59
问题 I'm new to MsSql and I'm not sure if this can be done but I figured I'd ask before I want on my way with the current process.. I need to create a script that loops through all tables in a database and deletes the rows where CorporationId = "xxx". There are a few tables that do not have this column, but of my ~50 tables, only 1 or two do not. I can individually delete the records in the table with this: USE MyDatabase DECLARE @CorporationId UniqueIdentifier DECLARE @TokenId UniqueIdentifier

SQL primary key constraint although record does not exist

跟風遠走 提交于 2019-12-02 03:20:39
I am getting the following error: Violation of PRIMARY KEY constraint 'PK_ss_student_grade'. Cannot insert duplicate key in object 'dbo.ss_student_grade'. The duplicate key value is (301, 1011, 24801, 33). If I check the table before the insert there are no records with such a primary key. The insert is done through C# code and I made sure that the code runs only once. even after the error if I check the table I still get no record with such a primary key. Note: a trigger runs on the insert in the table but it only writes to a log file and does not affect any data in the database What could

Cannot cast DBNull.Value to type 'System.DateTime'. Please use a nullable type

ぐ巨炮叔叔 提交于 2019-12-02 03:18:58
This is my code var finalResults = (from r in results.AsEnumerable() where r.Field<DateTime>("PrintTime") is DBNull where PrintTime is a column in my Sql Server 2008 r2 database, its type is datetime and it is nullable I got this exception: Cannot cast DBNull.Value to type 'System.DateTime'. Please use a nullable type. Could you help please? DataRow.Field supports nullable types, so use DateTime? instead of DateTime : var finalResults = from r in results.AsEnumerable() let printTime = r.Field<DateTime?>("PrintTime") where !printTime.HasValue select r; 来源: https://stackoverflow.com/questions

T-SQL Pivot - Total Row and Dynamic Columns

我是研究僧i 提交于 2019-12-02 02:43:23
Let's jump straight into it. Here's the code SELECT [prov], [201304], [201305], [201306], [201307] FROM ( SELECT [prov], [arrival], [Amount] FROM [tblSource]) up PIVOT (SUM([Amount]) FOR [arrival] IN ([201304], [201305], [201306], [201307])) AS pvt GO It brings me back an ever so lovely table. I was wondering how I would get the totals for each "date" column to show in an appended last row? In addition, the underlying table will have more data added, specifically more dates. This means that 201308 will be added next, then 201309 etc This will mean that currently I will have to amend the code

Why does SQL LEN function return '1' for a string with several characters?

心不动则不痛 提交于 2019-12-02 02:22:03
Simple question - why when I print the value of the @len variable in the query below would I be getting the value 1, instead of 12 (the number of characters in the specified string)? DECLARE @string varchar DECLARE @index int DECLARE @len int DECLARE @char char(1) SET @string = 'content loop' SET @index = 1 SET @len= LEN(@string) print @len Your declaration of @string is wrong. You have no length on the varchar . Try this: declare @string varchar(255); -- or whatever You just learned that the default in this situation is 1. This is clearly specified in the documentation . As a further note, MS

need help to export table from sql server 2008 to text file

别等时光非礼了梦想. 提交于 2019-12-02 02:12:36
问题 I am trying to export a table present in ms sql server 2008 to a text file on my system. I am writing the following command in sql server query window SELECT * FROM [AdventureWorks].[Person].[AddressType] INTO OUTFILE 'C:/filename.csv' FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n'; Now whenever I write this command the sql help gives me error that incorrect syntax near 'INTO' then I tried interchanging from and into keywords as follows SELECT * INTO OUTFILE 'C:/filename.csv' FIELDS

Update a table from two comma separated parameter as input

自闭症网瘾萝莉.ら 提交于 2019-12-02 02:05:41
I have a Gridview in front end where Grid have two columns : ID and Order like this: ID Order 1 1 2 2 3 3 4 4 Now user can update the order like in front end Gridview: ID Order 1 2 2 4 3 1 4 3 Now if the user click the save button the ID and order data is being sent to Stored Procedure as @sID = (1,2,3,4) and @sOrder = (2,4,1,3) Now if I want to update the order and make save I want to store it into database. Through Stored procedure how can update into the table so that the table is updated and while select it gives me the results like: ID Order 1 2 2 4 3 1 4 3 There is no built in function

SQL Server — how to shred one multielement XML to single-element XML values inserted into table?

梦想的初衷 提交于 2019-12-02 01:50:27
I am fighting with @x.nodes('...') as I am new to XQuery. I do have the XML variable @x constructed the following way: CREATE TABLE tab (a int, b int, c int); GO INSERT INTO tab (a, b, c) VALUES (1, 11, 111); INSERT INTO tab (a, b, c) VALUES (2, 22, 222); INSERT INTO tab (a, b, c) VALUES (3, 33, 333); INSERT INTO tab (a, b, c) VALUES (4, 44, 444); GO DECLARE @x XML = (SELECT * FROM tab FOR XML RAW, TYPE); When its content is displayed, it looks like: <row a="1" b="11" c="111" /> <row a="2" b="22" c="222" /> <row a="3" b="33" c="333" /> <row a="4" b="44" c="444" /> i.e. single multiline string.