sql-server-2012

split comma separated string into columns

北城以北 提交于 2019-12-14 03:09:40
问题 I have a string like this: '1,A;2,B;3,C' Is there anyway that I can split comma separated values into columns then split to rows by ; like below: ID Text 1 A 2 B 3 C 回答1: Try this: declare @s varchar(50) = '1,A;2,B;3,C' --convert string to xml table (I used HTML tags for clarity) declare @xml xml = cast('<tr><td>' + replace(replace(@s, ';', '</td></tr><tr><td>'), ',', '</td><td>') + '</td></tr>' as xml) --query the xml to get SQL table select tbl.col.value('td[1]', 'int') [ID], tbl.col.value(

SQL Server error: “Cannot insert explicit value for identity column” even when I SET IDENTITY_INSERT ON

混江龙づ霸主 提交于 2019-12-14 03:07:30
问题 I REALLY review several times, that's the reason I am asking; looking for guidance... I have one table, as the script below. Then, I set IDENTITY_INSERT ON . Then I try to do an insert select, (I NEED the very same ids) I keep getting this error: Msg 544, Level 16, State 1, Line 2 Cannot insert explicit value for identity column in table 'Table1' when IDENTITY_INSERT is set to OFF. Does anybody knows why? Any set up at DB level can overrule the IDENTITY_INSERT ON ? I appreciate any advice.

Selecting Records based on column value in SQL Server

☆樱花仙子☆ 提交于 2019-12-14 02:38:50
问题 I have table contains two columns having below input records. Sample Input Records Column1 Column2 ----------------------- A B A C A D R B R D S E in the above records if i give where condition Column2='D' it will display below output records. Output Records Column1 Column2 ----------------------- A B A C A D R B R D Logic:In the Sample input records Column2='D' contains two records.These two records contain the colum1 values as 'A' and 'R'.so i want to display the records which contains 'A'

Get start and end date for each week in month

假装没事ソ 提交于 2019-12-14 02:33:36
问题 I need to get start and end date for each week in given month/year. (month and year are always given - like march 2017). Example, january 2017: 1 week : '2017-01-01' - '2017-01-01' 2 week: '2017-01-02' - '2017-01-08' 3 week: '2017-01-09' - '2017-01-15' 4 week: '2017-01-16' - '2017-01-22' 5 week: '2017-01-23' - '2017-01-29' 6 week: '2017-01-30' - '2017-01-31' I already know how to get number of weeks for given month / year: select *, DATEDIFF(WEEK, DATEADD(day,-1,StartAt), DATEADD(day,-1,EndAt

Insert into table the result from stored procedure plus extra columns

筅森魡賤 提交于 2019-12-14 02:17:32
问题 How to insert a few more columns into the dbFileListOnly table along with EXEC query: INSERT INTO admindb..dbfilelistonly (path, col1, col2....) @path, EXEC ('RESTORE FILELISTONLY FROM DISK = ''' + @path + '''') I don't know if it is possible or not. I want to save database name along with fileListHeader so that I know while restoring which files belong to a given database. 回答1: OPENROWSET is a tricky way but may comfort you. It allows even to join sp results with other tables. https://msdn

Configure Django with MS SQL Server database

五迷三道 提交于 2019-12-14 02:13:14
问题 I'm setting up a Django application and I want to use SQL Server 2012 for my database. To configure my website I'm following this section of the official Django documentation. In the section Database setup I found instructions for changing RDBMS . And in settings.py file I found these instructions for setting up Django with SQlite . DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), } } How I can change this configuration to use

How to use the result from a second select in my first select

て烟熏妆下的殇ゞ 提交于 2019-12-14 01:30:51
问题 I am trying to use a second SELECT to get some ID, then use that ID in a second SELECT and I have no idea how. SELECT Employee.Name FROM Emplyee, Employment WHERE x = Employment.DistributionID (SELECT Distribution.DistributionID FROM Distribution WHERE Distribution.Location = 'California') AS x This post got long, but here is a short "tip" While the syntax of my select is bad, the logic is not. I need that "x" somehow. Thus the second select is the most important. Then I have to use that "x"

how to fetch data from sql server database in php without refreshing the page

独自空忆成欢 提交于 2019-12-14 01:26:51
问题 I am trying to get some data from the database. I create a function that is located in functions.php file that return a value. On another page, I create a variable and just get that value. I was trying to use the onkey to check the database but then I realize that i need to know the amount of tickets even if they don't type anything. Here is the function: function.php function is_ticket_able($conn){ $query = "select number_of_tickets from [dbo].[TICKETS] " ; $stmt = sqlsrv_query($conn, $query

How to code a certain maths algorithm

陌路散爱 提交于 2019-12-14 00:30:03
问题 I have been kindly given this algorithm to help me create a fixture list in SQL, but applying it as SQL code, I have no idea how to do. Is there a way somebody can guide me on how to apply it with code? Below is my tables schema and below that is the algorithm: League: [LeagueID] TINYINT IDENTITY(1,1) NOT NULL PRIMARY KEY, [LeagueName] VARCHAR(30) UNIQUE Team: [TeamID] TINYINT IDENTITY(1,1) NOT NULL PRIMARY KEY, [TeamAbbreviation] CHAR(3) UNIQUE, [TeamName] VARCHAR(50) UNIQUE, [LeagueID]

different number of rows for a few different fields in one column

末鹿安然 提交于 2019-12-13 23:42:57
问题 Can someone help? I have an existing query below(SQL Server) that is running and pulls thousands of rows.However, I need to select a specific number of rows for a few different field values in one of the column in 'vw_client_uli_member_type' table. Where and how do I pit this condition? Thank you. select ind_int_code as 'Individual Type', ind_first_name as 'First Name', ind_last_name as 'Last Name', cst_recno as 'Member ID', cst_eml_address_dn as 'Email Address', adr_city as 'City', adr_state