Sql Server - Insufficient result space to convert uniqueidentifier value to char - sql

I am getting below error when I run sql query while copying data from one table to another,
Msg 8170, Level 16, State 2, Line 2
Insufficient result space to convert
uniqueidentifier value to char.
My sql query is,
INSERT INTO dbo.cust_info (
uid,
first_name,
last_name
)
SELECT
NEWID(),
first_name,
last_name
FROM dbo.tmp_cust_info
My create table scripts are,
CREATE TABLE [dbo].[cust_info](
[uid] [varchar](32) NOT NULL,
[first_name] [varchar](100) NULL,
[last_name] [varchar](100) NULL)
CREATE TABLE [dbo].[tmp_cust_info](
[first_name] [varchar](100) NULL,
[last_name] [varchar](100) NULL)
I am sure there is some problem with NEWID(), if i take out and replace it with some string it is working.
I appreciate any help. Thanks in advance.

A guid needs 36 characters (because of the dashes). You only provide a 32 character column. Not enough, hence the error.

You need to use one of 3 alternatives
1, A uniqueidentifier column, which stores it internally as 16 bytes. When you select from this column, it automatically renders it for display using the 8-4-4-4-12 format.
CREATE TABLE [dbo].[cust_info](
[uid] uniqueidentifier NOT NULL,
[first_name] [varchar](100) NULL,
[last_name] [varchar](100) NULL)
2, not recommended Change the field to char(36) so that it fits the format, including dashes.
CREATE TABLE [dbo].[cust_info](
[uid] char(36) NOT NULL,
[first_name] [varchar](100) NULL,
[last_name] [varchar](100) NULL)
3, not recommended Store it without the dashes, as just the 32-character components
INSERT INTO dbo.cust_info (
uid,
first_name,
last_name
)
SELECT
replace(NEWID(),'-',''),
first_name,
last_name
FROM dbo.tmp_cust_info

I received this error when I was trying to perform simple string concatenation on the GUID. Apparently a VARCHAR is not big enough.
I had to change:
SET #foo = 'Old GUID: {' + CONVERT(VARCHAR, #guid) + '}';
to:
SET #foo = 'Old GUID: {' + CONVERT(NVARCHAR(36), #guid) + '}';
...and all was good. Huge thanks to the prior answers on this one!

Increase length of your uid column from varchar(32) ->varchar(36)
because guid take 36 characters
Guid.NewGuid().ToString() -> 36 characters
outputs: 12345678-1234-1234-1234-123456789abc

You can try this. This worked for me.
Specify a length for VARCHAR when you cast/convert a value..for uniqueidentifier use VARCHAR(36) as below:
SELECT Convert (varchar(36),NEWID()) AS NEWID
The default length for VARCHAR datatype if we don't specify a length during CAST/CONVERT is 30..
Credit : Krishnakumar S
Reference : https://social.msdn.microsoft.com/Forums/en-US/fb24a153-f468-4e18-afb8-60ce90b55234/insufficient-result-space-to-convert-uniqueidentifier-value-to-char?forum=transactsql

Related

empty string changing to 0 for int

Very unique..... and not authorized to change collation
i have created a table
CREATE TABLE BT_INPUT_CHK
(
[ID] [int] NOT NULL,
[RELID] [int] NULL,
[OPNO] [varchar](35) NULL
)
and when i am inserting data
INSERT ZBT_INPUT_CHK ([ID],[RELID],[OPNO])
VALUES('1000002','','')
the problem starts here.
for varchar empty string inserted as same, but for int empty string is changing to 0.
collation: Latin1_General_CI_AS
can anyone please help me on this.
Thanks
When empty sting is implicitly converted to INT it converts to ZERO. if you need to set it to NULL then use NULLIF function.
DECLARE #RELID VARCHAR(10) = ''
INSERT ZBT_INPUT_CHK ([ID],[RELID],[OPNO])
VALUES('1000002',NULLIF(#RELID,''),'')
Check This..You will get clear idea on Inserting Empty String to Numeric Column
INSERT into BT_INPUT_CHK ([ID],[RELID],[OPNO]) VALUES('1000007',NULL,'');
try this,
CREATE TABLE #BT_INPUT_CHK
(
[ID] [int] NOT NULL,
[RELID] [int] NULL,
[OPNO] [varchar](35) NULL
)
INSERT #BT_INPUT_CHK ([ID],[RELID],[OPNO])
VALUES('1000002',(case when cast('' as varchar(10))='' then 'Error' else '' end),'')

SQL Insert - String or binary data would be truncated for INT values

I'm getting the "String or binary data would be truncated" error when trying to insert integers to one of my tables.
I've read several post about the length of the column vs the length of the value one is inserting, but it doesn't seem to be my case once the columns are all int or smallint type and the values are all maximum two digits.
The table structure is the following:
CREATE TABLE [tblvUserLocation] (
[User_Location_ID] [int] IDENTITY (1, 1) NOT NULL ,
[Location_ID] [int] NULL ,
[Line_Type_ID] [int] NULL ,
[User_ID] [int] NULL ,
[Active] [smallint] NULL CONSTRAINT [DF_tblvUserLocation_Active] DEFAULT (1),
[Last_Updated] [smalldatetime] NULL CONSTRAINT [DF_tblvUserLocation_Last_Updated] DEFAULT (getdate()),
[Last_Updated_By] [varchar] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL CONSTRAINT [DF_tblvUserLocation_Last_Updated_By] DEFAULT (suser_sname())
) ON [PRIMARY]
GO
The insert I'm trying to run is the following:
insert into tblvUserLocation (Location_ID, Line_Type_ID, [User_ID], Active)
values (20, 2, 41, 1)
And the error I'm getting is the following:
Server: Msg 8152, Level 16, State 2, Line 1 String or binary data
would be truncated. The statement has been terminated.
If that makes any difference, I'm using SQL Server 2000.
Please let me know what your thoughts are.
Thanks!
Looks like the problem comes from your [DF_tblvUserLocation_Last_Updated_By] constraint.
It's pulling the current username which is more than likely longer than the length of your [Last_Updated_By] column VARCHAR(10).
Update your DDL to:
[Last_Updated_By] [varchar] (128)

SQL Server Msg 241, Level 16, State 1, Conversion failed when converting date and/or time from character string

I am getting this error when executing a stored procedure that I have created
Msg 241, Level 16, State 1, Procedure UDP_INITIAL_CUSTOMER_DIM_POPULATION, Line 28
Conversion failed when converting date and/or time from character string.
The code for the procedure is:
CREATE PROCEDURE UDP_INITIAL_CUSTOMER_DIM_POPULATION
AS
BEGIN
SET NOCOUNT ON
EXEC UDP_REMOVE_NULLS_FROM_CUSTOMER_STG
INSERT INTO dbo.customer_dim(customer_no, first_name, middle_name, last_name,
street_number, street_name, po_address, zip_code, city,
region, country)
SELECT DISTINCT
CAST(customer_no AS VARCHAR),
first_name,
middle_name,
last_name,
street_number,
street_name,
po_address,
zip_code,
city,
region,
country
FROM
dbo.customer_stg
PRINT 'Initial customer data in place!'
SET NOCOUNT OFF
END
and the code to create the customer_dim table is:
CREATE PROCEDURE dbo.UDP_CREATE_CUSTOMER_DIM
AS
BEGIN
IF OBJECT_ID('customer_dim', 'U') IS NOT NULL
BEGIN
DROP TABLE customer_dim
END
CREATE TABLE [dbo].[customer_dim]
(
[customer_sk] INT IDENTITY (1,1) PRIMARY KEY,
[customer_no] [varchar](50) NULL,
[first_name] [varchar](50) NULL,
[middle_name] [varchar](50) NULL,
[last_name] [varchar](50) NULL,
[customer_name] [varchar](50) NULL,
[street_number] [varchar](50) NULL,
[street_name] [varchar](50) NULL,
[customer_address] [varchar](100) NULL,
[po_address] [varchar](50) NULL,
[zip_code] [varchar](50) NULL,
[city] [varchar](50) NULL,
[region] [varchar](50) NULL,
[country] [varchar](50) NULL,
[effective_date] DATE NULL DEFAULT GETDATE(),
[expiry_date] DATE NULL DEFAULT '999-12-31'
) ON [PRIMARY]
END
I have tried looking for a solution but because I am not trying to do anything on the date or time I cannot find anything that is making sense to me.
The error is caused by the default value for the expiry_date. Running
SELECT CAST('999-12-31'AS DATE)
will produce an error:
Conversion failed when converting date and/or time from character
string.
What you want is
[expiry_date] DATE NULL DEFAULT '9999-12-31'
I'm doing string to date conversion in SQL 2017 and it properly handles strings in m/d/yyyy format with single and double digit days or months and two or four digit years. For example, the following strings converted cleanly:
2/6/2012
1/20/2020
12/21/2017
6/14/21
In my case I found the problem to be a record with a value of '5/15/161'. To find the bad data, use the ISDATE() function. In my case:
SELECT [EMPLOYER NUMBER],[PLAN],[EMPLOYER NAME],[LAST UPDATED]
FROM [CONTACT NAME ADDRESS]
where [last updated] is not null
and isdate([last updated]) <> 1
Default value should be converted to the data type of column by query executor.
Date data type requires data in " YYYY-mm-dd" fromat
Luke problem is with your default vale for date data type.
While sql is trying to convert /cast yyy-mm-dd it fails to convert it to date

SQL server Invalid Column name Invalid object name

I'm having a problem with a table I created. I am trying to run a query however a red line appears under my code ('excursionID', and 'excursions'), claiming 'Invalid Column name 'excursionID' and 'Invalid object name 'dbo.excursions' even though I have created the table already!
Here is the query
SELECT
excursionID
FROM [dbo].[excursions]
Here is the query I used to create the table
USE [zachtravelagency]
CREATE TABLE excursions (
[excursionID] INTEGER NOT NULL IDENTITY (1,1) PRIMARY KEY,
[companyName] NVARCHAR (30) NOT NULL,
[location] NVARCHAR (30) NOT NULL,
[description] NVARCHAR (30) NOT NULL,
[date] DATE NOT NULL,
[totalCost] DECIMAL NOT NULL,
I've tried dropping the table and inserting table again.
For some reason all my other tables work, it's just this table that doesn't identify itself. I'm very new to SQL so thank you for your patience!
You use DB [zachtravelagency] for create table.And You dont use this DB in your query. Default used db master in SSMS. Try
SELECT
excursionID
FROM [zachtravelagency].[excursions]

Computed column based on nullable columns

I want to create a computed column that is the concatenation of several other columns. In the below example, fulladdress is null in the result set when any of the 'real' columns is null. How can I adjust the computed column function to take into account the nullable columns?
CREATE TABLE Locations
(
[id] [int] IDENTITY(1,1) NOT NULL PRIMARY KEY,
[fulladdress] AS (((([address]+[address2])+[city])+[state])+[zip]),
[address] [varchar](50) NULL,
[address2] [varchar](50) NULL,
[city] [varchar](50) NULL,
[state] [varchar](50) NULL,
[zip] [varchar](50) NULL
)
Thanks in advance
This gets messy pretty quick, but here's a start:
ISNULL(address,'') + ' '
+ ISNULL(address2,'') + ' '
+ ISNULL(city,'') + ' '
+ ISNULL(state,'') + ' '
+ ISNULL(zip,'')
(If isnull doesn't work, you can try coalesce. If neither work, share what DMBS you're using.)
You shouldn't have a full address column (which is a duplicate of other columns) stored in your database unless you have a good reason. The correct way would be to construct the full address string in your queries. By creating the field dynamically you reduce redundancy in the table and you have one less column to maintain (which would need to be updated anytime any other column changes).
In your query you would do something like
SELECT CONCAT(ISNULL(address,''), ISNULL(address2,''), ISNULL(city,''), ISNULL(state,''), ISNULL(zip,'')) AS fulladdress FROM Locations;
The CONCAT() function performs concatenation and the ISNULL() gives you your string if it's not null or the second param (which was passed as '') if it is null