sql-server-2012

Conversion failed when converting the nvarchar value to data type int - Error Message

纵饮孤独 提交于 2021-02-19 09:13:22
问题 My Query Select * from MyTable The table consists 300k rows. It runs for 200k+ rows and this error pops up. How to handle this to get the full data? Does MyTable have any computed columns? Table consists of a computed column with the name IsExceeds which is given below for your reference. This is the computed column formula: (CONVERT([int],[Pro_PCT])-CONVERT([int],replace([Max_Off],'%',''))) Field Definitions: [Pro_PCT] [nvarchar](50) NULL, [Max_Off] [nvarchar](50) NULL, [IsExceeds] AS

Find min and max for subsets of consecutive rows - gaps and islands

半城伤御伤魂 提交于 2021-02-19 08:13:26
问题 Trying to build a query. The input is ordered by rownumber in column 'rn' starting with 1 for each unique value in 'name' and defining a given sequence of entries in 'act'. In column 'act' it holds two values in multiple occurence, >sleep< and >wake<. The goal is to find for each consecutive set of rows of one of those values the minimum and maximum value of startt and endd. This shall be the input: name act rn startt endd ---------- ---------- ------ ------ ------ jimmy sleep 1 1 3 jimmy

Generate Row Number for every 3 rows

你离开我真会死。 提交于 2021-02-19 06:39:09
问题 I want generate number for every three rows CREATE TABLE #test(period INT) INSERT INTO #test VALUES (602),(603),(604),(605),(606),(607),(608),(609) I know we can generate sequence using row_number window function or while loop or cursor SELECT period, ( Row_number()OVER(ORDER BY period) - 1 ) / 3 + 1 FROM #test Result; +--------+-----+ | period | seq | +--------+-----+ | 602 | 1 | | 603 | 1 | | 604 | 1 | | 605 | 2 | | 606 | 2 | | 607 | 2 | | 608 | 3 | | 609 | 3 | +--------+-----+ Is there any

cannot login to sql server with new user created

爷,独闯天下 提交于 2021-02-19 03:56:08
问题 I created a login named logintest (SQL Authentication) then i created a user named usertest with this login the user creation is successful, And i changed the authentication mode to mixed mode and also restarted the services SQLSERVERAGENT and MSSQLSERVER and still this error appear when i try to login with the new user created Cannot connect to SARAH. Login failed for user 'usertest'. (Microsoft SQL Server, Error: 18456) 回答1: The login must have CONNECT right given. The login must be enabled

Date From Week Number and Day In Sql Server 2012

痴心易碎 提交于 2021-02-19 02:57:12
问题 I have day number of the week as well as week number of the year .How can i calculate date of that day in sql .For Example.the day number of 22-Feb-2014 is 7th and it is 8th week of the year.Now how can i calculate the date back from this information.Its urgent.Please help.I want query. 回答1: Here is my verison create FUNCTION date_from_week_number_day ( @year int = 2014 ,@weeknumber int = 8 ,@day int = 7 ) RETURNS date AS BEGIN declare @date date ,@first_date_of_year date set @first_date_of

MS SQL 2012 : In SQL Shift columns to left side if column contains 0

微笑、不失礼 提交于 2021-02-18 22:21:42
问题 I need to shift data(columns) to left side if first columns(left side columns) have 0 value and NULL should be added in right side columns. Once non-zero value found in any columns then 0 value in later column should remain as it is. Input Data:- cust_id month1 month2 month3 month4 month5 c1 100 200 300 400 500 c2 0 0 50 250 350 c3 0 0 100 0 0 c4 100 0 100 0 500 c5 0 0 0 0 0 Expected Output Result:- cust_id month1 month2 month3 month4 month5 c1 100 200 300 400 500 c2 50 250 350 NULL NULL c3

SQL Server 2012: Select Top n based on multiple criteria

时间秒杀一切 提交于 2021-02-18 16:59:32
问题 Pretty new to SQL here - help would be much appreciated. I have a table with Region , Month , Member ID , and Sales (with multiple transactions per member). I just want to extract the top 2 members, based on sum of sales, per region, per month....so essentially: Region Month MemberID Sales ----------------------------------------- 1 1/1/2013 A $200 2 2/1/2013 B $300 1 1/1/2013 A $100 1 1/1/2013 B $50 2 1/1/2013 D $500 2 2/1/2013 C $200 Becomes: Region Month Member ID Sales -------------------

SQL Server 2012: Select Top n based on multiple criteria

谁说胖子不能爱 提交于 2021-02-18 16:59:09
问题 Pretty new to SQL here - help would be much appreciated. I have a table with Region , Month , Member ID , and Sales (with multiple transactions per member). I just want to extract the top 2 members, based on sum of sales, per region, per month....so essentially: Region Month MemberID Sales ----------------------------------------- 1 1/1/2013 A $200 2 2/1/2013 B $300 1 1/1/2013 A $100 1 1/1/2013 B $50 2 1/1/2013 D $500 2 2/1/2013 C $200 Becomes: Region Month Member ID Sales -------------------

How to set a column name in SQL query as parameter?

强颜欢笑 提交于 2021-02-16 20:10:30
问题 I want to transfer to CommandText table name as parameter, something like @column . How can I do this? Because column name is transferred as custom parameter. using (SqlConnection connection = SQL.Connection()) { using (SqlCommand cmd = connection.CreateCommand()) { cmd.Parameters.Add("@data", SqlDbType.VarChar).Value = "some_string"; cmd.CommandText = "UPDATE users SET colum=@data"; cmd.ExecuteNonQuery(); } } 回答1: You cannot do this in regular SQL - if you must have configurable column names

View not running - percentile_cont

北城余情 提交于 2021-02-11 13:33:01
问题 I have the following code: SELECT DISTINCT SQ.COMP_ID, SQ.JT, CAST(MIN(SQ.SUM_SALES) OVER (PARTITION BY SQ.JT) AS DECIMAL (10,2)) AS MINIMUM, CAST(PERCENTILE_CONT(0.15) WITHIN GROUP (ORDER BY SQ.SUM_SALES) OVER (PARTITION BY SQ.JT) AS DECIMAL (10,2)) AS P15, CAST(PERCENTILE_CONT(0.25) WITHIN GROUP (ORDER BY SQ.SUM_SALES) OVER (PARTITION BY SQ.JT) AS DECIMAL (10,2)) AS P25, CAST(AVG(SQ.SUM_SALES) OVER (PARTITION BY SQ.JT) AS DECIMAL (10,2)) AS AVERAGE, CAST(PERCENTILE_CONT(0.5) WITHIN GROUP