How to write a column name with dot (".") in the SELECT clause? - sql

I'm trying to write a column name using "." with no success
sample:
SELECT PrmTable.Value = MAX(Value)
FROM TempTable
or
SELECT MAX(Value) AS PrmTable.Value
FROM TempTable
Any idea ?

Just enclose it in square brackets, and it will work
e.g.
SELECT MAX(Value) AS [PrmTable.Value]
FROM TempTable

I would not recommend you use field names which always require you to enclose the name in brackets, it becomes a pain.
Also the period is used in SQL Server to denote schema and database name separators. Using your field name the full name for a field becomes:
[DatabaseName].[SchemaName].[TableName].[FieldName.WithPeriod]
That just looks odd and would probably confuse other DBAs. Use an underscore to separate words in your field names, it's a much more common style:
[DatabaseName].[SchemaName].[TableName].[FieldName_WithUnderscore]

SELECT [PrmTable.Value] = MAX(Value)
FROM TempTable
or
SELECT MAX(Value) AS [PrmTable.Value]

Related

Converting db2 column names in select to be lowercase in json file

I currently have a very simple select that my code then dumps into JSON
SELECT user, phone
FROM table t;
But the select returns all uppercase column names, resulting in uppercase JSON keys, which I don't want. Is there a way in DB2 to return lowercase column names?
If you want to get lowercase columns names (not data) in DB2, you must use double quotes around the column names.
SELECT user as "user", phone as "phone"
FROM table t;
If you want the result of the query to be small, try this
SELECT LOWER(user), LOWER(phone)
FROM table t;
Use the LOWER() function;
SELECT LOWER(user) AS 'user', LOWER(phone) AS 'phone'
FROM table t;

Is it possible to avoid specifying a column list in a SQL Server CTE?

Is it possible to avoid specifying a column list in a SQL Server CTE?
I'd like to create a CTE from a table that has many columns so that the structure is identical. There probably is a way to accomplish this without relisting every column name.
I've tried (unsuccessfully):
with pay_cte as
(select * from payments)
select * from pay_cte
I'm encouraged in my quest by this statement in the msdn documentation:
The list of column names is optional only if distinct names for all resulting columns are supplied in the query definition.
https://msdn.microsoft.com/en-us/library/ms175972.aspx
Yes, assuming you mean that you don't have to name every column in the with cte(Col1, Col2) as section.
You can easily try this yourself with a very simple test query along the lines of:
with cte as
(
select *
from sys.tables
)
select *
from cte

Is there any way to select column form table that has name as keyword from oracledb?

There is a column LEVEL in table. Is there any way to select it? Like select LEVEL from table_name.
Just put double quotes around the name:
SELECT MyTable."LEVEL" FROM MyTable;
If it's not too late in the game I'd recommend just changing the column name, but I know that's not always possible.
Also note that double quotes make the name case sensitive, so if you have a problem with MyTable."LEVEL" (all uppercase column name) you can find out the exact case of the column name like this:
SELECT Column_Name
FROM User_Tab_Columns
WHERE Table_Name = 'MYTABLE' AND UPPER(Column_Name) = 'LEVEL';
Use double quotes "LEVEL" to escape the column. This should fix your issue.

What is the difference in [ ] and ( ) in SQL?

I've seen both used but I can't seem to understand when to use each?
To me is seems like you enter the name of the table you are referring from in the ( ) and the field name in the [ ]?
Could anyone explain?
The square brackets are used In Microsoft products to specify that what's within them is an identifier (the standard quoted identifiers are double quotes " ", which Microsoft SQL Sever also supports). This is used when you have a database name, user name, table name, field name, view name, procedure name (et.c.) that happens to be the same as a keyword, or contains characters that would break the syntax. This is often used in generated code to safeguard against identifiers that can't otherwise be used in the code. A generated query could look like this:
select [Id], [Name], [Password hint]
from [dbo].[MyDataBase].[User]
Here the field name Password hint would break the syntax if used without brackets, and the table name User could conflict with the keyword User.
Parentheses are used to group items, for example as part of the syntax of some clauses, for example an insert:
insert into someTable (field1, field2) values ('value1', 'value2')
They can also be used in expressions:
select Price * (Quantity + FreeItems) from Articles
They can also be used around queries to make subqueries:
select o.Name
from (select Name, Age from Persons where City = 'Oslo') as o
where o.Age > 18
() are used for passing parameters to functions and stored proceedures etc. [] are used to encapsulate field name (etc.) which include punctuation (spaces and special characters as per the comment above). [] are useful sometimes to name fields for display
SELECT FFgg AS [Some field discription] FROM table1;
Hope this helps.

How make for order by name in MYSQL as with order name in Window

table
Picture with rows have name
1.jpg,2.jpg,3.jpg,4.jpg,5.jpg,6.jpg,7.jpg,8.jpg,9.jpg,10.jpg,11.jpg
select * from Picture order by name
mysql order : 1.jpg,10.jpg,11.jpg,2.jpg,3.jpg,......
Issue:
I want it sort all type name like as with Window 1.jpg,2.jpg,3.jpg,4.jpg,5.jpg,6.jpg,7.jpg,8.jpg,9.jpg,10.jpg,11.jpg
and it must working with other case as
flower01.jpg,flower02.jpg,flower031.jpg,....,flower10.jpg
please help me
You basically have 4 choices.
Change name to a numeric type, and remove the .jpg (adding it later in code)
Add an 'order' column and sort by that
Sort it in your code, not in SQL
Use of the of the cast 'hacks' (e.g. CAST(NAME AS UNSIGNED)
A hackish way to do it:
... ORDER BY CAST(name AS UNSIGNED);
Edit: an alternative would be:
... ORDER BY LPAD(name,N,'0');
Where N is the maximum width of your column.
Sadly, MySQL doesn't support natural sorting natively AFAIK.
ODER BY name sees that name is a string and sorts it accordingly,charater by character. You have to make MySQL interpret the name as numeric value. A way might be something like this:
select * from Picture order by name * 1;