Tag: sql-server-2012

How can I find out which rows (if any) have a non-unique value in a column?

I have the following table in my SQL Server 2012 database: CREATE TABLE [dbo].[Problem]( [ProblemId] [int] IDENTITY(1,1) NOT NULL, [SubTopicId] [int] NOT NULL, [ReferenceId] [int] NOT NULL, [ProblemStatusId] [int] NOT NULL, [Locator] [nvarchar](50) NULL ) The Locator is supposed to be unique but I think there are some cases where more than one row has […]

Converting string 'yyyy-mm-dd' to date

I want to select from table where date column is equal to specific date which I sending as a string in format ‘yyyy-mm-dd’. I need to convert that string and than to compare if I have that date in my table. For now I am doing this: select * FROM table where CONVERT(char(10), date_column,126) = […]

How to pass rows of values into SQL Server stored procedure?

I would like to insert a range of values into a pair of SQL tables, with data requiring some pre-processing (like attaching a common time stamp and some revamp of the data in both tables). Ideally I prefer to shield my code from direct SQL statements, opting to use stored procedures instead. How could I […]

Error with simple INSERT statement

I made an insert statement that runs inside a asp.net page. It gave me an error, so I went to the sql server and ran the statement as it should be and used it to compare with what I wrote in the asp.net page. The thing is, it it writen properly but it doesn’t work. […]

SQL statements to ROLLBACK but Audit inserts to COMMIT

SQL Server 2012 I have a Master stored proc, which calls several other ‘child’ stored procs. The master proc encapsulates the child procs in a transaction. Throughout the master and child procs, there are many inserts into an audit table. If the process succeeds, I get a detaied chain of events. If the process fails […]

SQL Query – Design struggle

I am fairly new to SQL Server (2012) but I was assigned the project where I have to use it. The database consists of one table (counted in millions of rows) which looks mainly like this: Number (float) Date (datetime) Status (nvarchar(255)) 999 2016-01-01 14:00:00.000 Error 999 2016-01-02 14:00:00.000 Error 999 2016-01-03 14:00:00.000 Ok 999 […]

T-SQL date pickup

I am looking for some T-SQL code that should pick the date which is “One Year back from current date (at the same time last Sunday in the month of January)”. I have some T-SQL code which is being used in SQL Server 2014: select convert(varchar(10), DATEADD(day, DATEDIFF(day, ‘19000107’, DATEADD(month, DATEDIFF(MONTH, 0, CONVERT(date, CONVERT(VARCHAR(4), (CASE […]

Add A,B,C letters to Duplicate values

I need big help from you I am using sql server 2008 and I want to get the output using sql query. I have a following data in the table. Id Code —————– 1 01012 2 01012 3 01012 4 01012 5 01013 6 01013 7 01014 I need Following output Id Code —————– 1 […]

How to use CASE without adding new column to table in SQL

How to change column value by CASE command depending on condition without giving adding a new column to table? The only way I know is by adding new column: SELECT t1.* ,CASE what=’costs’ THEN amount*(-1) ELSE sales END AS NewAmount FROM t1 Is there a way to get the results as on the picture below? […]

Add value to row number in SQL server 2012

I want to add specific value to the row number but this statement doesn’t work : SELECT (ROW_NUMBER()+1) OVER(ORDER BY col_1 value DESC) as Row FROM table what is the correct syntax to do it ?

MS SQL Server is a Microsoft SQL Database product, include sql server standard, sql server management studio, sql server express and so on.