Pad numbers with leading zeros in an Access query - ms-access-2007

I have a column of numbers between 0 - 6 digits long. For those less than 6 I need to pad out with zeros to ensure they are all 6 digits i.e 12563 = 012563 or 23 000023 etc etc. Can someone recommend a solution?

Probably the easiest way to pad numbers with leading zeros would be to use the Format() function, as in
Format(fieldName, "000000")

If you're searching on this (like for PIN numbers, where '12' would be represented as '000012' here's an example using Gord's correct answer;
SELECT CStr(Format(fieldName,"000000")) FROM table WHERE CStr(Format(fieldName,"000000"))="000012";

I had a similar issue. I couldn't change the field on the actual file because it was a split database and it had to be changed on the data source (Database_be). I went to the data source and made the change from Number to Short Text to all tables and that was it... Like magic!!

Try:
Update TABLE set DIGITS = string(6- len(DIGITS),"0")
DIGITS TABLE is the table where your numbers are stored.
DIGITS is the field that contains your numbers.

The above does NOT work.
Corrected version:
Update TABLE set DIGITS = string(6- len(DIGITS),"0")&DIGITS
The number '6' can be altered for whatever the total length of your field.

Related

Select column ignore beginning numbers

I have a column that I need to select but it has an inconsistent amount of numbers/formatting in the beginning
The column values are ideally supposed to be structured like:
# Question_-_Answer
But here are some examples which make it hard to remove the numbers in the beginning
0 Question1_-_50-60
1.Question_-_apple
12Question_-_40/50
13 Question_-_orange
14.Question_-_apple
15. Question_-_orange2
Is there a way I can query this column so that it ignores everything until the first alphabetical character while also not removing any characters/alphanumerical values in the question and answer portion?
You can use PATINDEX and STUFF to achieve this:
SELECT STUFF(V.YourString,1,PATINDEX('%[A-z]%',V.YourString)-1,'')
FROM (VALUES('0 Question1_-_50-60'),
('1.Question_-_apple'),
('12Question_-_40/50'),
('13 Question_-_orange'),
('14.Question_-_apple'),
('15. Question_-_orange2'))V(YourString);
This removes all characters up to the first alpha character.

Filtering records based on last 4/5 characters of VARCHAR

I have a VARCHAR column that contains an integer followed by a forward slash and a four digit integer.
Example Data:
82/2015
126/2017
5763/2017
I'm trying to retrieve only the records that end in 2017.
126/2017
5763/2017
I'm assuming I need to use the forward slash to split the string and then perform a comparison? Or maybe I could simply perform a comparison against the last 4/5 characters? Does SQL contain a function that can do either of these for me?
Use like:
where col like '%/2017'
You can use RIGHT to do this.
SELECT *
FROM YourTable
WHERE RIGHT(SomeDate, 4) = '2017'

SQL to_char() printing 2 digit number as 0xx and not touching 3 number digit

I'm currently battling with something
that must be trivial for you.
I have 2 number 191 and 97, and I need to put them in a SQL request, as chars and 97 must be printed as 097.
At first I tried 999, but it added 2 space to my numbers.
then 099, it does print 097 but it adds a space to it.
to_char(:center, '099') = " 197" and " 097"
Where is this space coming from?
Thanks.
What you're looking for is the Format Modifier element:
to_char(:center, 'fm099')
The leading space is for the potential minus sign. To remove it you can use FM in the format:
to_char(v_num,'FM099')
9 9999 Returns value with the specified number of digits with a leading space if positive or with a leading minus if negative.Leading zeros are blank, except for a zero value, which returns a zero for the integer part of the fixed-point number.
From http://docs.oracle.com/cd/B19306_01/server.102/b14200/sql_elements004.htm#i34510
Use #DavidAldridge solution, or trim your value.
If you are looking for the all column values in same number of digits even the actual value having less digits. Try this
a) SELECT TO_CHAR(COLUMN_NAME, 'FM099') FROM TABLE_NAME;
b) SELECT TO_CHAR(COLUMN_NAME, 'FM000') FROM TABLE_NAME;
Both is working fine. but don't know which one would be the best choice.

TOAD SQL - LPAD Number, but also add decimals?

Basically what I'm trying to do is add '000' to a number (between 5-8 characters in length) and make the whole numbers have decimals.
What I came up with is:
SELECT DISTINCT
'000' || TO_CHAR(Blah, '9,999,999.99') AS "Data"
FROM Blah database
While this does what I ideally want, there is a gap between the zeroes of either 3 or 4 depending on the number. Obviously I don't want the gap there. Where am I going astray?
Use trim(to_char(x, '9,999,999.99')) this way you will avoid gap

sql query for alphanumeric ID in hex

I want to be able to differentiate between a string that is alphnumeric and a string that is in hex format.
My current query is:
<columnName> LIKE '?_____=' + REPLICATE('[0-9A-Fa-f]',16)
I found this method of searching for hex ID's online and I thought it was working. However after getting a significantly larger sample size I can see a high false positive rate in my results. The problem is that this gives me all the results I do want but it also gives me a bunch of results I dont care about. For example:
I want to see:
<url>.php?mains=d7ad916d1c0396ff
but i dont want to see:
<url>.php?mblID=2007012422060265
The difference between the 2 strings is that the 16 characters at the end that i want to collect are all numeric and not a hex ID. What are some ways you guys use to limit the results to hex ID only? Thanks in advnace.
UPDATE:
Juergen brought up a good point, the second number could be a hex value to. Not all hex numbers contain [a-F]. I would like to rephrase the question to state that I am looking for an ID with both letters and numbers in it, not just numbers.
The simplest way is just to add a separate clause for that restriction:
<columnName> LIKE '?_____=' + REPLICATE('[0-9A-Fa-f]',16)
AND <columnName> NOT LIKE '?_____=' + REPLICATE('[0-9]',16)
It should be fairly simple to determine if a string contains only numbers...
Setting up a test table:
CREATE TABLE #Temp (Data char(32) not null)
INSERT #Temp
values ('<url>.php?mains=d7ad916d1c0396ff')
,('<url>.php?mblID=2007012422060265 ')
Write a query:
SELECT
right(Data, 16) StringToCheck
,isnumeric(right(Data, 16)) IsNumeric
from #Temp
Get results:
StringToCheck IsNumeric
d7ad916d1c0396ff 0
2007012422060265 1
So, if the IsNumeric function returns 0, it could be a hex string.
This makes several assumptions:
The rightmost 16 characters are what you want to check
You only ever hit 16 characters. I don't know when the string would get too long to check.
A non-numeric character means hex. Any chance of "Q" or "~" being embedded in the string?