How to get the byte size of resultset in an SQL query? - sql

Is it possible to get the size in bytes of the results of an sql query in MySQL?
For example:
select * from sometable;
ths returns 10000 rows. I don't want the rows but the size of the resultset in bytes. Is it possible?

select sum(row_size)
from (
select
char_length(column1)+
char_length(column2)+
char_length(column3)+
char_length(column4) ... <-- repeat for all columns
as row_size
from your_table
) as tbl1;
char_length for enum, set might not accurate, please take note

To build on Angelin's solution, if your data contains nulls, you'll want to add IFNULL to each column:
select sum(
ifnull(char_length(column1), 0) +
ifnull(char_length(column2), 0) +
ifnull(char_length(column3), 0) +
ifnull(char_length(column4), 0) ... <-- repeat for all columns
)
from your_table

simplify :
select sum(char_length(column1)+
char_length(column2)+
char_length(column3)+
char_length(column4) ... )<-- repeat for all columns
from your_table
You need to add IFNULL() to each column as #futilerebel has mentioned

CHAR_LENGTH() gets number of characters if unicode will be more bytes - use LENGTH() for number of bytes:https://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_length

Related

how can I convert a number to a string, extract each digit, cast it again to integers, and add them all together (e.g. 1+2+3+4+5+6) in SQL?

I'm thinking I have to create a view that stores the cast as text and I have to utilize the substring function in sql but I'm a little lost. Any help would be greatly appreciated?
You can convert a string to rows using unnest() and string_to_array() and then add the digits using sum()
select sum(digit::int)
from unnest(string_to_array(12345::text, null)) as x(digit)
You can format the number
select to_char(12345, '0+0+0+0+0+0+0');
to_char
----------------
0+0+1+2+3+4+5
Inside a function, you can run this statement as a dynamic query to get the result
EXECUTE 'SELECT ' || to_char(12345, '0+0+0+0+0+0+0');
You can use a recursive CTE:
WITH RECURSIVE qsum AS (
SELECT 123456 AS num,
0 AS sum
UNION ALL
SELECT num / 10,
sum + num % 10
FROM qsum
WHERE num > 0
)
SELECT max(sum)
FROM qsum;
max
═════
21
(1 row)
You can get there with regexp_split_to_table, using ?!^ to split on every character.
regexp_split_to_table(cast(<your column> as text),'(?!^)') as s
Here's a simple Fiddle
You can shred the string to rows, then sum:
SELECT SUM(CAST(n as INTEGER))
FROM regexp_split_to_table('01234567', '') as n
But this sort of string math is probably a better fit for calling code. If you are trying to validate new records, consider doing it outside of the DB.

How to check the last two digits?

SUBBIS
SUBB1D
SUBBD3
SUBB12
In above values, how can I check the last two digits (IS, 1D, D3, 12) are numbers using a sql code?
Do you mean to fetch those values? You can do that with like:
where column like '%[0-9][0-9]'
If you need to ensure that the values always end with 2 numbers, you can do it with similar check constraint.
To check the last two digits are numbers in column, you can use the following script.
... WHERE ISNUMERIC(RIGHT(your_column,2)) = 1
Here RIGHT(your_column,2) will return the last two digits from the string.
or
SELECT ISNUMERIC(RIGHT(your_column,2))
will return 1 (if its number) otherwise 0
You can do it this way:
SELECT MyId,
ISNUMERIC(RIGHT(MyColumn,2)) -- your column to check last 2 (if numeric)
FROM (
----- replace with your table
SELECT 1 MyId,'SUBBIS' MyColumn UNION SELECT 2,'SUBB1D' UNION
SELECT 3,'SUBBD3' UNION SELECT 4,'SUBB12'
----- replace with your table
) A
Hope it helps. :)
You can use like and _ "underscore" to get last one digits record columName
SELECT columName FROM sub WHERE columName LIKE "SUBB__" ;
Record :
columName
SUBBIS
SUBB1D
SUBBD3
SUBB12
SUBBBA

SQL Query for .005

I want to be able to do a SQL Query that gets all numbers that end in .005
Select * from amount where (numbers end in X.XX5)
I don't care what the numbers are for X
I am assuming you don't care what the first and second digit after the decimal is.Try this:
select * from MyTable where MyField like '%.__5'
The % specifies a substitution for 0 or more characters, and the underscores are a substitute for a single character.
If there are digits after the 5, use
select * from MyTable where MyField like '%.__5%'
SELECT * FROM amount WHERE numbers LIKE '%.005';
Try this:-
Select * from amount where number like '%.005'
or
Select * from amount where number like '%.005%'
Considering the data type of number as string

T-SQL Substring - Last 3 Characters

Using T-SQL, how would I go about getting the last 3 characters of a varchar column?
So the column text is IDS_ENUM_Change_262147_190 and I need 190
SELECT RIGHT(column, 3)
That's all you need.
You can also do LEFT() in the same way.
Bear in mind if you are using this in a WHERE clause that the RIGHT() can't use any indexes.
You can use either way:
SELECT RIGHT(RTRIM(columnName), 3)
OR
SELECT SUBSTRING(columnName, LEN(columnName)-2, 3)
Because more ways to think about it are always good:
select reverse(substring(reverse(columnName), 1, 3))
declare #newdata varchar(30)
set #newdata='IDS_ENUM_Change_262147_190'
select REVERSE(substring(reverse(#newdata),0,charindex('_',reverse(#newdata))))
=== Explanation ===
I found it easier to read written like this:
SELECT
REVERSE( --4.
SUBSTRING( -- 3.
REVERSE(<field_name>),
0,
CHARINDEX( -- 2.
'<your char of choice>',
REVERSE(<field_name>) -- 1.
)
)
)
FROM
<table_name>
Reverse the text
Look for the first occurrence of a specif char (i.e. first occurrence FROM END of text). Gets the index of this char
Looks at the reversed text again. searches from index 0 to index of your char. This gives the string you are looking for, but in reverse
Reversed the reversed string to give you your desired substring
if you want to specifically find strings which ends with desired characters then this would help you...
select * from tablename where col_name like '%190'

How can I select the first 100 characters in SQL Server?

I want to truncate a column to a max of 100 characters. How do you do this in SQL Server?
Try this:
SELECT LEFT (your_column, 100) FROM your_table
Edit:
you can also try something like this:
SELECT LEFT (your_column, LEN(your_column)-5) FROM your_table
for say if you want to trim the last 5 characters from a record.
You can also use the LEFT() function.
LEFT(col, 100)
SUBSTRING(myColumn, 1, 100)
See the docs: http://msdn.microsoft.com/en-us/library/ms187748.aspx
substring is the method:
SUBSTRING ( value_expression ,start_expression , length_expression )
from the help.
SELECT SUBSTR(COLUMN_NAME, 1, LENGTH) FROM TABLENAME where LENGTH(COLUMN_NAME) > LENGTH
Ex:
SELECT SUBSTR(DESCRIPTION,1,100) FROM STOREDETAILS where LENGTH(DESCRIPTION)>100
For those records, with length less than 100, the actual value would be shown.
Otherwise, some databases induce blank characters in the resultant records.