I have a field with datatype VARCHAR and MAX length.
However, I just want to print only the first 2000 characters from that field.
I cannot use VARCHAR(2000) as the datatype for the field upon table creation since there are a records exceeding this length and as you might all know, this results in a 'String or binary data would be truncated' error.
What I'm doing is sending an email report which may get too lengthy because of that field so I just want to output the first 2000 characters.
Any help would be greatly appreciated! Thanks in advance!
you can use substring.
select SUBSTRING(yourcolumnname,0,2000) from yourtablename
Related
Can anyone help me to fix this issue please?
I'm getting an error while trying to convert this to BIGINT:
SELECT CONVERT(BIGINT, '10472553255347451137')
Thanks
Well, your number simply is to large for a bigint. A bigint's maximum is 9223372036854775807, see the documentation.
And as 9223372036854775807 < 10472553255347451137, there's no way to convert 10472553255347451137 to a bigint.
And BTW, do not post images of error messages. Paste the text into the post's text.
If you query for the length of the number, it's 20. Therefore, you may use Numeric(20, 0) to store the value.
Length
SELECT len('10472553255347451137')
20
Select converted value
SELECT CONVERT(numeric(20, 0), '10472553255347451137')
10472553255347451137
I am getting the below error while doing a count distinct on a varchar field
Function EXTRACT does not support VARCHAR(16777216) argument type
I am not able to figure out how to solve this
I am getting the below error while doing a count distinct on a varchar field
Please include a query that reproduces the issue for better answers. The limited description does not cover the context behind the error message.
Function EXTRACT does not support VARCHAR(16777216) argument type
Going by the error message alone, it appears you've used more than just COUNT over your VARCHAR typed column (perhaps there are predicates with other functions in use).
Specifically, Snowflake's EXTRACT function cannot be applied to a VARCHAR type, it only accepts a DATE or TIMESTAMP type.
This is just a example/guess, but replace your EXTRACT(year FROM column_name) portion with EXTRACT(year FROM TO_TIMESTAMP(column_name)) if the column can be parsed as a timestamp.
Thanks Harsh, you are right. I also had a where clause on a data field and I was doing a YEAR(). I assumed that the date field is of timestamp data type. But someone defined it as VARCHAR and that was causing the issue
hopefully the title describes what I'm trying to do.
I have a varchar field in a SQL Server 2008 table that contains text dates in the format dd-mm-yyyy (e.g., 31-12-2009). I am trying to use CONVERT to convert it to a DATE field. I was successful in converting a similar varchar field in the same table using the following:
SELECT DISTINCT(CONVERT(DATE, MYDATEFIELD1, 103)) AS [CONV_MYDATEFIELD1] FROM MYTABLE;
But when I apply the same to MYDATEFIELD2, which appears to have the same type of data values as MYDATEFIELD1, it fails with the following error:
Msg 241, Level 16, State 1, Line 1
Conversion failed when converting date and/or time from character string.
I've tried sorting and using LIKE to try to find any characters that might prevent the conversion but I haven't been able to pinpoint anything.
Any help will be greatly appreciated. Thanks!
You may have some invalid dates (e.g. 30-02-2009), try to find them splitting the characters and validating the day and the months, assuring that the days correspond to the month and the month is in the range 01 - 12.
If you can't find which value is causing the conversion error then use a cursor to go through all the records individually and use TRY CATCH to find which record(s) cause the conversion error. You could use a PRINT statement in the CATCH block to identify the records that are erroring.
Find your bad dates with the following:
SET DATEFORMAT dmy;
select MYDATEFIELD1, isdate(MYDATEFIELD1)
from MYDATEFIELD1
I figured out the issue that was causing the CONVERT to fail but I'm not sure of the best way to select an answer (veritable stack noob) so, any help on that would be appreciated. Here are the major steps I took to find the issue:
I used MIN and MAX SUBSTRING to identify that the component parts of the
varchar field were correct (i.e., the 1st two digits min=01 max=31,
middle two min=01 max=12)
I used DISTINCT SUBSTRING to identify that all of the date separators were consistent (i.e., all dashes).
I used MAX(LEN) to determine that my varchar "date" field was 12 characters (vs. the 10 characters I was expecting).
I used CONVERT(VARBINARY, MYDATEFIELD2) to determine what was actually stored in the string.
The last step revealed that the field contained line feeds (00A). I opened the source text file in notepad++, clicked View -> Show Symbol -> Show All Characters and I could see the LF at the end of each line.
So now I'm modifying the DTSX package (fixed width text) to include an extra field for the linefeed that I can drop afterwards. Now that I know what the intended format of the date fields is, I'll try to import them as DT_DATE vs DT_STR. I'm not exactly sure how to specify the correct date style 105 at import (thanks #Panagiotis Kanavos) but I'll figure it out.
Whew! What a learning experience! :D
Thanks to everyone who helped - and if you can give advice on the best way to select the best answer it will be greatly appreciated.
What would be the best way to store a large amount of text in a Database? I would expect about 2500 words and since on average each word in English is around 6 characters, I expect over 15000 characters. This text may be non-English so I guess I would need Unicode to support everything.
This text needs to be inserted, retrieved, and also searched by keywords.
Maxwell.
You should use NVARCHAR(MAX) as the datatype for that particular column in question. Also, I would suggest you have a FULLTEXT INDEX on that column since you said that column will also include searching by keywords.
You need to use NVARCHAR(MAX)
You can store..
1 billion, 73 million, 741 thousand and 823 characters
If you will insert non-english characters you have to use NVARCHAR, and also when inserting the data you have to prefix it with an N like this:
CREATE TABLE tmp( description NVARCHAR(MAX) )
INSERT INTO tmp VALUES (N'Добро...')
I really want to have a column of inifinite length for one of the properties of my ActiveRecord object of type string, but I don't know how to set it. When I set the length to -1 (the number for MAX in SQL server) I get an error.
Can anyone help?
EDIT: I meant to say Castle ActiveRecord.
I found the answer. Apparently if you set the length to anything greater than 4000 it will set the column to MAX length.
the varchar(max) or nvarchar(max) type is suitable for you.
set the data type of the column to text....