Questions tagged [truncate]

Data truncation is the automatic or deliberate shortening of data. A string, decimal number, or datestamp can be truncated to a shorter value. A data stream (such as a file or record set) can be truncated when either the entirety of the data is not needed or when it will be stored in a location too short to hold its entire length. No rounding occurs when numbers are truncated.

Data truncation may occur automatically, such as when a long string is written to a smaller buffer, or deliberately, when only a portion of the data is wanted. The truncation principles can be used in several different ways.

###Function Calls

Many data handling programs have built in methods or functions to handle truncation. Syntax will vary, but the principles are the same. Examples include:

  • Strings: TRUNC("This is a string.", 12) = This is a st
  • Decimals: TRUNC(3.14159, 2) = 3.14 or floor(3.14159) = 3
  • Dates: TRUNC(#7/4/2017 23:45#) = #7/4/2017 00:00#

###SQL Statement

In SQL, the TRUNCATE TABLE statement removes all rows from a table without invoking any triggered actions. The result of this operation quickly removes all data from a table, typically bypassing a number of integrity enforcing mechanisms. It was officially introduced in the SQL:2008 standard. In the SQL standard TRUNCATE TABLE is defined as a data manipulation language (DML) feature but database vendors often classify it as DDL for their own implementation-specific reasons.

The TRUNCATE TABLE mytable statement is logically (though not physically) equivalent to the DELETE FROM mytable statement (without a WHERE clause).

Datestamp values

Datestamps can be truncated. 2009-02-09 09:41:22 can be truncated, for example, to the:

  • year 2009-01-01 00:00:00
  • month 2009-02-01 00:00:00
  • day 2009-02-09 00:00:00
  • hour 2009-02-09 09:00:00

Related tags

References

Function Calls

SQL Statements

1585 questions
101
votes
11 answers

I want to truncate a text or line with ellipsis using JavaScript

I'm looking for a simple script which can truncate a string with ellipsis (...) I want to truncate something like 'this is a very long string' to 'this is a ve...' I don't want to use CSS or PHP.
Dollar Friend
  • 1,019
  • 2
  • 7
  • 4
91
votes
5 answers

Unix shell script to truncate a large file

I am trying to write a Unix script which will truncate/empty a file which is continuously being written/open by an application when it reaches say 3GB of space. I know that the below command would do it : cp /dev/null [filename] But I am going to…
peedee
  • 1,941
  • 2
  • 14
  • 14
76
votes
7 answers

SQL Server silently truncates varchar's in stored procedures

According to this forum discussion, SQL Server (I'm using 2005 but I gather this also applies to 2000 and 2008) silently truncates any varchars you specify as stored procedure parameters to the length of the varchar, even if inserting that string…
Jez
  • 27,951
  • 32
  • 136
  • 233
71
votes
9 answers

Truncate string with Rails?

I want to truncate a string as follows: input: string = "abcd asfsa sadfsaf safsdaf aaaaaaaaaa aaaaaaaaaa dddddddddddddd" output: string = "abcd asfsa sadfsaf safsdaf aa...ddddd"
krunal shah
  • 16,089
  • 25
  • 97
  • 143
71
votes
4 answers

Postgresql Truncation speed

We're using Postgresql 9.1.4 as our db server. I've been trying to speed up my test suite so I've stared profiling the db a bit to see exactly what's going on. We are using database_cleaner to truncate tables at the end of tests. YES I know…
brad
  • 31,987
  • 28
  • 102
  • 155
70
votes
3 answers

Truncate Table Within Transaction

Can the SQL "truncate table" command be used within a transaction? I am creating an app and my table has a ton of records. I want to delete all the records, but if the app fails I was to rollback my transaction. Deleting each record takes a very…
Davin Studer
  • 1,501
  • 3
  • 15
  • 28
69
votes
5 answers

I got error "The DELETE statement conflicted with the REFERENCE constraint"

I tried to truncate a table with foreign keys and got the message: "Cannot truncate table because it is being referenced by a FOREIGN KEY constraint". I read a lot of literature about the problem and thought that I found the solution by using…
Peter
  • 691
  • 1
  • 5
  • 3
69
votes
11 answers

Pros & Cons of TRUNCATE vs DELETE FROM

Could someone give me a quick overview of the pros and cons of using the following two statements: TRUNCATE TABLE dbo.MyTable vs DELETE FROM dbo.MyTable It seems like they both do the same thing when all is said and done; but are there must be…
Jim B
  • 8,344
  • 10
  • 49
  • 77
67
votes
9 answers

How to TRUNCATE TABLE using Django's ORM?

To empty a database table, I use this SQL Query: TRUNCATE TABLE `books` How to I truncate a table using Django's models and ORM? I've tried this, but it doesn't work: Book.objects.truncate()
Silver Light
  • 44,202
  • 36
  • 123
  • 164
67
votes
10 answers

Truncate string on whole words in .NET C#

I am trying to truncate some long text in C#, but I don't want my string to be cut off part way through a word. Does anyone have a function that I can use to truncate my string at the end of a word? E.g: "This was a long string..." Not: "This was a…
TimS
  • 5,922
  • 6
  • 35
  • 55
64
votes
6 answers

How to truncate a file in C?

I'm using C to write some data to a file. I want to erase the previous text written in the file in case it was longer than what I'm writing now. I want to decrease the size of file or truncate until the end. How can I do this?
sofr
  • 5,407
  • 6
  • 28
  • 29
64
votes
3 answers

MySQL truncates concatenated result of a GROUP_CONCAT function

I've created a view which uses GROUP_CONCAT to concatenate results from a query on products column with data type of 'varchar(7) utf8_general_ci' in a column named concat_products. The problem is that MySQL truncates value of "concat_products"…
pouya
  • 3,400
  • 6
  • 38
  • 53
61
votes
8 answers

Limiting double to 3 decimal places

This i what I am trying to achieve: If a double has more than 3 decimal places, I want to truncate any decimal places beyond the third. (do not round.) Eg.: 12.878999 -> 12.878 If a double has less than 3 decimals, leave unchanged Eg.: 125 ->…
Ayush
  • 41,754
  • 51
  • 164
  • 239
61
votes
12 answers

Truncate table(s) with rails console

I have this testingdatabase which, by now, is stuffed with junk. Now I've done a few Table.destroy_all commands in the rails console which deletes all records and dependencies which is awesome. However; I'd like to truncate everything so the ID's…
CaptainCarl
  • 3,411
  • 6
  • 38
  • 71
57
votes
4 answers

Text-overflow CSS truncation

Earlier i was doing it dynamically using JS.. but we were getting some performance issues cuz of which we have to come with an alternative option. I am now truncating a long text on my tab names using text-overflow style. but i have a small issue…
nipiv
  • 773
  • 1
  • 8
  • 21