The ISNULL function and the COALESCE expression have a similar purpose but ISNULL can have two parameters only, COALESCE can have multiple input.
Both are checking the parameters in order and returns the current value of the first expression that initially doesn’t evaluate to NULL.
SELECT ISNULL(hourly_wage * 40 * 52, salary) AS income FROM dbo.wages
SELECT COALESCE(hourly_wage * 40 * 52, salary, commission) AS income FROM dbo.wages
The NULLIF function returns a NULL value if the two parameters are equal. for instance, we could use NULLIF if we wanted to return NULLs whenever the field equal giving value:
SELECT NULLIF(4,4) AS Same, NULLIF(5,7) AS Different;
The msg_str argument in RAISERROR (Transact-SQL) described the structure of message string and the use of parameters in the string. It’s very useful if we want to build dynamic text or string with multiple parameters. In this article, we are listed variety of format sample.
Conversion specifications format
% [[flag] [width] [. precision] [{h | l}]] type
flag
Is a code that determines the spacing and justification of the substituted value.
Code
Prefix or justification
Description
– (minus)
Left-justified
Left-justify the argument value within the given field width.
+ (plus)
Sign prefix
Preface the argument value with a plus (+) or minus (-) if the value is of a signed type.
0 (zero)
Zero padding
Preface the output with zeros until the minimum width is reached. When 0 and the minus sign (-) appear, 0 is ignored.
# (number)
0x prefix for hexadecimal type of x or X
When used with the o, x, or X format, the number sign (#) flag prefaces any nonzero value with 0, 0x, or 0X, respectively. When d, i, or u are prefaced by the number sign (#) flag, the flag is ignored.
‘ ‘ (blank)
Space padding
Preface the output value with blank spaces if the value is signed and positive. This is ignored when included with the plus sign (+) flag.
width
Is an integer that defines the minimum width for the field into which the argument value is placed. If the length of the argument value is equal to or longer than width, the value is printed with no padding. If the value is shorter than width, the value is padded to the length specified in width.
An asterisk (*) means that the width is specified by the associated argument in the argument list, which must be an integer value.
precision
Is the maximum number of characters taken from the argument value for string values. For example, if a string has five characters and precision is 3, only the first three characters of the string value are used.
For integer values, precision is the minimum number of digits printed.
An asterisk (*) means that the precision is specified by the associated argument in the argument list, which must be an integer value.
{h | l} type
Is used with character types d, i, o, s, x, X, or u, and creates shortint (h) or longint (l) values.
When we set up SQL Server Job, its is a very common requirement ‘need to get email notification once Job is failed’. It’s not difficult, just need few clicks in SQL Server Management Studio to implement this requirement.
Overview Steps
Configure Database Mail.
Set up SQL Server Agent Alert System .
Create an Operator.
Set up SQL Server job notifications
Configuring Database Mail
Open SQL Server Management Studio (SSMS), expand Management and right-click on Database Mail and then click Configure Database Mail.
Select “Set up Database Mail by performing the following tasks” and Click Next.
Click Yes and then click Next if mail hasn’t been configured yet.
Give the New Profile a name and then click Add.
Fill in the correct SMTP information as below
click Next
Check the option to make the new profile Public. If the profile isn’t set to Public
Review the System Parameters , please keep the default settings if your are not sure how to adjust them. Click Next and Finish the Mail set up.
You should see that each Action was completed and has a Status of Success. Click Close.
Set up SQL Server Agent Alert System
Right-click SQL Server Agent and select Properties.
Click on Alert System
· Under Mail session, select the option to Enable mail profile. Ensure the correct Mail profile is selected.
· Under Token replacement, enable Replace tokens for all job responses to alerts.
Click OK. Restart the SQL Server Agent service.
Create an Operator
Under the SQL Server Agent, right-click Operators and select New Operator…
Type in the recipient email address in the E-mail name and click OK.
Set up SQL Server job notifications
Expand SQL Server Agent and click on Jobs. Find the job you want to change, right click it and select Properties.
Under Select a page, select Notifications. Enable the first option, E-mail. Then select the Operator we just created and change the job to email When the job fails. Click OK.
SQL Server has a couple of inbuilt functions to get the day of week from the given date. To get the name of the day of week, you can use DATENAME function and to get the number of the day of week, you can use DATEPART function.
Example
SELECTDATENAME(WEEKDAY, GETDATE()) /* Result */ Monday
SELECTDATEPART(WEEKDAY, GETDATE()) /* Result */ 2
By default, SQL server always uses Sunday as the first day of the week. You can use SET DATEFIRST 1 to set Monday as the first day of the week.
I am trying to get all products and category name if category is still active with below left join script. But I cannot get production information if category is not active. What’s wrong with this left join script?
select p.name, c.name from products p left join categories c on p.categoryid = c.categoryid where c.isactive = 1 or c.isactive is null
Reason
To find out what’s the issue of this script have, we need to understand order of a SQL query. In the script above we used SELECT, FROM, LEFT JOIN and WHERE
FROM and JOIN: The FROM clause, and subsequent JOINs are first executed to determine the total working set of data that is being queried
WHERE: Once we have the total working set of data, the WHERE constraints are applied to the individual rows, and rows that do not satisfy the constraint are discarded.
SELECT: Any expressions in the SELECT part of the query are finally computed.
The WHERE clause is execution after the ON clause. which means the predicates in the ON clause are applied to the table before the join, WHERE clause is semantically applied to the result of the join.
So below script will get all the data from product table and will keep categories data as null if cannot find category.
select p.name, c.name from products p left join categories c on p.categoryid = c.categoryid
WHERE clause is applied to the JOIN result which will remove products result if category missing.
where c.isactive = 1 or c.isactive is null
Solution
if we want to get all product information even if categories is missing or is not active, we need to changed the WHERE clause to ON clause as below:
select p.name, c.name from products p left join categories c on p.categoryid = c.categoryid and (c.isactive = 1 or c.isactive is null )
This blog I will show you a very trick issue thing a bout nchar and nvarchar actual size and storage size.
Issue
When I use sp_help to check the table property. I got below result:
but when I using SSMS to check the column size, it shows nvarchar(50)
so what’s the different between sp_help and SSMS design view?
Finding
I checked sp_help , it is get the max length field from sys.all_columns. In Microsoft document website, it mentioned maximum length (in bytes) of the column.
But we know nchar and nvarchar, the storage size is two times n bytes (n is the actual number of characters ). so this explained, sp_help is not real character size, it is the storage size in bytes. But SSMS design view is using real character size.
Recently, I try to setup a new SQL SERVER on AWS. When I try to connect to server via private IP. I got below SQL Login error:
Login failed. The login is from an untrusted domain and cannot be used with Integrated authentication. [CLIENT: 172.31.23.96]
SSPI handshake failed with error code 0x8009030c, state 14 while establishing a connection with integrated security; the connection has been closed. Reason: AcceptSecurityContext failed. The operating system error code indicates the cause of failure. The logon attempt failed [CLIENT: 172.31.23.96]
WORKAROUND/SOLUTION
Loopback check can be removed by adding a registry entry as follows:
Edit the registry using regedit. (Start –> Run > Regedit )
Navigate to: HKLM\System\CurrentControlSet\Control\LSA
One of the most frequently question I asked is which date format style number I should to use. Here’s a summary of different date and time style matrix that we can used in T-SQL as part of the CONVERT function.
But how we can compare two tables schema quickly via T-SQL? We can use INFORMATION_SCHEMA.COLUMNS to compare two tables schema easily, but this way cannot compare table cross database or instance. In this blog I will show how to compare it with internal function sys.dm_exec_describe_first_result_set
before we start compare two tables, let’s see what kind of information the function can get
select * from sys.dm_exec_describe_first_result_set ( N’SELECT * FROM dbo.demo1′, NULL, 1)
We can see the returned schema as below:
Doing Comparisons
To do two tables comparison, we just need to simply doing OUTER JOIN between the two function results. (To do comparisons between to database or instance, please use linked servers and full part name to address the table. like product.dbo.demo2)
select a.column_ordinal,b.column_ordinal,a.name,b.name ,a.is_nullable,b.is_nullable ,a.system_type_name,b.system_type_name,a.max_length,b.max_length,a.precision,b.precision,a.scale,b.scale from sys.dm_exec_describe_first_result_set ( N’SELECT * FROM dbo.demo1′, NULL, 1) as a full outer join sys.dm_exec_describe_first_result_set ( N’select * from dbo.demo2′, NULL, 1) as b on a.name = b.name
We can see the comparison result between to tables now.
The bulk copy program utility (bcp) is a very powerful bulk copy tool between database and flat file. Normally used for large number of data import and export. But if you sql server version is older then 2014 SP2, unlucklly you cannot process UTF8 data even if there is a code page parameter -C , but it doesn’t support UTF8. Refer to Microsoft community document
code_page
Specific code page number; for example, 850.
** Important ** SQL Server does not support code page 65001 (UTF-8 encoding).
If you want to process UTF8, then must need to upgrade SQL Server to version 2014 SP2 or later. Once you installed the upgrade, then you can use BCP to import or export UTF8 as below:
To import UTF-8 data to SQL Server, use the BCP utility and run the following bcp t_users in “D:\test.csv” -c -C 65001
To export UTF-8 data to SQL Server, use the BCP utility and run the following bcp t_users out ” D:\test.csv ” -c -C 65001