Programmatically process numbers into word form - numbers

This might be a stupid question and a little embarrassing for me to ask but I really wanna know how I could have done this if I knew what to do that time.
So back in college when I was applying at a company for my internship, they conducted a test through a technical examination. The task was to write a code of any choice of programming language or just a pseudo code for processing inputted integer numbers from 0 up to 9999 and provide its output in worded form.(Ex. 4130 = Four thousand one hundred thirty)
Right now that I am already working I still think about this as I still do not know it. Thank you in advance

I would create a map of pairs with an integer and a string that converts words, such as
(1, 'one'), (2, 'two'), (3, 'three'), (4, 'four')
, (5, 'five'), (6, 'six'), (7, 'seven'), (8, 'eight'), (9, 'nine'), (10, 'ten'), (11, 'eleven')
, (12, 'twelve'), (20, 'twenty'), (30, 'thirty'), (40, 'forty'), (50, 'fifty'), (60, 'sixty'), (70, 'seventy'), (80, 'eighty'), (90, 'ninety')
, (100, 'hundred'), (1000, 'thousand')
I would then parse out the numbers as characters and multiply it based on their place.
e.g. 4130 = value of 4 == 'four', with a place of [1][0][0][0] = 1000, concatenate 'thousand' based on that lookup, and so on.
Once you know the exceptions you can create the rest of them easily. This would scale up to 999,999.

Related

Create `has_($value)`column for table values

I would like to perform an operation similar to a dynamic pivot table. I utilize Postgresql as database framework. The table t has a column with values 10, 20, and 30. I wish to create n columns, in this case equal to 3, to allow the boolean assignment has_($value) equal to 1 if existent in respective group, or 0 if not. I tried to understand tablefunc and crosstab without success.
CREATE TABLE IF NOT EXISTS t (
id INTEGER NOT NULL,
value INT NOT NULL
)
INSERT INTO t (id, value) VALUES
(1, 10),
(1, 20),
(2, 10),
(3, 30),
(3, 20)

How to format date in SSRS?

In SSRS report query is generating date as a column in the format of :
Sales ID20200331 ID20200430 ID20200531
To remove the ID i used following expression:
=Right( Fields!ID20210331.Value, len(Fields!ID20210331.Value) - 2)
This gives me 84, instead of removing ID.
How can I remove ID and format date as 2020 Mar etc.
Thanks
If your fields values are "ID20200430" etc then in SSRS you can use something like this..
=DateSerial(
MID(Fields!IDDate.Value, 3, 4),
MID(Fields!IDDate.Value, 7, 2),
RIGHT(Fields!IDDate.Value, 2)
)
However It appears that it's your column [names] that represent dates is this correct?
If this is true, then you would have to UNPIVOT the columns in SQL then convert the resulting values into a real date format.
Here' some sample data to show how to do this.
DECLARE #t TABLE (Sales varchar(10), ID20200331 int, ID20200430 int, ID20200531 int)
INSERT INTO #t VALUES
('A', 1,2,3),
('B', 4,5,6),
('C', 7,8,9)
SELECT
Sales, IdDate, SomeNumber
, MyDate = DATEFROMPARTS(SUBSTRING(IdDate, 3, 4), SUBSTRING(IdDate, 7, 2), SUBSTRING(IdDate, 9, 2))
FROM #t
UNPIVOT(
SomeNumber FOR IdDate IN ([ID20200331],[ID20200430],[ID20200531])
) unpvt
Which gives us this including the myDate column which is the correct date type
You could then use this in a matrix control in SSRS to get the data back into a pivoted view

How to Order By a column but array come first

(Sorry for my bad English)
I have an array of users ids as below:
[5, 9, 3, 22, 16]
Obviously the values are dynamic.
Now, I need to SELECT all users but users with above ids come first.
What I've tried so far?
This query gives me exact answer :
SELECT * FROM users WHERE id IN (5, 9, 3, 22, 16)
UNION ALL
SELECT * FROM users WHERE id NOT IN (5, 9, 3, 22, 16);
But is there any better way?
P.S:
I'm using PostgreSQL 10.
Try this:
select *
from users
order by id not in (5, 9, 3, 22, 16), id
As stated in the documentation, an expression used in the ORDER BY clause
can be an arbitrary expression formed from input-column values.
In particular, you can use a Boolean expression, as values of this type are sortable. Note that false < true.
You can use CASE statements on your ORDER BY, I'll give you an example:
select *
from users
order by
CASE WHEN id=5 THEN 1
WHEN id=9 THEN 2
WHEN id=3 THEN 3
WHEN id=22 THEN 4
WHEN id=16 THEN 5
END, id
With CASE you can tell postgres the "priority" or value you want for each id if you know them beforehand. After that I added "id" so the rest of the rows gets ordered properly.

Bulk insert and update in one query sqlite

is there any way to insert and update bulk data in same query. I have seen many likes but not getting solution. I get a code but its not working
INSERT INTO `demo1` (`id`,`uname`,`address`)
VALUES (1, 2, 3),
VALUES (6, 5, 4),
VALUES (7, 8, 9)
ON DUPLICATE KEY UPDATE `id` = VALUES(32), `uname` = VALUES (b),`address` = VALUES(c)
Can any one help me.
SQLite has the REPLACE statement (which is an alias for INSERT OR REPLACE), but this just deletes the old row if a duplicate key is found.
If you want to keep data from the old row(s), you must use two statements for each row:
db.execute("UPDATE demo1 SET ... WHERE id = 1")
if db.affected_rows == 0:
db.execute("INSERT ...")

to find even odd and plain series in postgresql

this might be naive but I want to know is there any way possible to find rows which have even entries and which have odd.
I have data in this format
"41,43,45,49,35,39,47,37"
"12,14,18,16,20,24,22,10"
"1,2,3,4,5,6"
"1,7,521,65,32"
what I have tried is to split these values with an Id column and then reading them with Even and Odd functions but it is too time taking.
is there a query or a function through I can find out that which of the rows are even, odd sequence and arbitrary?
thanks in advance.
Assuming your table has some way of (uniquely) identifying each of those lists, this should work:
create table data (id integer, list text);
insert into data
values
(1, '41,43,45,49,35,39,47,37'),
(2, '12,14,18,16,20,24,22,10'),
(3, '1,2,3,4,5,6'),
(4, '1,7,521,65,32');
with normalized as (
select id, unnest(string_to_array(list,',')::int[]) as val
from data
)
select id,
bool_and((val % 2) = 0) as all_even,
bool_and((val % 2) <> 0) as all_odd
from normalized
group by id;
Not sure if that is fast enough for your needs