SQL server version query - sql

How to check whether we have installed sql server full version or client version ?

You can use Select ##version
Or to get more data:
SELECT SERVERPROPERTY('productversion')AS Product_version,
SERVERPROPERTY('productlevel')AS Product_level,
SERVERPROPERTY('edition')AS Edition

To get some more details like Edition, Service Pack.
select SERVERPROPERTY('MachineName')as 'Host Name',
SERVERPROPERTY('ServerName') as 'Instance Name' ,
SERVERPROPERTY('IsClustered') as 'Cluster' ,
SERVERPROPERTY('Edition') as 'Edition',
SERVERPROPERTY('ProductVersion') as 'version',
SERVERPROPERTY('Productlevel') as 'Service Pack',
SERVERPROPERTY('LicenseType') as 'LicenseType' ,
SERVERPROPERTY('NumLicenses') as 'NumLicenses'

I've created this query which it gonna return data like this:
SQL Server 2016 Standard Edition (64-bit) 13.0.5237.0 SP2
DECLARE
#productver VARCHAR(50) = (SELECT CAST(SERVERPROPERTY('productversion') AS VARCHAR(50)))
DECLARE
#version VARCHAR(30)= CAST(LEFT(#productver, CHARINDEX('.', #productver)-1) AS INT)
SELECT
'SQL Server '+CASE #version
WHEN 9
THEN '2005'
WHEN 10
THEN '2008'
WHEN 11
THEN '2012'
WHEN 12
THEN '2014'
WHEN 13
THEN '2016'
WHEN 14
THEN '2017'
ELSE 'Unknow Version'
END + ' ' + CAST(SERVERPROPERTY('edition') AS VARCHAR(50)) AS SQLServerEdition,
#productver AS ProductVersion,
SERVERPROPERTY('productlevel') AS ServicePack

In linux Terminal:
$ sqlcmd -S localhost -U sa
Note: Here sa is the username, change it if your username is different.
Provide your password then enter your sql server.
Write:
1> select ##VERSION
2> GO

Try this to know your SQL Server Version:
SELECT ##VERSION

Related

SQL to get whole words till end from the keyword

Consider I have text from the field (notes) as below:
Please check http://example.com
I want a SQL query which fetches the link part only. That is to find keyword http and print till the last.
Output:
http://example.com
Also if the text field doesnt have any link, can we print NA?
CASE WHEN sys like '%Clo%'
THEN RIGHT( i.notes,LEN(i.notes) - CHARINDEX('http',i.notes,1)+1)
ELSE "No Link Available" END AS Cl_Link
For SQL Server you can consider this below logic-
DECLARE #T VARCHAR(MAX) = 'Please check http://example.com'
SELECT RIGHT(#T,LEN(#T) - CHARINDEX('http:',#T,1)+1)
For MySQL-
SET #T = 'Please check http://example.com';
SELECT RIGHT(#T,LENGTH(#T) - POSITION("http:" IN #T)+1)
In case of select query using table, queries will be-
-- SQL Server
SELECT RIGHT(column_name,LEN(column_name) - CHARINDEX('http:',column_name,1)+1) FROM your_table_name
-- MySQL
SELECT RIGHT(column_name,LENGTH(column_name) - POSITION("http:" IN column_name)+1) FROM your_table_name
To apply 'NA' when no link available, please use the below logic-
DECLARE #T VARCHAR(MAX) = 'Please check http://example.com'
SELECT
CASE
WHEN CHARINDEX('http:',#T,1) >= 1 THEN RIGHT(#T,LEN(#T) - CHARINDEX('http:',#T,1)+1)
ELSE 'NA'
END
Below is the query for your reference, executed on mysql:
SELECT substr(columnname,locate('http',columnname)) as link
FROM `tablename` where column=value
For MySQL try this:
select REGEXP_SUBSTR(text_column, 'http\:\/\/([\\w\\.\\S]+)') from mytest

How to get database size in SQL Server Enterprise edition

I'm getting an error with regard of this query
SELECT
DB.name,
SUM(CASE WHEN type = 0 THEN MF.size * 8 / 1024.0 ELSE 0 END) AS DataFileSizeMB,
SUM(CASE WHEN type = 1 THEN MF.size * 8 / 1024.0 ELSE 0 END) AS LogFileSizeMB,
SUM(mf.size* 8 / 1024.0) AS TotalSizeMB,
SYSDATETIME() AS Datelogged
FROM
sys.master_files MF
JOIN
sys.databases DB ON DB.database_id = MF.database_id
WHERE
DB.source_database_id IS NULL
GROUP BY
DB.name
ORDER BY
Datelogged DESC
I get an error:
Arithmetic overflow error converting expression to data type int
I'm running this on SQL Server 2016 Enterprise edition. I've tried to run this on Standard and other lower editions and it'll work. Can someone tell what's wrong with this code and what should be the correct one. I do not own this code, I've just copied it from another site.
Action taken : I've try to convert this into bigint data type but still no avail.
Thank you very much
instead of MF.size * 8 use MF.size * 8.0

Error "DBCC execution completed" when running data connection from Excel

I get this error when I try to run this data connection from Excel 2010
Connection string:
Provider=SQLOLEDB.1;Persist Security Info=True;User ID=sa;Data Source=sql-
server;Use Procedure for Prepare=1;Auto Translate=True;Packet
Size=4096;Workstation ID=PV-SAMSUNG;Use Encryption for Data=False;Tag with
column collation when possible=False;Initial Catalog=BVR_AUTOMAX
Command text:
EXECUTE sp_executesql N'
BEGIN
DBCC TRACEON(8765);
SELECT *
FROM OPENQUERY(SugarCRM, ''
select ticker_symbol,count(a.id) as pocet,sum(case when ifnull(a.account_erp_id,0)=''''0'''' then 0 else 1 end) as bvr, count(a.id) - sum(case when ifnull(a.account_erp_id,0)=''''0'''' then 0 else 1 end) as delta
from crm.accounts a inner join crm.users u on a.assigned_user_id=u.id
inner join crm.accounts_cstm ac on a.id=ac.id_c
where a.deleted=0
group by ticker_symbol
having delta>0 and bvr>0
order by delta desc;
'' );
END';
When I run this code in MS SQL Server Mngt Studio it works fine.
Thanks for your help
Petr
I found a solution, quite simple one. Just change this line of code to the following (add the WITH NO_INFOMSGS argument)
DBCC TRACEON(8765) WITH NO_INFOMSGS ;

How to get current instance name from T-SQL

How can I get the SQL Server server and instance name of the current connection, using a T-SQL script?
Just found the answer, in this SO question (literally, inside the question, not any answer):
SELECT ##servername
returns servername\instance as far as this is not the default instance
SELECT ##servicename
returns instance name, even if this is the default (MSSQLSERVER)
How about this:
EXECUTE xp_regread #rootkey='HKEY_LOCAL_MACHINE',
#key='SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQl',
#value_name='MSSQLSERVER'
This will get the instance name as well. null means default instance:
SELECT SERVERPROPERTY ('InstanceName')
http://technet.microsoft.com/en-us/library/ms174396.aspx
SELECT ##servername will give you data as server/instanceName
To get only the instanceName you should run select ##ServiceName query .
Why stop at just the instance name? You can inventory your SQL Server environment with following:
SELECT
SERVERPROPERTY('ServerName') AS ServerName,
SERVERPROPERTY('MachineName') AS MachineName,
CASE
WHEN SERVERPROPERTY('InstanceName') IS NULL THEN ''
ELSE SERVERPROPERTY('InstanceName')
END AS InstanceName,
'' as Port, --need to update to strip from Servername. Note: Assumes Registered Server is named with Port
SUBSTRING ( (SELECT ##VERSION),1, CHARINDEX('-',(SELECT ##VERSION))-1 ) as ProductName,
SERVERPROPERTY('ProductVersion') AS ProductVersion,
SERVERPROPERTY('ProductLevel') AS ProductLevel,
SERVERPROPERTY('ProductMajorVersion') AS ProductMajorVersion,
SERVERPROPERTY('ProductMinorVersion') AS ProductMinorVersion,
SERVERPROPERTY('ProductBuild') AS ProductBuild,
SERVERPROPERTY('Edition') AS Edition,
CASE SERVERPROPERTY('EngineEdition')
WHEN 1 THEN 'PERSONAL'
WHEN 2 THEN 'STANDARD'
WHEN 3 THEN 'ENTERPRISE'
WHEN 4 THEN 'EXPRESS'
WHEN 5 THEN 'SQL DATABASE'
WHEN 6 THEN 'SQL DATAWAREHOUSE'
END AS EngineEdition,
CASE SERVERPROPERTY('IsHadrEnabled')
WHEN 0 THEN 'The Always On Availability Groups feature is disabled'
WHEN 1 THEN 'The Always On Availability Groups feature is enabled'
ELSE 'Not applicable'
END AS HadrEnabled,
CASE SERVERPROPERTY('HadrManagerStatus')
WHEN 0 THEN 'Not started, pending communication'
WHEN 1 THEN 'Started and running'
WHEN 2 THEN 'Not started and failed'
ELSE 'Not applicable'
END AS HadrManagerStatus,
CASE SERVERPROPERTY('IsSingleUser') WHEN 0 THEN 'No' ELSE 'Yes' END AS InSingleUserMode,
CASE SERVERPROPERTY('IsClustered')
WHEN 1 THEN 'Clustered'
WHEN 0 THEN 'Not Clustered'
ELSE 'Not applicable'
END AS IsClustered,
'' as ServerEnvironment,
'' as ServerStatus,
'' as Comments
I found this:
EXECUTE xp_regread
#rootkey = 'HKEY_LOCAL_MACHINE',
#key = 'SOFTWARE\Microsoft\Microsoft SQL Server',
#value_name = 'InstalledInstances'
That will give you list of all instances installed in your server.
The ServerName property of the SERVERPROPERTY function and ##SERVERNAME return similar information. The ServerName property provides the Windows server and instance name that together make up the unique server instance. ##SERVERNAME provides the currently configured local server name.
And Microsoft example for current server is:
SELECT CONVERT(sysname, SERVERPROPERTY('servername'));
This scenario is useful when there are multiple instances of SQL Server installed on a Windows server, and the client must open another connection to the same instance used by the current connection.
To get the list of server and instance that you're connected to:
select * from Sys.Servers
To get the list of databases that connected server has:
SELECT * from sys.databases;
Just to add some clarification to the registry queries. They only list the instances of the matching bitness (32 or 64) for the current instance.
The actual registry key for 32-bit SQL instances on a 64-bit OS is:
HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Microsoft SQL Server
You can query this on a 64-bit instance to get all 32-bit instances as well. The 32-bit instance seems restricted to the Wow6432Node so cannot read the 64-bit registry tree.
You can get your server name, machine name and instance name with Transact-SQL(T-SQL) as shown below:
SELECT ##SERVERNAME -- DESKTOP-OVPADTC\SQLEXPRESS
SELECT SERVERPROPERTY ('ServerName') -- DESKTOP-OVPADTC\SQLEXPRESS
SELECT HOST_NAME() -- DESKTOP-OVPADTC
SELECT SERVERPROPERTY ('MachineName') -- DESKTOP-OVPADTC
SELECT ##SERVICENAME -- SQLEXPRESS
SELECT SERVERPROPERTY ('InstanceName') -- SQLEXPRESS
another method to find Instance name- Right clck on Database name and select Properties, in this part you can see view connection properties in left down corner, click that then you can see the Instance name.

Weird SQL Behavior, why is this query returning nothing?

Assume there is a table named "myTable" with three columns:
{**ID**(PK, int, not null),
**X**(PK, int, not null),
**Name**(nvarchar(256), not null)}.
Let {4, 1, аккаунт} be a record on the table.
select * from myTable as t
where t.ID=4
AND t.X = 1
AND ( t.Name = N'аккаунт' )
select * from myTable as t
where t.ID=4
AND t.X = 1
AND ( t.Name LIKE N'%аккаунт%' )
The first query return the record, however, the second does not? Why?
Systems where this issues are experienced:
*Windows XP - Professional - Version 2002 - SP3
Server Collation: Latin1_General_CI_AS
Version: 9.00.3073.00
Level: SP2
Edition: Developer Edition
Sever Collation: SQL_Latin1_General_CP1_CI_AS
Version: 9.00.3054.00
Level: SP2
Edition: Enterprise Edition
Results:
SELECT SERVERPROPERTY('SQLCharSetName')
iso_1
Using OSQL.exe
0x30043A043A04300443043D04420400000000000000000000000000000000
0x3F3F3F3F3F3F3F0000000000000000000000000000000000000000000000
0x253F3F3F3F3F3F3F25000000000000000000000000000000000000000000
SELECT CAST(name AS BINARY),
CAST(N'аккаунт' AS BINARY),
CAST(N'%аккаунт%' AS BINARY)
FROM myTable t
WHERE t.ID = 4
AND t.X = 1
CAST(name AS BINARY)
0x30043A043A04300443043D04420400000000000000000000000000000000
CAST(N'аккаунт' AS BINARY)
0x3F3F3F3F3F3F3F0000000000000000000000000000000000000000000000
CAST(N'%аккаунт%' AS BINARY)
0x253F3F3F3F3F3F3F25000000000000000000000000000000000000000000
Could you please post the result of the following query:
SELECT CAST(name AS BINARY),
CAST(N'аккаунт' AS BINARY),
CAST(N'%аккаунт%' AS BINARY)
FROM myTable t
WHERE t.ID = 4
AND t.X = 1
This will help to narrow the problem down.
UPDATE:
As I can see from the results of your query, you have a problem with encoding.
The Cyrillic literals from your string constants are being converted to the question marks (0x3F).
Unfortunately, I cannot reproduce this behavior with Management Studio on my test server.
I reckon there is some problem with OS settings, as Cyrillic characters most probably don't even reach SQL Server.
Could you please answer three more questions:
What OS are you using (version, language, MUI if any)
What does this query return:
SELECT SERVERPROPERTY('SQLCharSetName')
Connect to your server using osql.exe and issue this query:
SELECT CAST(name AS BINARY),
CAST(N'аккаунт' AS BINARY),
CAST(N'%аккаунт%' AS BINARY)
FROM myTable t
WHERE t.ID = 4
AND t.X = 1
GO
What does it return being run in osql.exe?
Both queries return the same result for me.
select * from myTable as t
where t.ID=4
AND t.X = 1
AND (t.Name = N'аккаунт')
Returns:
ID X Name
----------- ----------- ------------
4 1 аккаунт
And
select * from myTable as t
where t.ID=4
AND t.X = 1
AND (t.Name LIKE N'%аккаунт%')
Returns:
ID X Name
----------- ----------- ------------
4 1 аккаунт
(1 row(s) affected)
My version of SQL Server is:
Microsoft SQL Server 2005 - 9.00.3077.00 (Intel X86)
Dec 17 2008 15:19:45
Copyright (c) 1988-2005 Microsoft Corporation
Express Edition on Windows NT 5.1 (Build 2600: Service Pack 3)
My collation is set to: SQL_Latin1_General_CP1_CI_AS
My results for Quassnoi:
0x30043A043A04300443043D04420400000000000000000000000000000000
0x30043A043A04300443043D04420400000000000000000000000000000000
0x250030043A043A04300443043D0442042500000000000000000000000000
(1 row(s) affected)
Alright, after a great deal of research, I found it is indeed a problem found on the following versions of SQL Server 2005:
Windows XP - Professional - Version 2002 - SP3
Version: 9.00.3073.00
Level: SP2
Edition: Developer Edition
Version: 9.00.3054.00
Level: SP2
Edition: Enterprise Edition
..may be other versions as well.
FIX: Upgrade to SP3.