I have a column in snowflake which contain big URLs I have given it varchar(50), does varchar(50) mean it can only store 50 characters?
Thanks,
Xi
Does varchar(50) mean it can only store 50 characters?
Yes.
CREATE OR REPLACE TABLE t(col VARCHAR(50));
INSERT INTO t(col) SELECT REPEAT('a', 51);
-- String 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' is too long
-- and would be truncated
SELECT CAST(REPEAT('a', 51) AS VARCHAR(50));
-- String 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' is too long
-- and would be truncated
Related
create procedure SP_insert_test #name varchar(20), #emailid varchar(20), #trainer_name varchar(50), #training_date varchar(50), #training_time varchar(50), #gymname varchar(50) , #success int out as
begin
if(
select
count(id)
from
Add_Booking_Fitness_Training
where
training_time = #training_time) > 11 print N'Number of Booking Is Complete for this time and date plz book other time';
else
insert into
Add_Booking_Fitness_Training(memeber_name, member_emailid, trainer_name, training_date, training_time, gymname)
values
(
#name,
#emailid,
#trainer_name,
#training_date,
#training_time,
#gymname
)
SELECT
SCOPE_IDENTITY()
set
#success = 1;
end
begin
set
#success = 0;
end
i have an table in which i want to insert data on give time only 12 member can insert at that time after that they get message list is full plz change the time for inserting i have create procedure its working when its reach number of 12 than its show me message but when i change the time its also show me the same message and not insert any data into database
like 26/04/2018,'6:00' i want to insert this value only 12 time after 12 this show me a message about the limit of number is reach plz change (time)
Create table Add_Booking_Fitness_Training ( id int identity primary key,
memeber_name varchar(20),
member_emailid varchar(20),
trainer_name varchar(50),
training_date varchar(50),
training_time varchar(50),
gymname varchar(50))
i just want to inserting a value into this table only 12 time for a give time like (6:00) if the number of inserting value reach to 12 than its show me the message number of values insert is reach to 12 please change the time.
i want input the value into table only 12 time for a give time 6:00Am when the value is insert into table 12 time than message come up for change time than insert value for change time
Honestly, I am completely guessing here, I still don't really know what you're asking.
I think the OP's statement of "i want input the value into table only 12 time for a give time 6:00Am when the value is insert into table 12 time than message come up for change time than insert value for change time." means that they only want a time to appear in the table up to 12 times. If it appears more than that, the INSERT fails.
This can be achieved with a check constraint and a scalar function. So, as a very simple example:
USE Sandbox;
GO
--Create a very simple table
CREATE TABLE SampleTable (TrainingTime datetime2(0));
GO
--Create the scalar function
CREATE FUNCTION TrainingAtTime (#TrainingTime datetime2(0))
RETURNS INT
AS BEGIN
DECLARE #Trainees int;
SELECT #Trainees = COUNT(*)
FROM SampleTable
WHERE TrainingTime = #TrainingTime;
RETURN #Trainees;
END
GO
--Add the check constraint
ALTER TABLE SampleTable ADD CONSTRAINT MaxTrainees CHECK (dbo.TrainingAtTime(TrainingTime) <= 12) ;
GO
--Insert first trainee
INSERT INTO SampleTable
VALUES ('2018-04-26T06:00:00');
--It works
SELECT TrainingTime, COUNT(*) AS Trainees
FROM SampleTable
GROUP BY TrainingTime;
GO
--insert 11 more
INSERT INTO SampleTable
VALUES ('2018-04-26T06:00:00'),
('2018-04-26T06:00:00'),
('2018-04-26T06:00:00'),
('2018-04-26T06:00:00'),
('2018-04-26T06:00:00'),
('2018-04-26T06:00:00'),
('2018-04-26T06:00:00'),
('2018-04-26T06:00:00'),
('2018-04-26T06:00:00'),
('2018-04-26T06:00:00'),
('2018-04-26T06:00:00');
--It works
SELECT TrainingTime, COUNT(*) AS Trainees
FROM SampleTable
GROUP BY TrainingTime;
GO
--Try to insert another
INSERT INTO SampleTable
VALUES ('2018-04-26T06:00:00');
--It fails
SELECT TrainingTime, COUNT(*) AS Trainees
FROM SampleTable
GROUP BY TrainingTime;
GO
--Use a different time
INSERT INTO SampleTable
VALUES ('2018-04-26T08:00:00');
--it works
SELECT TrainingTime, COUNT(*) AS Trainees
FROM SampleTable
GROUP BY TrainingTime;
GO
--Clean up
DROP TABLE SampleTable;
DROP FUNCTION TrainingAtTime;
GO
If this isn't what you're after, unfortunately I don't understand your requirements due the the language barrier (and absence of a question).
I am writing a stored procedure that will dummy some credit card data (please note that this is not live!) It is for internal purposes only. This sp runs, but it is only printing out a subset of 10 character (numeric) lengths and not 16. Does anyone have any insight?
Here is my SProc:
DECLARE #RESULT int
DECLARE #cc as varchar(50) = (SELECT ABS(CAST(CAST(NEWID() AS VARBINARY) AS INT)))
UPDATE trans
SET trans_CCNUM =(SELECT stuff(#cc,1,LEN(#cc)-4,REPLICATE('x', LEN(#cc)-5)))
where LEN(trans_ccNum) = 16;
PRINT #RESULT
Here are the results
dateCreated trans_CCNUM
2014-09-11 16:55:13.800 xxxx9328
If your example above, #cc is only going to be 9 or 10 characters long...
DECLARE #cc as varchar(50) =
(SELECT ABS(CAST(CAST(NEWID() AS VARBINARY) AS INT)))
SELECT #cc,len(#cc)
select stuff(#cc,1,LEN(#cc)-4,REPLICATE('x', LEN(#cc)-5))
That is why you are only seeing 9/10 characters
Try changing the INT to BIGINT and you should be OK
I have a column of type ntext called Emp_details_list, and it consists of data like
emp1###emp2###emp3...
At most it has 20 thousand characters as string and I am storing in that column and I need to split it and save in other table EmpDet and in other column (Single_Emp_det) but while splitting I can't cast ntext as nvarchar so am using a local variable and declared as nvarchar(max) and splitting but I can store only 8000 character only if I have 8001 characters it showing exception because it can't store so how can I store whole ntext data in other column using splitting concept in SQL Server
So you are probably stuck with Sql server 2000. If you can't use nvarchar(max), one possible way is to may be use substring function and copy your ntext to manageable chunks of varchar(8000) in a loop. In each iteration, save the 'part of chunk after the last #', to be used in next iteration. So you basically loop over your table, within that loop again loop over the ntext field value in chunks of 8k and do the rest. Hope it is clear enough.
As others already mentioned you can easily store 20000 characters in nvarchar(max). You are probably doing something wrong when converting these types.
Here is an example of converting from and to nvarchar(max) that clearly shows how can you store 20000 characters there.
DECLARE #v1 nvarchar(max)
DECLARE #v2 nvarchar(max)
create table #textExample
(
id int,
t1 ntext
)
declare #count int
set #v1 = ''
SET #count = 0
while #count < 20000
begin
set #v1 = #v1 + '1'
set #count = #count + 1
end
--converting nvarchar(max) to ntext
insert into #textExample
values (1, CONVERT(ntext,#v1))
select * from #textExample
-- converting ntext back to nvarchar(max)
SET #v2 = CONVERT(nvarchar(max), (select t1 from #textExample where id = 1))
select #v2, LEN(#v2)
drop table #textExample
alter procedure SP_employeedetails
(
#tablename varchar(50),
#empname varchar(50),
#age int,
#address varchar (50)
)
as
begin
declare #xxx varchar(50)
set #xxx= 'create table '+#tablename+'
( '+#empname+'varchar(50),
'+#age+' int ,
'+#address+' varchar(50)
)'
print #xxx
exec (#xxx)
end
When I excecuted this using the statement
exec SP_employeedetails 'Employeedetails', 'jhon', 25, 'cochin'
following error message was displayed
Msg 245, Level 16, State 1, Procedure SP_employeedetails, Line 11
Conversion failed when converting the varchar value 'create table
Employeedetails ( jhonvarchar(50),
' to data type int.
A few potential problems:
space between column name and data type
attempt to add an int to a string
your string could almost certainly exceed 50 characters
the age column name is a number - this needs to be properly escaped. I escaped all to be safe. You should watch out for an #empname like Jimmy O'Shea or addresses like 253 Main Street - these column names won't be valid in your current script.
50 characters is clearly not enough for your string. Let's try it:
declare #xxx varchar(50)
set #xxx= 'create table employeedetails
( john varchar(50),
[25] int ,
cochin varchar(50)
)'
print #xxx;
Results:
create table employeedetails
( john varchar(50),
Here is how I would do it:
ALTER PROCEDURE dbo.SP_employeedetails
(
#tablename varchar(50),
#empname varchar(50),
#age int,
#address varchar (50)
)
as
begin
SET NOCOUNT ON;
DECLARE #sql NVARCHAR(2000); -- 50 is unlikely enough
SET #sql = N'create table dbo.' + #tablename + '
( ' + QUOTENAME(#empname) + ' varchar(50), -- need space here
'+ QUOTENAME(CONVERT(VARCHAR(12), #age) +' int , -- need to convert this and delimit
'+ QUOTENAME(#address) + ' varchar(50)
);';
PRINT #sql;
EXEC sp_executesql #sql;
END
GO
However, as I mentioned in a comment, are you really creating a new table for every employee that's added to your system? It seems like this should be inserting a row into a table, not creating a whole new table.
And finally, you should not be using the SP_ prefix for stored procedures.
You're missing a whitespace before the type:
'+#empname+'varchar(50),
And you must cast the #age parameter to a varchar type.
[My answer explains the error you're getting, but it is the least of your problems - Aaron's answer contains a detailed analysis of them]
You need to explicitly convert #age to varchar.
Data Type Precedence:
When an operator combines two expressions of different data types, the rules for data type precedence specify that the data type with the lower precedence is converted to the data type with the higher precedence. If the conversion is not a supported implicit conversion, an error is returned. When both operand expressions have the same data type, the result of the operation has that data type.
Data type int has higher precedence than varchar, so SQL Server tries to convert #xxx to int.
What I need is to search for a string in a specific column (datatype: text) of a table and replace it with another text.
For example
Id | Text
-----------------------------
1 this is test
2 that is testosterone
If I chose to replace test with quiz, results should be
this is quiz
that is quizosterone
What I've tried so far?
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROC [dbo].[SearchAndReplace]
(
#FindString NVARCHAR(100)
,#ReplaceString NVARCHAR(100)
)
AS
BEGIN
SET NOCOUNT ON
SELECT CONTENT_ID as id, CONTENT_TEXT, textptr(CONTENT_TEXT) as ptr, datalength(CONTENT_TEXT) as lng
INTO #newtable6 FROM HTML_CONTENTS
DECLARE #COUNTER INT = 0
DECLARE #TextPointer VARBINARY(16)
DECLARE #DeleteLength INT
DECLARE #OffSet INT
SELECT #TextPointer = TEXTPTR(CONTENT_TEXT)
FROM #newtable6
SET #DeleteLength = LEN(#FindString)
SET #OffSet = 0
SET #FindString = '%' + #FindString + '%'
WHILE (SELECT COUNT(*)
FROM #newtable6
WHERE PATINDEX(#FindString, CONTENT_TEXT) <> 0) > 0
BEGIN
SELECT #OffSet = PATINDEX(#FindString, CONTENT_TEXT) - 1
FROM #newtable6
WHERE PATINDEX(#FindString, CONTENT_TEXT) <> 0
UPDATETEXT #newtable6.CONTENT_TEXT
#TextPointer
#OffSet
#DeleteLength
#ReplaceString
SET #COUNTER = #COUNTER + 1
END
select #COUNTER,* from #newtable6
drop table #newtable6
SET NOCOUNT OFF
I get the error:
Msg 7116, Level 16, State 4, Procedure SearchAndReplace, Line 31
Offset 1900 is not in the range of available LOB data.
The statement has been terminated.
Thank you
If you can't change your column types permanently, you can cast them on the fly:
ALTER PROC [dbo].[SearchAndReplace]
(#FindString VARCHAR(100),
#ReplaceString VARCHAR(100) )
AS
BEGIN
UPDATE dbo.HTML_CONTENTS
SET CONTENT_TEXT = cast (REPLACE(cast (CONTEXT_TEXT as varchar(max)), #FindString, #ReplaceString) as TEXT)
END
The datatype TEXT is deprecated and should not be used anymore - exactly because it's clunky and doesn't support all the usual string manipulation methods.
From the MSDN docs on text, ntext, image:
ntext, text, and image data types will
be removed in a future version of
MicrosoftSQL Server. Avoid using these
data types in new development work,
and plan to modify applications that
currently use them. Use nvarchar(max),
varchar(max), and varbinary(max)
instead.
My recommendation: convert that column to VARCHAR(MAX) and you should be fine after that!
ALTER TABLE dbo.HTML_CONTENTS
ALTER COLUMN CONTEXT_TEXT VARCHAR(MAX)
That should do it.
When your column is VARCHAR(MAX), then your stored procedures becomes totally simple:
ALTER PROC [dbo].[SearchAndReplace]
(#FindString VARCHAR(100),
#ReplaceString VARCHAR(100) )
AS
BEGIN
UPDATE dbo.HTML_CONTENTS
SET CONTENT_TEXT = REPLACE(CONTEXT_TEXT, #FindString, #ReplaceString)
END
Two observations on the side:
it would be helpful to have a WHERE clause in your stored proc, in order not to update the whole table (unless that's what you really need to do)
you're using TEXT in your table, yet your stored procedure parameters are of type NVARCHAR - try to stick to one set - either TEXT/VARCHAR(MAX) and regular VARCHAR(100) parameters, or then use all Unicode strings: NTEXT/NVARCHAR(MAX) and NVARCHAR(100). Constantly mixing those non-Unicode and Unicode strings is a mess and causes lots of conversions and unnecessary overhead