Category "tsql"

How do I escape parenthesis within a CONTAINS using T-SQL?

I want to have the following query: SELECT COUNT(*) FROM MyTable WHERE CONTAINS (MyField, '(429)') The problem, is that the parenthesis are ignored and it is

SQL Declaring Variables in a View

I have created a view which uses dynamic variables in the where statement below. DECLARE @wkFileYear SMALLINT = (select fs.FileYear from dbo.FileSemesters fs

Mask the last 4 digit in an account number

I need to do an Select on a table that has many records to mask the Account Number. Example AccNumber : 123400012341234 Output result should look like 1234000

Two foreign keys reference one table - ON UPDATE SET NULL doesn't work

I've got two foreign keys in a table. Let's assume that table is called News and has foreign keys updatedById and createdById, both of which point to userId in

Reset Identity Seed ('DBCC CHECKIDENT ... RESEED' is not working)

For my unit tests I have an automated process which builds a copy of a DB, table by table. I first of all run a SELECT INTO and add the primary key and any indi

Drop default constraint on a column in TSQL

I have a table with a column like this that is currently live: name NVARCHAR(128) NOT NULL DEFAULT '' I am altering the column like this to make it nullable:

SQL query to determine that values in a column are unique

How to write a query to just determine that the values in a column are unique?

Correct use of transactions in SQL Server

I have 2 commands and need both of them executed correctly or none of them executed. So I think I need a transaction, but I don't know how to use it correctly.

How to insert into a table that specifies a DEFAULT value for every column?

I have a table where all columns are auto-populated whenever an insertion happens: CREATE TABLE … ( ItemID INT NOT NULL IDENTITY(…

ALTER DATABASE failed because a lock could not be placed on database '<db_name>'. Try again later

I really don't care what I do with this test database...it's for sandbox testing (attached to a production server instance)! All I'm trying to do is KILL all co

SSMS 2012: Convert DATETIME to Excel serial number

I can't seem to find an answer to this anywhere --- I want to convert a datetime in SQL to the excel serial number. I'm essentially looking for the DATEVALUE f

Is it necessary to test for NULL if also testing for greater than?

I inherited some old stored procedures today, and came across several examples that followed this general pattern, where @Test is some INT value: IF @Test IS N

Using a SELECT statement to return a row where a DateTime column = variable of type Date?

I am using OleDb, trying to do this: Dim d as Date = DateSerial(Year(rptDate), Month(rptDate), 1 - 1) Dim conn as OleDbConnection = new OleDbConnection(connSt

SQL - The conversion of a varchar data type to a datetime data type resulted in an out-of-range value

I have been getting the following error when running a SQL to convert my data type value from varchar to datetime. Msg 242, Level 16, State 3, Line 1 The

Export database schema into XML file

I would like to export whole database schema to XML file. Information there what are needed. tables - columns, data type, pk, fk, views - returned columns, fun

CONCAT_WS() for SQL Server

How can I emulate MySQL's CONCAT_WS() function in SQL Server? This function is similar to CONCAT() function in SQL Server 2012 except that it adds a separator

How do I create a SQL Function to return a BIT?

I am using this script below to create a function but I get an error in the messages log: CREATE FUNCTION [dbo].[MyFunction] () RETURNS BIT AS RETURN CAST(1 AS

Conversion failed when converting date from character string

I am trying to rearrange Invoice date field of YYYMMDD into short date format DD/MM/YYYY or DD-MM-YYYY UK/British format in a SELECT statement. Performing on

Getting maximum value of float in SQL programmatically

Is there an method for programmatically (in T-SQL) retrieving the maximum (and minimum) value of a datatype? That it would act like float.MaxValue in C#. I wou

Getting maximum value of float in SQL programmatically

Is there an method for programmatically (in T-SQL) retrieving the maximum (and minimum) value of a datatype? That it would act like float.MaxValue in C#. I wou