Category "tsql"

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

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

Return a value if no rows are found in Microsoft tSQL

Using a Microsoft version of SQL, here's my simple query. If I query a record that doesn't exist then I will get nothing returned. I'd prefer that false (0) is

Return a value if no rows are found in Microsoft tSQL

Using a Microsoft version of SQL, here's my simple query. If I query a record that doesn't exist then I will get nothing returned. I'd prefer that false (0) is

Return a value if no rows are found in Microsoft tSQL

Using a Microsoft version of SQL, here's my simple query. If I query a record that doesn't exist then I will get nothing returned. I'd prefer that false (0) is