Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 months ago.
Improve this question
Context
A partition function is used to horizontally partition a table or index in SQL Server. It identifies the partition boundaries and indicates the behavior of the range - that is from LEFT to RIGHT of the boundary.
Read more in the online docs.
Here's a simple example of a partition function:
IF EXISTS(SELECT * FROM sys.partition_functions WHERE name = 'year_partition_function')
DROP PARTITION FUNCTION year_partition_function;
CREATE PARTITION FUNCTION year_partition_function (INT) AS
RANGE LEFT FOR VALUES (2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
,2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019
,2020, 2021, 2022, 2023, 2024, 2025, 2026, 2027, 2028, 2029, 2030);
The setup
Using $PARTITION in TSQL we can query a partition function with a sample value for the resulting, calculated partition. It is a handy way to debug how a function behaves. In the sample below, I am calling the function created above with one of every value - matching the boundaries exactly - just to see the range of partitions it will return.
Read more in the online docs.
WITH years AS
(
SELECT s.value AS Value
FROM STRING_SPLIT('2000,2001,2002,2003,2004,2005,2006,2007,2008,2009' +
',2010,2011,2012,2013,2014,2015,2016,2017,2018,2019' +
',2020,2021,2022,2023,2024,2025,2026,2027,2028,2029,2030',',') AS s
)
, parts as
(
SELECT Value
, $PARTITION.customer_partition_function(Value) AS [ParitionNumber]
FROM years
)
SELECT [ParitionNumber]
, STRING_AGG(Value, ',') AS Boundaries
FROM parts
GROUP BY [ParitionNumber]
The question
I would have expected the results of this query to have as many partitions as there were boundaries I passed in. Each one was unique and each one was listed as a boundary in the partition function. Instead, I only get a few partitions.
Q: Why don't I get more partitions in this test of the function?
Actual results:
⚠️ This error occurs when you are testing the incorrect partition function! How embarrassing.
I will leave this question here anyway since it has a lot of useful information for anyone trying to get partitions setup & tested.
The correct test query:
WITH years AS
(
SELECT s.value AS Value
FROM STRING_SPLIT('2000,2001,2002,2003,2004,2005,2006,2007,2008,2009' +
',2010,2011,2012,2013,2014,2015,2016,2017,2018,2019' +
',2020,2021,2022,2023,2024,2025,2026,2027,2028,2029,2030',',') AS s
)
, parts as
(
SELECT Value
, $PARTITION.year_partition_function(Value) AS [ParitionNumber]
FROM years
)
SELECT [ParitionNumber]
, STRING_AGG(Value, ',') AS Boundaries
FROM parts
GROUP BY [ParitionNumber]
Actual results (as expected):
Good evening ,
I have an alphanumeric field and I want after the letters to subtract the zeros that it has
my base is azure and my field is a bit like that, the number is 10 digits but I want to subtract the zeros to make it smaller in report. my field is SALDOC.RELDOCSDATE DEAP0000013169 3/12/2021
If your database support REGEXP_REPLACE, then you may try:
SELECT REGEXP_REPLACE('GFRE000005268', '([A-Z]+)0*([1-9][0-9]*)', '\1\2')
Here is a MySQL demo. The actual syntax on your database might be slightly different.
It would depend on the requirement.
You can use this if you are sure that there is always just 4 letter prefix.
This would just convert the numbers to INT to remove those zeros from the start.
SELECT CONCAT(LEFT('GFRE000005268',4),CONVERT(int,(REPLACE('GFRE000005268',LEFT('GFRE000005268',4),''))))
This question already has answers here:
How to pad zeroes for a number field?
(4 answers)
How to add leading zero in a number in Oracle SQL query?
(3 answers)
Closed 1 year ago.
I have a numeric attribute that contains IDs in a table in my database. However, the IDs are not of the same length (e.g. 54261, 73284619, 3723). I want all of them to be in the same length by adding 0s in front of them so that all of them are 10 digits (e.g. 000054261, 0073284619, 0000003723). I'm wondering if I can write a SQL query to achieve this or I have to do this in Excel.
Usually functions like LPAD, RPAD are recommended to achieve your usecase
Try something like below
Select TO_NUMBER(LPAD(TO_CHAR(ID) , 10,'0')) FROM
DUAL;
LPAD will add zeroes to the left of the string id to make it a 10 digit and RPAD likewise is vice versa adding 0s to the right
Note: may not be supported accross all dbs but mainly oracle, postgres etc.
This question already has answers here:
Oracle Date TO_CHAR('Month DD, YYYY') has extra spaces in it
(6 answers)
Closed 8 years ago.
I have a table called room_allocation and want to extract details of those who were admitted in the month of January.So I have used the following query:
select * from room_allocation where to_char(adm_date,'Month')='January ';
the output for this is:
no data found
but when I give it as:
select to_char(adm_date,'Month') from room_allocation;
I get the output as:
TO_CHAR(ADM_DATE,'MONTH')
October
November
December
January
So please tell me why its not working in the first case.
Thank you.
Use the format modifier FM. This will remove the trailing space.
SELECT *
FROM room_allocation
WHERE to_char(adm_date,'FMMonth')='January';
Maybe the extra space you have at the end of January.
select * from room_allocation where to_char(adm_date,'Month')='January ';
Remove that and try it again
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a query in which I am calculating time. I want to make time double so that if the time is 01:40 then it should become 03:20. Here is my current query
SELECT TO_CHAR(TRUNC((ATN_INN-ACT_INN)*24),'09')||':'||
TO_CHAR(TRUNC(((ATN_INN-ACT_INN)*24-TRUNC((ATN_INN-ACT_INN)*24))*60),'09') B
FROM SML.EMP_INFO A
How should I go about this?
If you really what to work only with the time element, the easiest way to so is to employ the 'SSSSS' date mask, which returns the number of seconds after midnight. So this query will return double the number of seconds between the two columns:
select ( to_number(to_char(atn_inn, 'sssss'))
- to_number(to_char(acn_inn, 'sssss')) ) * 2 as double_diff_in secs
from sml.emp_info
/
Converting seconds into hours and minutes is left as an exercise for the reader.
Note that this query only makes sense if ATN_INN is later than ACT_INN but still on the same day. This is the clarification #Ben was trying to make (with no success). If this is not the case a different solution is required, something like ....
select ( ( extract ( hour from diff ) * 60)
+ extract ( minute from diff ) ) *2 as double_diff_in mins
from ( select to_dsinterval ( atn_inn - act_inn ) as diff
from sml.emp_info )
/
This returns the doubled different in minutes. Again, rendering the output into a display format is left to the reader.
It seems to me you are using the method described here (Subtraction between dates): http://www.akadia.com/services/ora_date_time.html
If you want to double the lenght of the period, you really only need to multiply the base time difference with 2, like this:
SELECT TO_CHAR(TRUNC((2*(ATN_INN-ACT_INN))*24),'09')
|| ':' ||
TO_CHAR(TRUNC(((2*(ATN_INN-ACT_INN))*24
-TRUNC((2*(ATN_INN-ACT_INN))*24))*60),'09') B
FROM SML.EMP_INFO A
This will give you the hour part and the minutes part of your results, you will probably want to have the days part too.