IIF Statement in SQL Server Management Studio 2014 - ssms-2014

I’m using SQL Server Management Studio 2014 and trying to run code with an IIF statement.
The syntax is correct as far as I can see but I keep getting an error
Msg 102, Level 15, State 1, Line 2
Incorrect syntax near '='.
The code is below
SELECT
[Name], [Street], [City],
IIF([Country] = 'UK', 'UK', 'Overseas') AS Country
FROM
[dbo].TblAddress

The syntax is correct.
It now depends on which version of SQL Server you have rather than which version of SQL Server Management Studio.
For example running
SELECT
[Name], [Street], [City],
IIF([Country] = 'UK','UK','Overseas') AS Country
FROM
[dbo].TblAddress
on SQL Server 2012 or higher would work (SQL Server 2014 in the below example)
But on SQL Server 2008 or lower it returns:
(SQL Server 2008 R2 is Version 10.50...)
Msg 102, Level 15, State 1, Line 2
Incorrect syntax near '='.
This is because IIF is only available in SQL Server 2012 onwards
https://learn.microsoft.com/en-us/sql/t-sql/functions/logical-functions-iif-transact-sql
The version of SQL Server Management Studio doesn't matter.

Related

why dosen't oracle database link show column with image type from sqlserver

we have 2 server :
1-sqlserver 2012 on windows 2012
2-oracle 11g on linux
we want to show information from table1 in sql server on oracle database and created database link between them
when i have a query like
"select ID,NAME from TABLE1" in plsql
i can see true result
but when i have a query like
"select ID,NAME,picimage from TABLE1" in plsql
i get these errors
1 ORA-03113:end-of-file on communication channel
2 ORA-03114:not connected to oracle
can anybody help me?
Most probably the IMAGE data type is not recognized by Oracle. It is DEPRECATED in SQL Server, convert the column to VARBINARY either in the query or in the table.
I highly recommend to convert it in the table, since NTEXT, TEXT and IMAGE data types will be removed from further versions of SQL Server. Microsoft recommends to use NVARCHAR(MAX), VARCHAR(MAX), VARBINARY(MAX) instead.
https://learn.microsoft.com/en-us/sql/t-sql/data-types/ntext-text-and-image-transact-sql

MSSQL query no longer works with Windows 10 client: The conversion of a varchar data type to a datetime data type resulted in an out-of-range value

We have been using the following SQL query for a long time in a WinForms program with no problems, until some end users upgraded to Windows 10.
They suddenly get the exception:"ERROR [22007] [Microsoft][SQL Server Native Client 11.0][SQL Server]The conversion of a varchar data type to a datetime data type resulted in an out-of-range value."
This error has been posted to inside here earlier, but I did not find any post where the occurrence was connected to a Windows 10 upgrade.
The query is targeted to a SQL server 2012, using Native Client 11. It works on windows 7 and 8, but throws exception in Windows 10:
SELECT DISTINCT tblEmployee.EmployeeID, tblEmployee.Lastname, (COALESCE(tblEmployee.Firstname, '') + ' (' + COALESCE(tblEmployee.EmployeeIDText, '') +')' ) AS Firstname
FROM tblEmployee
LEFT JOIN tblAssignmentService ON tblEmployee.EmployeeID = tblAssignmentService.EmployeeID
WHERE tblAssignmentService.ServiceDate >= '2015-08-31 00.00.00'
AND tblAssignmentService.ServiceDate < '2015-09-07 00.00.00'
ORDER BY tblEmployee.Lastname;
The only place where DateTime fields are used is in the Where clause, and the query works fine against the same DB with a windows 8 client. Both clients run Einglish Windows versions. Another interesting observation is that the query is accepted from Microsoft SQL Management Studio on the Windows 10 machine. But not through the native client. The dates used in the filter is created in our program through a GUI.
Have somebody else experienced strange things with Native Client on Windows 10, or does anyone have a suggestion to how this problem may be solved?
I confirm that the solution purposed in this thread solved my problem.
After some research we found that the ToString("yyyy-MM-dd HH:mm:ss") call responded differently in Windows 7 and Windows 10 if the current culture was set to "no" or "nb-NO" at runtime (Thread.CurrentThread.CurrentCulture = new CultureInfo("no");).
The suggested modification did the trick: ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture). This solves my problem, as the dots in the time formatting was its root cause.
But should ToString("yyyy-MM-dd HH:mm:ss") return different formatting in the same culture depending on OS? :O This is kind of scary.

Membership.GetUser() -> Failed to generate a user instance of SQL Server

If i try to call Membership.GetUser();, I get this error: "Failed to generate a user instance of SQL Server due to a failure in starting the process for the user instance. The connection will be closed."
Other than that, membership works, so I can create a new user and log in with it.
(At first, I kept getting the sql sever 2008 and 2008 r2 compatibility error, so I deleted 2008 and installed 2012. )
What should I do?
Well, I tried this solution once again: (I did it a few days ago, but I installed SQL Express 2012 since): Failed to generate a user instance of SQL Server due to a failure in starting the process for the user instance. The connection will be closed, and this time it worked!

SqlMembership on both Azure and Standalone SQL Server

I'm working on an ASP.NET Web Forms application with SqlMembershipProvider. As it is running on Azure too, I use KB2006191 Updated ASP.NET scripts for use with SQL Azure for tables and SPs.
Apparently, they do not work smoothly with a standalone SQL Server 2008 R2 SP1. Especially dbo.aspnet_Users_DeleteUser fires an internal error:
Msg 8624, Level 16, State 1, Procedure aspnet_Users_DeleteUser, Line 111
Internal Query Processor Error: The query processor could not produce a query plan. For more information, contact Customer Support Services.
Edit:
I broke down the issue to one SQL statement which actually fails (even if I hard-code a value for #UserId):
DELETE FROM dbo.aspnet_Users WHERE UserId = #UserId
Original:
I tried fixing my SQL Server installation by downloading a Cumulative Update Package and setting trace flag 4199. However, this did not help. To be honest, I'm not too sure whether I applied the patch properly. I'm doing further research on this.
Moreover, I tried to rewrite the SP a little, as I found some tips on the web. This did not help either, and the same query is generated by aspnet_regsql for standalone SQL Server anyway. Actually, I tried to get rid of a subquery around line 111 by turning this:
IF ((#TablesToDeleteFrom & 1) <> 0 AND
(#TablesToDeleteFrom & 2) <> 0 AND
(#TablesToDeleteFrom & 4) <> 0 AND
(#TablesToDeleteFrom & 8) <> 0 AND
(EXISTS (SELECT UserId FROM dbo.aspnet_Users WHERE #UserId = UserId)))
... into this:
IF ((#TablesToDeleteFrom & 15) <> 0)
Did anybody come across similar issues dealing with membership and both Azure and standalone SQL Server?
According to Scott Hanselman and this blog post, the ASP.NET Universal Providers For SqlExpress 1.1 package (System.Web.Providers) is the officially supported method of enabling the SQL membership, role and session providers across SQL Azure and standalone SQL Server.

Why Windows Server 2003 causes "varchar to datetime out-of-range", wheras Server 2008 does not

I have written a small Asp.net application using Entity Framework.
A Stored Procedure accepts the following:
employeeID int, startDate varchar(12),endDate varchar(12)
I then use SQL server to convert the short date strings to DateTime.
SET #correctStartDate = CONVERT(DATETIME,(convert(varchar(10),#startDate,103) + ' 00:00:00am'), 103)
SET #correctEndDate = CONVERT(DATETIME,(convert(varchar(10),#endDate,103) + ' 11:59:59pm'), 103)
In development environment iis7 Windows Server 2008 with SQL Server 2008 R2 there are no issues.
If we deploy to production server iis6 (Windows Server 2003 SP2 with SQL Server 2008 R2) we get:
"The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.The conversion of a varchar data type to a datetime data type resulted in an out-of-range value."
Why would this be? Both have regional settings the same.
What are the values of start and end date?
Try doing the same using datetime2 instead of datetime.
The problem was what Martin said. The language settings have a login context.
To solve the issue I have inserted the datetime into the stored procedure instead of the datetime.ToShortDateString() method.
You could also use myDatetime.ToString("dd/MM/yyyy");

Resources