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
23
votes
4 answers

List of tables to safely truncate in Magento?

Is there a list of tables that can be safely truncated in Magento? By safely, I mean to preserve products. I've got a few but want to know if there are more: core_url_rewrite # Only safe if no custom rewrites are in…
user1529891
21
votes
3 answers

truncate string from a certain character in R

I have a list of strings in R which looks like: WDN.TO WDR.N WDS.AX WEC.AX WEC.N WED.TO I want to get all the postfix of the strings starting from the character ".", the result should look like: .TO .N .AX .AX .N .TO Anyone have any ideas?
user802231
  • 853
  • 2
  • 8
  • 10
21
votes
6 answers

What is the best way to empty a self-referential MySQL table?

I have a self-referential MySQL table with a recursive parent_id: CREATE TABLE `recursive` ( `id` int(11) NOT NULL auto_increment, `parent_id` int(11) default NULL, `name` varchar(100) NOT NULL, PRIMARY KEY (`id`), KEY…
Mat
  • 82,161
  • 34
  • 89
  • 109
21
votes
14 answers

Truncate float numbers with PHP

When a float number needs to be truncated to a certain digit after the floating point, it turns out that it is not easy to be done. For example if the truncating has to be done to second digit after the point, the numbers should be 45.8976 =>…
Dessislava Mitova
  • 371
  • 2
  • 3
  • 8
21
votes
4 answers

postgres truncate is slow

In postgres 9.2 (CentOS), TRUNCATE TABLE command occasionally took a really long time to run. One time, it took more than 1.5 hours to truncate a table with 100K records, even longer in other cases. This problem also happened when I used pgAdmin to…
toanong
  • 363
  • 2
  • 4
  • 13
20
votes
2 answers

When is it better to use trunc() instead of int() to convert floating type numbers to integers?

trunc and int functions return the same output for every float type inputs that I have tried. They differ in the way that int can also be used to convert numerical strings to integers. So I have a two-fold question: I would like to know if, apart…
whateven
  • 344
  • 4
  • 9
20
votes
12 answers

Truncate a decimal value in Python

I am trying to truncate a decimal value in Python. I don't want to round it, but instead just display the decimal values upto the specified accuracy. I tried the following: d = 0.989434 '{:.{prec}f}'.format(d, prec=2) This rounds it to 0.99. But I…
visakh
  • 2,503
  • 8
  • 29
  • 55
20
votes
6 answers

Using hibernate/hql to truncate a table?

What is the recommended way to truncate a table using hibernate/hql? I've tried this: Query query = session.createQuery("truncate table MyTable"); query.executeUpdate(); But it didn't work (truncate doesn't seem do be documented anywhere in…
user149100
  • 1,089
  • 3
  • 12
  • 16
19
votes
4 answers

Truncate or Drop and Create Table

I have this table in a SQL Server 2008 R2 instance which I have a scheduled process that runs nightly against it. The table can have upward to 500K records in it at any one time. After processing this table I need to remove all rows from it so I am…
Mark Kram
  • 5,672
  • 7
  • 51
  • 70
19
votes
6 answers

How to truncate or pad a string to a fixed length in c#

Is there a one-liner way of setting a string to a fixed length (in C#), either by truncating it or padding it with spaces (' '). For example: string s1 = "abcdef"; string s2 = "abc"; after setting both to length 5, we should have: "abcde" "abc "
Graham
  • 7,807
  • 20
  • 69
  • 114
19
votes
5 answers

How to empty a SQL database?

I'm searching for a simple way to delete all data from a database and keep the structure (table, relationship, etc...). I using postgreSQL but I think, if there a command to do that, it's not specific to postgres. Thanks, Damien
Damien
  • 583
  • 3
  • 7
  • 18
19
votes
1 answer

WPF scale text to fit only when too big

I am trying to setup a list of items in WPF which contains strings of random length (people's names). The majority of them are generally within a certain size, but occasionally you come across a string so long that it runs out of the bounds of it's…
Robert Petz
  • 2,718
  • 4
  • 23
  • 52
18
votes
2 answers

How to truncate an UTF8 string in PHP?

I have some strings in my PHP code that need to be truncated if they are too long. For example if a text is something like this: Hi, I would like to tell you how wonderful this is. It would replace it with this: Hi, I would like to ... For that…
Andrei
  • 1,183
  • 2
  • 19
  • 40
18
votes
8 answers

How to generate a compiler warning/error when object sliced

I want to know if it is possible to let compiler issue a warning/error for code as following: Note: 1. Yea, it is bad programming style and we should avoid such cases - but we are dealing with legacy code and hope compiler can help identify such…
Baiyan Huang
  • 6,463
  • 8
  • 45
  • 72
18
votes
6 answers

How do I truncate a decimal in PHP?

I know of the PHP function floor() but that doesn't work how I want it to in negative numbers. This is how floor works floor( 1234.567); // 1234 floor(-1234.567); // -1235 This is what I WANT truncate( 1234.567); // 1234 truncate(-1234.567); //…
chrislondon
  • 12,487
  • 5
  • 26
  • 65