Taking Sql this quarter and not having any luck with the following question:
The height of players in feet (inches/12). Include their name. Hint: Calculation or derived field. Be sure to give the calculated field a column header.
We're learning the basic Select statment and didn't find any reference on how to make custom data at w3schools. I'm using Microsoft SQL server Management Express Here's my statment so far:
select nameLast, nameFirst, height
from Master
where height (I assume its something like 'Player_Height' = height/12)
order by nameLast, nameFirst, height
Thanks for the help
If you need that result a lot, you might want to consider putting that into its own, computed column and persist it into your table. That way, it'll be up to date at all times, and you won't have to compute and re-compute it over and over and over again:
ALTER TABLE dbo.YourTable
ADD HeightInFeet AS ROUND(Height / 12.0, 3) PERSISTED
With this, you can also round your result to e.g. 3 digits after the decimal point (or pick whatever makes sense to you). Each row in your table will now have a new column called HeightInFeet, always computed and always up to date, and you can query on it, you can return it in a SELECT - whatever you like to do! (you just can set a new value to it, since it's being computed by SQL Server itself, based on your Height column)
Try something like
select nameLast, nameFirst, (height/12) as heightInInches
from Master
order by nameLast, nameFirst, height
Related
I'm writing a dynamic sql pivot table query with a dataset. But when a nvarchar column value is used on the pivot, when the column value is larger than 128 it gives below error
The identifier that starts with <> is too long. Maximum length is 128.
Any workaround which can resolve this?
I'm not sure exactly how your tables look, some sample code and more detail would be good.
But, just based on what you have here, you might try changing your "nvarchar" column in the original table to "float".
Like so;
ALTER TABLE YourTableName
ALTER COLUMN YourColumnName FLOAT
I am using this method to pivot two values in a "VIEW" that are being used for a pie chart in a C# Windows Application.
Hope this helps.
I have a column PRICESTABLE.PRICE with values like the following
12.356898
13.587988
I need to make an update to PRICETABLE to update the prices like these:
12.350000
13.580000
Notice I don't want to ALTER the column, only round the values, thanks!
The following would work for MySQL, SQL Server, and Postgres:
UPDATE PRICESTABLE
SET PRICE = ROUND(PRICE, 2);
You might want to add a WHERE clause to the above update, unless you really want to apply it to the entire table. Also, an alternative here would be to keep all the original precision, and just call ROUND when you need to present the data.
the following function deletes all blanks in a text or varchar column and returns the modified text/varchar as an int:
select condense_and_change_to_int(number_as_text_column) from mytable;
This exact query does work.
Though my goal is to apply this function to all rows of a column in order to consistently change its values. How would I do this? Is it possible with the UPDATE-clause, or do i need to do this within a function itself? I tried the following:
UPDATE mytable
SET column_to_be_modiefied = condense_and_change_to_int(column_to_be_modiefied);
Basically i wanted to input the value of the current row, modify it and save it to the column permanantly.
I'd welcome all ideas regarding how to solve scenarios like these. I'm working with postgresql (but welcome also more general solutions).
Is it possible with an update? Well, yes and sort-of.
From your description, the input to the function is a string of some sort. The output is a number. In general, numbers should be assigned to columns with a number type. The assumption is that the column in question is a number.
However, your update should work. The result will be a string representation of the number.
After running the update, you can change the column type, with something like:
alter table mytable alter column column_to_be_modiefied int;
I have a query in MS Access which creates a table from two subqueries. For two of the columns being created, I'm dividing one column from the first subquery into a column from the second subquery.
The datatype of the first column is a double; the datatype of the second column is decimal, with scale of 2, but I want the second column to be a double as well.
Is there a way to force the datatype when creating a table through a standard make-table Access query?
One way to do it is to explicitly create the table before putting anything into it.
Your current statement is probably like this:
SELECT Persons.LastName,Orders.OrderNo
INTO Persons_Order_Backup
FROM Persons
INNER JOIN Orders
ON Persons.P_Id=Orders.P_Id
WHERE FirstName = 'Alistair'
But you can also do this:
----Create NewTable
CREATE TABLE NewTable(FirstName VARCHAR(100), LastName VARCHAR(100), Total DOUBLE)
----INSERT INTO NewTableusing SELECT
INSERT INTO NewTable(FirstName, LastName, Total)
SELECT FirstName, LastName,
FROM Person p
INNER JOIN Orders o
ON p.P_Id = o.P_Id
WHERE p.FirstName = 'Alistair'
This way you have total control over the column types. You can always drop the table later if you need to recreate it.
You can use the cast to FLOAT function CDBL() but, somewhat bizarrely, the Access Database Engine cannot handle the NULL value, so you must handle this yourself e.g.
SELECT first_column,
IIF(second_column IS NULL, NULL, CDBL(second_column))
AS second_column_as_float
INTO Table666
FROM MyTest;
...but you're going to need to ALTER TABLE to add your keys, constraints, etc. Better to simply CREATE TABLE first then use INSERT INTO..SELECT to populate it.
You can use CDbl around the columns.
An easy way to do this is to create an empty table with the correct field types and then to an Append-To query and Access will automatically convert the data to the destination field.
I had a similar situation, but I had a make-table query creating a field with NUMERIC datatype that I wanted to be short text.
What I did (and I got the idea from Stack) is to create the table with the field in question as Short Text, and at the same time build a delete query to scrub the records. I think it's funny that a DELETE query in access doesn't delete the table, just the records in it - I guess you have to use a DROP TABLE function for that, to purge a table...
Then, I converted my make-table query to an APPEND query, which I'd never done before... and I just added the running of the DELETE query to my process.
Thank you, Stack Overflow !
Steve
I add a '& ""' to the field I want to make sure are stored as text, and a ' *1 ' (as in multiplying the amount by 1) to the fields I want to store as numeric.
Seems to do the trick.
To get an Access query to create a table with three numeric output fields from input numeric fields, (it kept wanting to make the output fields text fields), had to combine several of the above suggestions. Pre-establish an empty output table with pre-defined output fields as integer, double and double. In the append query itself, multiply the numeric fields by one. It worked. Finally.
I need some help with a SQL query...
I have a SQL table that holds in a column details of a form that has been submitted. I need to get a part of the text that is stored in that column and put it into a different column on the same row. The bit of text that I need to copy is always in the same position in the column.
Any help would be appreciated guys... my mind has gone blank :">
UPDATE mytable
SET other_column = SUBSTRING(column, begin_position, length)
You may just want to use a computed column. This way if the source string changes, your computed column is still correct. If you need to seek to this substring then you might want a persisted computed column if your db supports it.
UPDATE table
SET Column2 = SUBSTRING(Column1, startPos, length)
What if the value you wanted to copy was in a different position in each record, but always followed the same text?