Convert query letters to uppercase using Management Studio - ssms

Can I convert query syntax to uppercase using SQL Server Management Studio? I have a bunch of queries that are all written in lowercase letters, and I want to convert them all to uppercase letters. I'm looking for a built-in editor functionality.

Select all of your query. Then go to Edit > Advanced > Make Uppercase or simply hit Ctrl+Shift+U to make the query uppercase.
Please note that it will make everything uppercase. (i.e., table names, fields name, strings etc.)
EDIT:
If you are able to install add-ins, you can also use SSMS Tools Packs to uppercase only the keywords.

Related

Is CARD a keyword in SQL? or: List of all SQL keywords

I'm adding a table to my database - CREATE TABLE dbo.Card - and SSMS is highlighting the word Card. I'm searching the internet trying to find out what that word means to SQL and if it is a keyword or not. I don't see it anywhere on Microsoft's list of SQL Reserved Words.
The main reason I care, aside from the highlighting bothering me, is that I want to avoid using any reserved words or keywords as schema/table/column/etc... names. When I absolutely have to - existing databases - I like to use square brackets to make things explicit.
Is Card a reserved word or keyword in SQL, or for any other reason unsafe to use as an identifier?
If it is safe to use, can anyone explain why SSMS is highlighting it?
If it is not 100% safe to use as a table name I'll most likely choose a different name.
Windows 8.1
SSMS v17.7
Red Gate - Up to date, including SQL Prompt 9.4.9
SQL Server 2017 (14.0)
Database Compatibility Level 2017 (140)
UPDATE
It looks like the main consensus is:
It is not a reserved or keyword
SSMS highlights it because reasons... It's probably used somewhere by MS SQL Server or SSMS
At this point I'm just terribly curious, but at least I know there's no need to worry. Thanks everyone for your answers.
No, card is not a reversed word in Microsoft SQLServer or SQL ANSI standard.
Card is definitely not a reserved keyword in SQL Server
You can check the complete list of reserved words Reserved Keywords (Transact-SQL)
SSMS highlights it because reasons... It's probably used somewhere by
MS SQL Server or SSMS
They are various keywords that have been subjectively chosen by Microsoft and placed into a list within the query editor syntax coloring file.
I suggest it's always good to use Delimited Identifiers ([] or ") when specifying table name and column names.
For example, the script you are using should be written as
CREATE TABLE [dbo].[Card](<Columns>)

MS Access Query -LIKE x0x6

im using MS Access as DB and the Field im searching is a Short Text. would like to know how i could search a field and match it with wildcards. For example('x' is wildcard) -
x0x6 (matches 1016, 1076.....)
x06x (matches 9067, 3068....)
.
.
.
have search online and found sample using this - % ? #, but none works for me :/
here is my sql query -
SELECT COUNT(DPlace) as CounTotal
FROM tblTest2
WHERE DSide=1 AND
DNumber LIKE '%06%' AND DDate BETWEEN #09-13-16# AND #09-13-17#
SELECT COUNT(DPlace) as CounTotal
FROM tblTest2
WHERE DSide=1 AND
DNumber LIKE '%0%6' AND DDate BETWEEN #09-13-16# AND #09-13-17#
Both the sql above returns wrong result as they matches all field that has '06'. Please advice. Thank you.
Here is an explanation of why [0-9] works and ? and # does not in your LIKE pattern. TL;DR - You are using the Access backend which runs ANSI-92 mode.
Background
MS Access is a unique type of software in that it is two-fold:
FRONT-END: A GUI .exe program and part of the MS Office Suite (with Word/Excel/PowerPoint) only for Windows installs. This GUI program can serve as a "front-end" to connect to any ODBC/OLEDB compliant "back-end" RDBMS database such as SQL Server, Postgres, MySQL, SQLite, etc.
BACK-END: By default, Access GUI connects to the Windows' JET/ACE SQL Engine (Windows .dll files), a file-system data store, which over time has been considered the "Access database" but any Windows program or script can connect to it without MSAccess.exe installed. Likewise, any application layer language (VB, C#, PHP, Python, Java) with appropriate ODBC/OLEDB libraries can connect to this backend. In fact, when using the GUI, VBA is connected by default! So really Access is a GUI console to database (#1 definition).
OP as commented is using VB6 (application code) to connect to the JET/ACE engine as a backend database, no where using the GUI program.
LIKE Operator
The above dichotomy is particularly relevant concerning the LIKE operator:
Using the GUI version of Access, its SQL dialect tends to be ANSI-plus and can interface with VBA. Hence, the LIKE operator behaves very similar to VBA's LIKE operator. Also, the LIKE operator runs in ANSI-89 mode which uses the asterisk * and not % wildcard for LIKE as well as ? and #. Even so, the GUI's LIKE can use percentage symbol with the ALIKE operator or setting the database to ANSI-92 mode (and not the default ANSI-89).
Using the backend database of Access as OP does, the SQL dialect uses ANSI-92 mode which MS docs show include:
%: Matches any number of characters. It can be used as the first or last character in the character string.
_: Matches any single character within the brackets.
[]: Matches any single alphabetic character.
-: Matches any one of a range of characters. You must specify the range in ascending order (A to Z, not Z to A).
Notice unlike the ANSI-89 mode, the symbols ? and # are not included. But the last symbol, the hyphen, matches a range of characters such as digits, [0-9]. The same below SQL statement if run inside the MSAccess.exe will not return same results unless you run in ANSI-92 mode.
SELECT COUNT(DPlace) as CountTotal
FROM tblTest2
WHERE DSide=1
AND DNumber LIKE '[0-9]0[0-9]6'
AND DDate BETWEEN #09-13-16# AND #09-13-17#
MS Access uses different like patterns from other databases. These are well-documented.
From what you describe, you want either:
DNumber LIKE '?06?'
where ? can be any character.
or:
DNumber LIKE '#06#'
where # is specifically a digit.
You can also write the latter as:
DNumber LIKE '[0-9]06[0-9]'
and this will work in SQL Server as well.
In any other database, the placeholder for any character is _:
DNumber LIKE '_06_'
This does not have the same effect in MS Access.
In Access it will be:
SELECT
COUNT(*) as CounTotal
FROM
tblTest2
WHERE
DSide=1 AND DNumber LIKE '?0?6' AND DDate BETWEEN #09-13-16# AND #09-13-17#

Use a checkmark as an column alias in SQL Server 2008 R2

I have a requirement to do something I believe should be simple enough, but am not finding the right answer to. How do I use a checkmark as a column alias in SQL Server 2008 R2?
I've tried using Char(251) by setting it to a value and trying to assign the value as the column alias, but no joy on that one.
I've tried using Char(251) (and I know that's more of a square root mark, but not sure of the checkmark ascii value if there is one. I believe that is a unicode value?) directly but again no joy. This should be simple, but I'm simply not finding it.
Thanks.
You cannot use expressions as identifiers in SQL Server (or any other SQL database for that matter). You can, however, use Unicode characters in identifiers, so simply copy and paste the desired character:
select 'yes' as "☑︎";
or even
select 'blah' as "😀";
Having said that, you should not be doing all that -- presentation is not the task for a database engine; it should be implemented in the client application.

SQL Left/Deliminated Character

Pretty simple one today. I've got a column, let's call it title, with a bunch of project titles. What I need to to pull everything from the left of the ":" and do a left/right trim (I'm then going to be using that in a join later on but I just need a column with the new data for now). So here's an example of what the current column looks like:
And here's what I need it to look like after the query is run:
The problem is while the # are 6 characters now, I can't guarantee they'll always be 6 characters. So if I was doing this in Excel I'd use the deliminated feature or just write a left/len/search function. Wondering how to do the same in SQL. BTW, I'm using SQL Server Management Studio.
Thoughts?
Assuming that your number is always followed by a [space]:[space], then simply look for that first space, and use its location as the argument for a left-substring operation:
SELECT LEFT(Title, CHARINDEX(' ', Title, 0)) AS "New Title"
p.s. Just say you're using MS SQL Server. SSMS is just a management front-end for that database.
check this post out. it does exactly what you are trying to do.
SQL Server replace, remove all after certain character

Is there a need to include brackets [] when running a query in SQL Server 2008?

I have SQL Server 2008 Express Edition installed and is my first time running a SQL query. I noticed that if I select to display the top 1000 rows that the query places brackets [] around the name of the database and its respective columns.
Why are there brackets around the name of the database and columns?
Is there a need to have these and if so when?
I just posted this answer on dba.stack.
They escape names that are not "friendly" - they can be useful if your database names contain special characters (such as spaces, dots or dashes) or represent SQL keywords (such as USE or DATABASE :-)). Without the square brackets, a query like this will fail:
SELECT column FROM database.dbo.table;
However if you write the query this way, the keywords are ignored:
SELECT [column] FROM [database].dbo.[table];
When building scripts or writing solutions that generate scripts from metadata (e.g. generating a script of all tables or all indexes), I always wrap entity names in square brackets so that they will work regardless of what kind of wonky names have been implemented (I am not always doing this for systems I control). You can do this easily using QUOTENAME function, e.g.:
SELECT QUOTENAME('table'), QUOTENAME('column'), QUOTENAME('database');
Results:
[table] [column] [database]
If you search my answers here for QUOTENAME you will find that I use it in a lot of scripts where I'm helping folks automate script generation or building dynamic SQL. Without the square brackets, a lot of people would run into issues running the scripts.
Enclosing a string in square braces is tells SQL Server that it should not try to parse whatever is inside them. This allows you to do use things like SQL reserved words (select, table, return, etc.) as well as spaces in identifiers. They are only required in those cases, but they are not harmful in any case.
No there isn't, but it ensures that if you had a db, table or column named as a reserved or keyword, for example date, that it wouldn't be confused with that keyword.