Using SQL-Server 2008 and concatenating string literals to more than 8000 characters by obvious modification of the following script, I always get the result 8000. Is there a way to tag string literals as varchar(max)?
DECLARE #t TABLE (test varchar(max));
INSERT INTO #t VALUES ( '0123456789012345678901234567890123456789'
+ '0123456789012345678901234567890123456789'
+ '... and 200 times the previous line'
);
select datalength(test) from #t
I used the following code on SQL Server 2008
CREATE TABLE [dbo].[Table_1](
[first] [int] IDENTITY(1,1) NOT NULL,
[third] [varchar](max) NOT NULL
) ON [PRIMARY]
END
GO
declare #maxVarchar varchar(max)
set #maxVarchar = (REPLICATE('x', 7199))
set #maxVarchar = #maxVarchar+(REPLICATE('x', 7199))
select LEN(#maxVarchar)
insert table_1( third)
values (#maxVarchar)
select LEN(third), SUBSTRING (REVERSE(third),1,1) from table_1
The value you are inserting in your example is being stored temporally as a varchar(8000) because. To make the insert one will need to use a variable which is varchar(max) and append to it to overcome the internal 8000 limit.
Try casting your value being inserted as a varchar(max):
INSERT INTO #t VALUES (CAST('0123456789012345678901234567890123456789'
+ '0123456789012345678901234567890123456789'
+ '... and 200 times the previous line' AS varchar(max)
);
Also, you may have to concatenate several <8000 length strings (each casted as varchar(max)).
See this MSDN Forum Post.
When I posted the question, I was convinced that there are some limitations for the length or maximal line width of a single string literal to be used in INSERT and UPDATE statement.
This assumption is wrong.
I was led to this impression by the fact the SSMS limits output width for a single column in text mode to 8192 characters and output of PRINT statements to 8000 characters.
Fact is, as far as I know you need only enclose the string with apostrophes and double all embedded apostrophes. I found no restrictions concerning width or total length of a string.
For the opposite task, to convert such strings back from database back to script the best tool I found is ssms toolspack which works for SQL-Server 2005+.
Related
I have this stored procedure:
Dbo.SprocName (#Id UNIQUEIDENTIFIER,
#ResponseCode INT OUTPUT,
#ResponseDescription VARCHAR(500) OUTPUT)
And it returns a dataset called say Result as a nvarchar(MAX) (always a single row).
I've tried OLE and ADO connections and as well as result sets. I've tried creating a table variable and storing the value there.
Nothing works.
I can see in the database that it's running successfully then it fails when returning the result data set.
I’ve done some debugging and I can assure the result string is returned as should be. The problem is that I don’t know how to handle this on SSIS.
The error that I get is:
Input string was not in a correct format
I appreciate any ideas.
Thanks.
EDIT: I have tried using a table variable again and it works. I guess I didn't do it well first time. sorry about that. Thanks!
One potential cause for your problem could be a mismatch in data types between SSIS and SQL Server.
An SSIS GUID data type does not match a SQL Server uniqueidentifier - the SSIS GUID has curly braces (e.g., {00000000-0000-0000-0000-000000000000}), while the SQL value does not. SQL cannot recognize the value as a unique identifier, and fails to convert.
To pass down a GUID, you will need to remove those curly braces, either in SSIS or in SQL. One approach I've used it to send it across as a VARCHAR and then strip out the curly braces, e.g.,
DECLARE #GUID VARCHAR(40) = '{00000000-0000-0000-0000-000000000000}'
DECLARE #CnvtGUID UNIQUEIDENTIFIER = REPLACE(REPLACE(#GUID, '}', ''), '{', '')
SELECT #GUID, #CnvtGUID
I have large tables of freely formatted text strings stored in MySQL database. Within each of those strings I have to find three substrings which are specifically formatted. This problem looks like an ideal fit for MySQL REGEXP pattern matching.
I know that MySQl REGEXP operator returns only True or False. Moreover, because I need to process large tables, I would need to achieve the goal within MySQL and not to involve PHP or any other server side language.
Example of source data:
FirstEntry_somestring_202320047A_210991957_700443250_Lieferadresse:_modified string c/o Logistics, some address and another text
SecondEntry_hereisanothertext_210991957_text_202320047A_and_700443250_another text which does not have any predefined structure
ThirdEntry_700443250_210991957_202320047A_Lieferadresse:_here some address, Logistics, and some another text with address.
FourthEntry some very long text before numbers__202320047A-700443250-210991957-Lieferadresse:, another text with address and company name. None of this text has predefined structure
The examples above have are four strings stored as TEXT datatypes within MySQL table. They do not have any specific structure. I know however, that somewhere in each records must be three numbers freely delimited and but they have specific format:
Regex Format: '\d{3}(30|31|32)\d{4}[A-Z])'
Regex Format:'(\d{3}(99)\d{4})')
Regex Format: '((700)\d{6})'
Could you please help me how can I get the substrings matching the Regex patterns in the text above?
The Server runs on:
Windows OS
IIS 7
MySQL for Windows
PHP
...
Thank you!
MariaDB 10.0.5 (from 2013) is virtually the same as MySQL, but it includes the full set of REGEXP. Also it has REGEXP_REPLACE().
See https://mariadb.com/kb/en/mariadb/pcre/
For those interested in this question, I have developed my own solution using MySQL Stored Procedures.
I think, this is the most valuable solution on this subject on StackOverflow, as it provides real solution. In contrast to others, there were only vague ideas offered:
-- Return REGEX Value
DELIMITER $$
DROP PROCEDURE IF EXISTS RETURNREGEX$$
CREATE PROCEDURE RETURNREGEX(IN strSentence VARCHAR(1024), IN regex_str VARCHAR(1024), IN length_str INT )
BEGIN
DECLARE index_str INT DEFAULT 0;
DECLARE match_str VARCHAR(1024) DEFAULT '';
DECLARE result BOOL DEFAULT FALSE;
REPEAT
-- Get substring with predefined length
SELECT SUBSTRING(strSentence, index_str, length_str) INTO match_str;
-- compare this substring agains REGEX to see if we have match
SELECT match_str REGEXP regex_str INTO result;
SET index_str = index_str + 1;
-- evaluate result (TRUE / FALSE)
UNTIL result OR index_str > length(strSentence)
END REPEAT;
IF result = TRUE THEN SELECT match_str;
ELSE SELECT NULL;
END IF;
END$$
DELIMITER ;
I am sorry if this is a duplicate question. Please point me in correct direction. I have a table with a column named MailBody with varchar(max) as datatype like below.
CREATE TABLE Mail
(
[ID] [int] IDENTITY(1,1) NOT NULL,
[MailBody] [varchar](max) NULL
)
When I try to insert a very long string, length > 10,000 characters into MailBody, it is not storing full string. It is truncating and then storing the truncated string in MailBody column. Can anybody tell me how to store the full string but not truncated string into MailBody column.
UPDATE
As stated by marc_s below in one of his comments, it is storing the full MailBody string into MailBody column. I created a small C# unit test method to read MailBody column and saw that I am getting my full string without truncated. I didnot change any settings in my SSMS. Thanks marc_s
Using your table structure, I was easily able to insert a 120 KB+ .txt file into the table with this code:
INSERT INTO Mail(MailBody)
SELECT BulkColumn
FROM OPENROWSET (BULK 'c:\tmp\large.txt', SINGLE_CLOB) MyFile
This can be seen by checking the length of the MailBody column:
SELECT ID, LEN(MailBOdy) FROM Mail
Output:
VARCHAR(MAX) is easily able to handle large text - up to 2 billion characters, actually - enough to hold way past 100 copies of the entire "War And Peace" by Leo Tolstoj. ....
DO NOT change your datatype - it's the right datatype to use! There must be something else going on that truncates your data ...
Update: you can set the amount of data that SSMS will show you under Tools > Options:
You can crank up the number of characters displayed - but be aware: the higher you go, the more data might need to be transferred to your computer to be displayed! Don't start complaining about lack of performance if you ask for 2 GB of data for each column! :-)
Need help in below.
I am using sql seerver 2008 and have a query in which i am using like operator.
when i am using a part of string then its working fine however when i am using complete string in like operator database is not populating any results.
for example.
i have table EMp which containd description column.
if descreption column contains
Description
-------------------------------------------------
'John joined on Wednesday/CELL PHONE [1234567890]'
when i am writing query
select * from EMp where
description like '%CELL%'
its working fine
however when i am writing my query as
select * from EMp where
description like '%John joined on Wednesday/CELL PHONE [1234567890]%'
its not returning any value.
does it mean that like operator works only part of string and not on full string.
I have also tried LTRIM and RTRIM just to make sure that space is not a problem however its not working.
Thanks
Keep in mind that LIKE supports a limited set of pattern matches in addition to the wildcard %. One of those patterns includes brackets for matching a range.
See: http://msdn.microsoft.com/en-us/library/ms179859.aspx
The brackets in your query will cause it to search for "Any single character within the specified range ([a-f]) or set ([abcdef])."
description like '%John joined on Wednesday/CELL PHONE [1234567890]%'
Thus, your query is asking for SQL Server to find a character within the set [1234567890].
If you read through the MSDN documentation, it provides guidelines for using wildcard characters as literals. Here's a little example:
DECLARE #table TABLE ( SomeText VARCHAR( 100 ) );
INSERT #table ( SomeText ) VALUES ( 'here is a string with [brackets]' );
-- matches with wildcards on both sides of the pattern
SELECT * FROM #table WHERE SomeText LIKE '%[brackets]%';
-- won't match
SELECT * FROM #table WHERE SomeText LIKE '%here is a string with [brackets]%';
-- matches, because expression is escaped
SELECT * FROM #table WHERE SomeText LIKE '%here is a string with [[brackets]%';
-- a confusing (but valid) escape sequence AND a wildcard
SELECT * FROM #table WHERE SomeText LIKE '%here is a string with [[][a-z]rackets]%';
Note that full text indexing may be more useful if you want to search larger strings with more complex patterns. It is supported in all editions of SQL Server 2008 (even Express).
I've got a column called description of type NVARCHAR(MAX) - biggest you can have. I need to return this field with quotes around it, so I'm trying to
SELECT QUOTENAME(description, '"')
This doesn't work - I get a "string or binary data would be truncated error."
My googling tells me that this problem can be solved by using SET ANSI_WARNINGS OFF, but if I do that, I still get the same error anyway.
Normally I would just pull the values into a temp table and use a field that is two characters bigger than the field I'm pulling in, thus ensuring that the QUOTENAME function won't cause any problems. How do I make a column two characters bigger than MAX, though?
QUOTENAME is a function intended for working with strings containing SQL Server identifier names and thus only works for strings less than or equal to the length of sysname (128 characters).
Why doesn't SELECT '"' + description +'"' work for you?