sql-server-2008-r2

How to get node name and values from an xml variable in t-sql

孤者浪人 提交于 2019-11-29 13:49:50
问题 I have the following xml - <Surveys xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ImmForm XML Schema NHS Direct.xsd"><Svy SurveyName="WeeklyFluSurveillance2012/13-NHSDirectWeek40w/e07/10/2012" OrgCode="NHS Direct"><TotCR>222.10</TotCR><PerCF>0.40</PerCF><PerCFunder1>0.20</PerCFunder1><PerCF1to4>0.30</PerCF1to4><PerCF5to14>0.50</PerCF5to14><PerCF15to44>0.40</PerCF15to44><PerCF45to64>0.20</PerCF45to64><PerCF65plus>3.60</PerCF65plus> <PerCFNE>4.22</PerCFNE>

Sort string as number in sql server

混江龙づ霸主 提交于 2019-11-29 13:31:25
I have a column that contains data like this. dashes indicate multi copies of the same invoice and these have to be sorted in ascending order 790711 790109-1 790109-11 790109-2 i have to sort it in increasing order by this number but since this is a varchar field it sorts in alphabetical order like this 790109-1 790109-11 790109-2 790711 in order to fix this i tried replacing the -(dash) with empty and then casting it as a number and then sorting on that select cast(replace(invoiceid,'-','') as decimal) as invoiceSort...............order by invoiceSort asc while this is better and sorts like

Extracting dollar amounts from existing sql data?

亡梦爱人 提交于 2019-11-29 12:45:30
I have a field with that contains a mix of descriptions and dollar amounts. With TSQL, I would like to extract those dollar amounts, then insert them into a new field for the record. -- UPDATE -- Some data samples could be: Used knife set for sale $200.00 or best offer. $4,500 Persian rug for sale. Today only, $100 rebate. Five items for sale: $20 Motorola phone car charger, $150 PS2, $50.00 3 foot high shelf. In the set above I was thinking of just grabbing the first occurrence of the dollar figure... that is the simplest. I'm not trying to remove the amounts from the original text, just get

Break into multiple rows based on date range of a single row

北城以北 提交于 2019-11-29 12:35:36
I have a table which captures appointments, some are single day appointments and some are multi day appointments, so the data looks like AppointmentId StartDate EndDate 9 2017-04-12 2017-04-12 10 2017-05-01 2017-05-03 11 2017-06-01 2017-06-01 I want to split the multi day appointment as single days, so the result I am trying to achieve is like AppointmentId StartDate EndDate 9 2017-04-12 2017-04-12 10 2017-05-01 2017-05-01 10 2017-05-02 2017-05-02 10 2017-05-03 2017-05-03 11 2017-06-01 2017-06-01 So I have split the appointment id 10 into multiple rows. I checked a few other questions like

Convert a string with 'YYYYMMDDHHMMSS' format to datetime

谁说胖子不能爱 提交于 2019-11-29 12:28:20
问题 I recognize there has been many questions posted about converting strings to datetime already but I haven't found anything for converting a string like 20120225143620 which includes seconds. I was trying to perform a clean conversion without substring-ing each segment out and concatenating with / and : . Does anyone have any suggestions? 回答1: You can use the STUFF() method to insert characters into your string to format it in to a value SQL Server will be able to understand: DECLARE

SQL Join to get most recent record

让人想犯罪 __ 提交于 2019-11-29 12:05:36
I have three tables: Measurements (MeasureID, Time, Distance, Value) Events(EventID, Time Value) EventValues (EventDataID, EventID, Type, Value) I need to get for every measurement, the most recent event (in the past) and its associated eventvalues data. My current query is quite ugly: SELECT M.*, (SELECT TOP 1 EV.value FROM [Event] E JOIN EventValues EV ON E.EventID = EV.EventID WHERE M.Time >= E.Time ORDER BY M.Time-E.Time) AS Data, FROM [Measure] M ORDER BY M.Distance and it only allows me to select one column from the EventValues table (I need more) Is there any way this can be done using

Is this a bug in MERGE, failing to implement FOREIGN KEY properly?

吃可爱长大的小学妹 提交于 2019-11-29 11:58:31
问题 I am using the following tables to implement subtypes, which is a very common approach: CREATE TABLE dbo.Vehicles( ID INT NOT NULL, [Type] VARCHAR(5) NOT NULL, CONSTRAINT Vehicles_PK PRIMARY KEY(ID), CONSTRAINT Vehicles_UNQ_ID_Type UNIQUE(ID, [Type]), CONSTRAINT Vehicles_CHK_ValidTypes CHECK([Type] IN ('Car', 'Truck')) ); GO CREATE TABLE dbo.Cars(ID INT NOT NULL, [Type] AS CAST('Car' AS VARCHAR(5)) PERSISTED, OtherData VARCHAR(10) NULL, CONSTRAINT Cars_PK PRIMARY KEY(ID), CONSTRAINT Cars_FK

Convert a 12 hour format to 24 hour format in sql server

安稳与你 提交于 2019-11-29 11:52:25
there are date values in the below format,in one of the sql server 2000 tables 10/1/2013 10:39:14 PM 10/1/2013 6:39:04 PM 10/1/2013 8:19:31 AM 10/1/2013 3:35:40 AM how to convert the above format data values into a 24hour date format,as shown below 10/1/2013 10:39 10/1/2013 18:39 10/1/2013 8:19 10/1/2013 3:35 Try this: First convert varchar date to datetime and then you can manipulate it in the way you want as: -- CONVERT TO DATETIME TO GET 24HR FORMAT SELECT CONVERT(DATETIME, '10/1/2013 6:39:04 PM', 0) -- Concatenate in required format SELECT CONVERT(VARCHAR(10), CONVERT(DATETIME, '10/1/2013

Bulk Insert w/ .fmt file: Operating system error code (null)

落爺英雄遲暮 提交于 2019-11-29 11:52:16
问题 I'm trying to import a text file defined by a .fmt format. Instead of being comma-delimited, this text file is what I would call column-delimited (i.e. the first 8 characters is the first field, the next 3 characters is the second, etc). When I run the query below, I get a "Operating system error code (null)" message, which is odd since I'm using SQL Server 2008r2 with Vista. Please explain to me what this error message means and how do I get around it? I've googled it and found similar

How to permit a SQL Server user to insert/update/delete data, but not alter schema?

可紊 提交于 2019-11-29 11:43:41
问题 My application (C#, ASP.Net) needs to insert, update and delete data in the DB, and run stored procedures. I need to prevent it from modifying the DB schema - no altering tables, creating or dropping, no changes to stored procedures. What permissions combination do I need to grant to the application user? Just 'select' isn't going to work, because it needs to insert/update/delete data in tables. How do I check permissions and access for a particular login? How do I grant or deny permissions