tsql

How do I update selective fields in SQL (leaving some unchanged)?

你。 提交于 2021-01-27 06:53:44
问题 I would like to update a record with two dates, leaving existing data intact if I do not have a new value to update with. Here is a sample table record: id last_foo last_bar -- ---------- ---------- 1 2010-05-30 2010-05-30 And the query I am using: UPDATE sampledates SET last_foo = @LastFoo, last_bar = @LastBar WHERE id = @ID; If my nullable datetimes LastFoo or LastBar are null, I would like to leave the existing SQL value as-is, otherwise update. For example, say I am updating this record

How to check VARCHAR(n) for well-formed XML before CAST/CONVERT

℡╲_俬逩灬. 提交于 2021-01-27 06:23:08
问题 My company has a logging table containing a VARCHAR(N) column in which a string is placed that is supposed to be XML, but as it turns out it is not always well-formed. In order to perform analysis on the logging (to identify error trends, etc.), I have been using a LIKE statement. However, this is remarkably slow. Recently, I discovered that SQL Server supports XQuery, so I started playing with it. The problem I'm running into is that I can't figure out a way to handle CAST/CONVERT errors

grouping by date range in t-sql

和自甴很熟 提交于 2021-01-27 05:26:47
问题 I'm trying to do a query on this table: Id startdate enddate amount 1 2013-01-01 2013-01-31 0.00 2 2013-02-01 2013-02-28 0.00 3 2013-03-01 2013-03-31 245 4 2013-04-01 2013-04-30 529 5 2013-05-01 2013-05-31 0.00 6 2013-06-01 2013-06-30 383 7 2013-07-01 2013-07-31 0.00 8 2013-08-01 2013-08-31 0.00 I want to get the output: 2013-01-01 2013-02-28 0 2013-03-01 2013-06-30 1157 2013-07-01 2013-08-31 0 I wanted to get that result so I would know when money started to come in and when it stopped. I am

grouping by date range in t-sql

匆匆过客 提交于 2021-01-27 05:23:26
问题 I'm trying to do a query on this table: Id startdate enddate amount 1 2013-01-01 2013-01-31 0.00 2 2013-02-01 2013-02-28 0.00 3 2013-03-01 2013-03-31 245 4 2013-04-01 2013-04-30 529 5 2013-05-01 2013-05-31 0.00 6 2013-06-01 2013-06-30 383 7 2013-07-01 2013-07-31 0.00 8 2013-08-01 2013-08-31 0.00 I want to get the output: 2013-01-01 2013-02-28 0 2013-03-01 2013-06-30 1157 2013-07-01 2013-08-31 0 I wanted to get that result so I would know when money started to come in and when it stopped. I am

SQL query for counting records per month

試著忘記壹切 提交于 2021-01-27 04:16:11
问题 I have a dataset I need to build for number of visits per month for particular user. I have a SQL table which contains these fields: User nvarchar(30) DateVisit datetime What I want to achieve now is to get all the visits grouped by month for each user, something like at the picture: I started the query, I am able to get the months and the total sum of visits for that month (not split by user) with this query; select [1] AS January, [2] AS February, [3] AS March, [4] AS April, [5] AS May, [6]

How to “pick” random records with T-SQL

為{幸葍}努か 提交于 2021-01-27 03:53:47
问题 This is a simple question that is actually hard to answer, because the "picking" has a special meaning. I need to give three random picks for each person (and give pick/row number of 1, 2, and 3). What makes it hard is that the persons and picks are from different tables and there is no logical joining between the person and picks. The closest I can get is: SELECT TOP 15 database_id, create_date, RowNo, cs.name FROM sys.databases CROSS apply ( SELECT top 3 Row_number()OVER(ORDER BY (SELECT

How to “pick” random records with T-SQL

心不动则不痛 提交于 2021-01-27 03:53:38
问题 This is a simple question that is actually hard to answer, because the "picking" has a special meaning. I need to give three random picks for each person (and give pick/row number of 1, 2, and 3). What makes it hard is that the persons and picks are from different tables and there is no logical joining between the person and picks. The closest I can get is: SELECT TOP 15 database_id, create_date, RowNo, cs.name FROM sys.databases CROSS apply ( SELECT top 3 Row_number()OVER(ORDER BY (SELECT

How to “pick” random records with T-SQL

孤街醉人 提交于 2021-01-27 03:53:25
问题 This is a simple question that is actually hard to answer, because the "picking" has a special meaning. I need to give three random picks for each person (and give pick/row number of 1, 2, and 3). What makes it hard is that the persons and picks are from different tables and there is no logical joining between the person and picks. The closest I can get is: SELECT TOP 15 database_id, create_date, RowNo, cs.name FROM sys.databases CROSS apply ( SELECT top 3 Row_number()OVER(ORDER BY (SELECT

Unable to start the Transact-SQL debugger, could not connect to the database engine instance

落花浮王杯 提交于 2021-01-21 12:10:41
问题 I have been trying to run debugging within SQl server management studio and for some reason the debugger has just stopped working. This is the message I get: Unable to start the Transact-SQL debugger, could not connect to the database engine instance 'server-sql'. Make sure you have enabled the debugging firewall exceptions and are using a login that is a member of the sysadmin fixed server role. The RPC server is unavailable. Before this I get two messages, one requesting firewall

Unable to start the Transact-SQL debugger, could not connect to the database engine instance

梦想与她 提交于 2021-01-21 12:06:33
问题 I have been trying to run debugging within SQl server management studio and for some reason the debugger has just stopped working. This is the message I get: Unable to start the Transact-SQL debugger, could not connect to the database engine instance 'server-sql'. Make sure you have enabled the debugging firewall exceptions and are using a login that is a member of the sysadmin fixed server role. The RPC server is unavailable. Before this I get two messages, one requesting firewall