Write a function unpack() which takes a 32-bit integer as input and decodes it to find age, occupation and year of birth - structure

Bitfields: Write a function pack(), that encodes the following data into a 32-bit integer: age (0-100), occupation (engineer, doctor, student, other), year of birth (1900 - 2016). Write a function unpack() which takes a 32-bit integer as input and decodes it to find age, occupation and year of birth. Do this using both bitwise operators and bitfields. Try the combination of packing using bit-fields and unpacking using bitwise operators and vice versa.
Output: Inverted number is: 207

Related

Out putting birth years with TO_CHAR(M.DOB, 'YYYY')

I have a database with a store of members and dates of birth.
I am to "List name and year of birth of all members in alphabetical order by family name and given name with "DHDSJHDSDH" as parent
I currently have Current Code
Everything works apart from the birthdays as for someone with a birthday in 1993 it outputs 2093.
It would certainly help if you had code or table structure posted but I will take a stab. I would imagine you want something along the lines of:
SELECT FamilyName,
GivenName,
CONVERT(varchar, yearOfBirth, 101)
FROM schema.TableYouUse
ORDER BY FamilyName;
Like I said, this may not be what you are asking at all. What I did was query up the first name, last name, and birth date of the person in the table. I used CONVERT to make the birth date column varchar instead of DATETIME that way we can format it the way we like.
Notice that CONVERT takes parameters, the first param is target expression type, the second is the target date, and lastly we use 101 to format it the way you need. Here is a link to the convert docs for more styles you can use https://learn.microsoft.com/en-us/sql/t-sql/functions/cast-and-convert-transact-sql?view=sql-server-ver15
Lastly, to address the crazy 2093 instead of 1993 that sounds like bad data entry to me.

What differences are between the different ways of specifying `datapart` arguments?

From https://learn.microsoft.com/en-us/sql/t-sql/functions/datepart-transact-sql
The following table lists all datepart arguments with corresponding return values for the statement SELECT DATEPART(datepart,'2007-10-30 12:15:32.1234567 +05:10'). The data type of the date argument is datetimeoffset(7). The nanoseconddatepart return value has a scale of 9 (.123456700) and the last two positions are always 00.
datepart Return value
year, yyyy, yy 2007
quarter, qq, q 4
month, mm, m 10
I wonder what differences are
between year, yyyy, and yy?
between quarter, qq, and q?
between month, mm, and m?
Thanks.
In some cases, the datepart() argument is an English word. The word is unambiguous and clear, both to the person who writes the code and to anyone else who reads it.
In some cases, the datepart() argument is a cryptic collection of one or more letters that bears some relationship to the date part being requested -- however, the relationship is often ambiguous.
Although for a given part, they do the same thing, you can decide which is better to use.

Getting the oldest record

How can I write a SQL Query to get the oldest male age in number format, not in the dob format?
name dob date job sex language prof salary
-------------------------------------------------------------------------
mitesh 1981-01-01 2001-01-01 m java architect 3100.00
ankur 1982-02-02 2001-02-02 m ruby scientist 3200.00
dhruv 1983-03-03 2001-03-03 m csharp designer 3300.00
ruchi 1981-01-01 2002-01-01 f php teacher 4000.00
You could do something like:
Select top (1) Name, dob, datediff(YY,[dob],getdate()) as Age
from dbo.YourTableName
where sex = 'm'
order by Age Desc
Which would work in SSMS
This MySQL query selects the oldest male and converts the 'dbo' format to an age
SELECT MAX(TIMESTAMPDIFF(dob, '1970-02-01', CURDATE())) AS age
FROM dbo.data
WHERE sex='m'
age function
Some database such as Postgres offer an age function to calculate the span of time between a pair of timestamps. Passing a single timestamp means the current date-time will be used as the second of the pair.
Time Zone
You may care about time zone if you want a precise age.
For example, a new days dawns earlier in Paris than in Montréal. So if running SQL code around midnight, you will get a different result if the code runs on a server with a different current default time zone.
If you care about this level of accuracy, specify the second date in the pair. Use a function that takes a time zone to determine today’s date.
String as Date-Time Type
Ideally you should be storing date-time values as date-time types. But I'm guessing that in this Question your date is actually a textual value.
Alphabetical Order = Chronological Order
If that date-of-birth column is a text value in SQL format, parallel to ISO 8601 format, then its alphabetical ordering is also a chronological ordering. So we can directly use such ordering to find the oldest value.
LIMIT To Get First Row
The LIMIT command truncates the result set to the first x number of rows. Sorting by the date of birth in ascending order means we will get the first row only.
Example Code
Tip: Naming columns and other objects in SQL with a trailing underscore avoids absolutely any collision with keywords/reserved words. So promises the SQL spec.
SELECT name_ , date_of_birth_ , age( timestamp date_of_birth_ ) AS age_
WHERE sex_ = 'm'
ORDER BY date_of_birth_ ASC
LIMIT 1
;

Get client age in MDX over time period

I need to calculate average client age across population. I have an enrollment record per client per month with age dimension key. So, to calculate an average age for one month is not a problem, but when I need to do it for a time period of a year that's where I flounder. How can I write a calculated measure to get age of only the first record per client in the time range?
Thanks everyone for the suggestions.
Michael
I would make age a measure with aggregation function "FirstChild". This means that for the time dimension, the first child is taken on each hierarchy level, and for all other dimensions, the age is summed. Hence, this age should be made invisible, and only the average age measure calculated from it by dividing by the number of clients should be made visible.
For this to work properly, you need to set the "Type" property of your time dimension to "Time", and it requires the Enterprise or Development edition of SQL Server. The "FirstChild" aggregation function is not supported in standard edition.

how to display number value in words

Q. Display the number value in Words and output should look like this
SAL In_Words
--------- -----------------------------------------------------
800 eight hundred
1600 one thousand six hundred
1250 one thousand two hundred fifty
And, I'm still didn't figure out, how this query is the solution for the above output.
select sal, to_char(to_date(sal,'j'),'Jsp') in_words from emp
What to_date is doing here ? Anyone have any idea about this query ?
So how the query works? Well here’s why:
select to_char(to_date(:number,'j'),'jsp') from dual;
If you look into the inner most part of the query to_date(:number,'j') the ‘j’ or J is the Julian Date (January 1, 4713 BC), basically this date is been used for astronomical studies.
So to_date(:number,'j') it take the number represented by number and pretend it is a julian date, convert into a date.
If you pass 3 to number, so it will convert date to 3rd Jan 4713 BC, it means 3 is added to the Julian date.
select to_char(to_date(3,'j'),'jsp') from dual;
Now to_char(to_date(3,'j'),'jsp'), jsp = Now; take that date(to_date(3,'j')) and spell the julian number it represents, the output is:
TO_CH
-----
three
There is a limitation while using Julian dates ,It ranges from 1 to 5373484. That’s why if you put the values after 5373484, it will throw you an error as shown below:
ORA-01854: julian date must be between 1 and 5373484
Hi everyone, it is interesting this topic. I remember when I was learning Oracle in 2005 one of the instructor required me to write a PL/SQL code to convert numbers in words, it was a whole two pages code to reach this.
Here is some reference that could help us to understand the Julian day, that is why we use the letter 'j' or 'J' during this operation.
First there is a website that has the example and explanation about "How To Convert Number Into Words Using Oracle SQL Query":
http://viralpatel.net/blogs/convert-number-into-words-oracle-sql-query/
Second if you want to know more about "Julian day" go to:
http://en.wikipedia.org/wiki/Julian_day
Third if you want to know more about who proposed the Julian day number in 1583, it was by "Joseph Scaliger":
http://en.wikipedia.org/wiki/Joseph_Justus_Scaliger
It is not make sence for me continue repiting what another author in these websites have made, that is why I just posted the link you can access them and read what you need to understand how query like this works:
SELECT TO_CHAR (TO_DATE (2447834, 'j'), 'jsp') FROM DUAL;
//Output: two million four hundred forty-seven thousand eight hundred thirty-four
I've never heard of a DBMS with a built-in function to do as you ask. You'll need a table of number-names, to join to that once per digit using modulo arithmetic, and string concatenation to produce one In_Words column. Plus some logic to eliminate leading zeros. It will take time to write.
J stands for Julian day - the number of days since January 1, 4712 BC. The numbers in your table converted to Julian date. JSP spells out the date:
SELECT to_char(SYSDATE,'JSP') AS number_of_days_sinse_4712_BC
FROM dual
/
to convert decimal number into words, you can follow below code
SELECT TO_CHAR(to_date(TRUNC(num),'J'),'Jsp')
||' and '
|| TO_CHAR(to_date(to_number(SUBSTR(num-TRUNC(num),instr(num-TRUNC(num),'.')+1)),'J'),'Jsp') Indicator
FROM
(SELECT &enter_numbr num FROM dual
);
Hope this will help!!!