|
Depends what you mean by "better".
You're using SQL 2016, so STRING_AGG[^] is out - that was added in 2017.
But you can use STRING_SPLIT[^], TRY_PARSE[^], and FORMAT[^]. And there are ways[^] to concatenate row values in 2016 and earlier.
I've assumed a table without a primary key; if your table has one, use that instead of the generated ROW_NUMBER :
WITH cteRN As
(
SELECT
ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) As RN,
tree
FROM
MyTable
)
SELECT
STUFF(
(
SELECT '.' + IsNull(Format(Try_Parse(P.value As int), 'D2'), P.value)
FROM cteRN As T2
CROSS APPLY string_split(T2.tree, '.') As P
WHERE T2.RN = T.RN
FOR XML PATH(''), TYPE
).value('.', 'varchar(max)')
, 1, 1, '') As tree
FROM
cteRN As T
GROUP BY
T.RN
; It's not pretty, but it works:
01.00
01.00.01
01.00.02
01.00.02.01
01.00.02.02
01.10.01.35
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
That is almost unreadable!
Never underestimate the power of human stupidity -
RAH
I'm old. I know stuff - JSOP
|
|
|
|
|
That's part of what makes it beautiful.
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
|
|
|
|
|
I did say it wasn't pretty!
STRING_AGG would probably make it slightly better, but that needs SQL Server 2017.
And if the source table has a primary key already, you can ditch the CTE.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Wanna hear something funny?
Our dev/test boxes are on SQL 2008R2, but our production and pre-production databases are 2016. I don't have access to the prod DBs, so I can't play with the code. :/
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
|
|
|
|
|
At least it's not the other way round.
You can play with it here: SQL Fiddle[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
That doesn't fix our in-house DB version conflicts.
BTW, I misspoke - dev is 2012, test is 2008r2, and prod/pre-prod are 2016. So it's even worse than I initially indicated.
I complained to the DBAs, and they just shrugged their shoulders...
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
|
|
|
|
|
I'm trying to create a small sample database. Towards the bottom when I', inserting Book3, I suddenly get an FK violation from Books into Categories. Yet the caregory id is there.
Can someone try thnis and tell me wtf is wrong????
USE master
IF EXISTS(SELECT * FROM sys.databases WHERE Name = 'BookCatalog')
DROP DATABASE BookCatalog
Go
CREATE DATABASE BookCatalog
GO
USE [BookCatalog]
GO
CREATE TABLE [dbo].[BookCategories]
(
[Id] [INT] IDENTITY(1,1) PRIMARY KEY NOT NULL,
[Name] VARCHAR(MAX) NOT NULL,
)
CREATE TABLE [dbo].[Books]
(
[Id] [INT] IDENTITY(1,1) PRIMARY KEY NOT NULL,
[Category] [INT] NULL FOREIGN KEY (Id) REFERENCES BookCategories(Id),
[Title] VARCHAR(MAX) NOT NULL,
[Price] MONEY NULL
)
CREATE TABLE [dbo].[Authors]
(
[Id] [INT] IDENTITY(1,1) PRIMARY KEY NOT NULL,
[Name] VARCHAR(MAX) NOT NULL,
)
CREATE TABLE [dbo].[BookAuthors]
(
[Id] [INT] IDENTITY(1,1) PRIMARY KEY NOT NULL,
[Book] [INT] NULL FOREIGN KEY (Id) REFERENCES Books(Id),
[Author] [INT] NULL FOREIGN KEY (Id) REFERENCES Authors(Id),
)
GO
USE [BookCatalog]
-- Book Categories
DECLARE @CategoryId1 INT
INSERT INTO BookCategories (Name) VALUES ('C#')
SET @CategoryId1 = SCOPE_IDENTITY()
DECLARE @CategoryId2 INT
INSERT INTO BookCategories (Name) VALUES ('Programming Practices')
SET @CategoryId2 = SCOPE_IDENTITY()
-- Authors
DECLARE @MartinId INT
INSERT INTO Authors (Name) VALUES ('Bob Martin')
SET @MartinId = SCOPE_IDENTITY()
DECLARE @Albahari1Id INT
INSERT INTO Authors (Name) VALUES ('Joseph Albahari')
SET @Albahari1Id = SCOPE_IDENTITY()
DECLARE @Albahari2Id INT
INSERT INTO Authors (Name) VALUES ('Ben Albahari')
SET @Albahari2Id = SCOPE_IDENTITY()
DECLARE @WagnerId INT
INSERT INTO Authors (Name) VALUES ('Bill Wagner')
SET @WagnerId = SCOPE_IDENTITY()
DECLARE @SkeetId INT
INSERT INTO Authors (Name) VALUES ('Jon Skeet')
SET @SkeetId = SCOPE_IDENTITY()
-- Books
DECLARE @BookId1 INT
INSERT INTO Books (Category, Title, Price) VALUES (@CategoryId1, 'Agile Principles, Patterns, and Practices in C#', 64.99)
SET @BookId1 = SCOPE_IDENTITY()
DECLARE @BookId2 INT
INSERT INTO Books (Category, Title, Price) VALUES (@CategoryId2, 'C# 3.0 in a Nutshell', 34.99)
SET @BookId2 = SCOPE_IDENTITY()
DECLARE @BookId3 INT
INSERT INTO Books (Category, Title, Price) VALUES (@CategoryId2, 'C# Core Langaue: Little Black Book', 20.00)
SET @BookId3 = SCOPE_IDENTITY()
DECLARE @BookId4 INT
INSERT INTO Books (Category, Title, Price) VALUES (@CategoryId2, 'C# In Depth', 44.99)
SET @BookId4 = SCOPE_IDENTITY()
--Book-Authors
INSERT INTO BookAuthors (Book, Author) VALUES (@BookId1, @MartinId)
INSERT INTO BookAuthors (Book, Author) VALUES (@BookId2, @Albahari1Id)
INSERT INTO BookAuthors (Book, Author) VALUES (@BookId2, @Albahari2Id)
INSERT INTO BookAuthors (Book, Author) VALUES (@BookId3, @WagnerId)
INSERT INTO BookAuthors (Book, Author) VALUES (@BookId4, @SkeetId)
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
Quote:
...
[Category] [INT] NULL FOREIGN KEY (Id) REFERENCES BookCategories(Id),
...
[Book] [INT] NULL FOREIGN KEY (Id) REFERENCES Books(Id),
[Author] [INT] NULL FOREIGN KEY (Id) REFERENCES Authors(Id)
... The definition of your foreign keys is wrong. You're saying that the ID of the book must also exist in the BookCategories table, and the ID of the book author must exist in both the Books and Authors tables.
Once you fix the FK definitions, the rest of your script will work:
CREATE TABLE [dbo].[BookCategories]
(
[Id] [INT] IDENTITY(1,1) PRIMARY KEY NOT NULL,
[Name] VARCHAR(MAX) NOT NULL
)
CREATE TABLE [dbo].[Books]
(
[Id] [INT] IDENTITY(1,1) PRIMARY KEY NOT NULL,
[Category] [INT] NULL FOREIGN KEY REFERENCES BookCategories(Id),
[Title] VARCHAR(MAX) NOT NULL,
[Price] MONEY NULL
)
CREATE TABLE [dbo].[Authors]
(
[Id] [INT] IDENTITY(1,1) PRIMARY KEY NOT NULL,
[Name] VARCHAR(MAX) NOT NULL
)
CREATE TABLE [dbo].[BookAuthors]
(
[Id] [INT] IDENTITY(1,1) PRIMARY KEY NOT NULL,
[Book] [INT] NULL FOREIGN KEY REFERENCES Books(Id),
[Author] [INT] NULL FOREIGN KEY REFERENCES Authors(Id)
)
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
That did it!!
Ya know I stared at this for an hour. I'm an idiot.
Thanks!
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
I'm trying to create a small sample database:
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
I think you forgot to post your question or script sample. I don't see it.
|
|
|
|
|
I need to connect to a client't SQL Server remotely. They want to set up a VPN for me to minimize security concerns.
Can I access SQL remotely on their network from my office if they set me up a VPN?
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
So long as the VPN's connected, it should be the same as accessing a server on the same network.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
I have been trying forever to set up access to SQL Server on my home server.
I again followed all of the instructions here:
https://medium.com/developer-diary/how-to-enable-remote-connections-to-sql-server-dc5b6c812b5
Windows Firewall is off.
When I try to connect remotely I get
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server) (Microsoft SQL Server, Error: 53)
This message mentions 'Named Pipes'. Named pipes is enabled, although from what I can see, Named Pipes are for inter-process communication to occur among processes running on the same machine, so I'm not sure what this is part of the error.
At this point I'm out of options. I've tried eveything I can think of. What could possibly be wrong here??
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
What do you mean by "remotely"? Something else on the same LAN, or something outside of your network?
Check which client protocols are enabled on the computer that's trying to connect. If it doesn't have the SQL configuration tools installed, you'll need to run cliconfg.exe from both C:\Windows\System32\ (64-bit) and C:\Windows\SysWow64\ (32-bit).
Also check your connection string to make sure you're using the correct server and instance name. For example, ".\SQLEXPRESS" won't work from a remote computer.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Remote from my office to my home server. I connect using my IP addrss.
Richard Deeming wrote: Check which client protocols are enabled on the computer that's trying to connect. If it doesn't have the SQL configuration tools installed, you'll need to run cliconfg.exe from both C:\Windows\System32\ (64-bit) and C:\Windows\SysWow64\ (32-bit).
Well, that did it!!!
Wow, I've been trying on & off again for MONTHS to get this to work. THANK YOU!!
I've set up SQL Remote access before, and I don't remember ever having use 'cliconfg.exe' before. Is this documented somewhere?
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
I can't see an MS documentation, but there are various blogs describing the tool - for example:
How to setup and use a SQL Server alias[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Awesome, thanks!
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
|
Kevin Marois wrote: I take all this back. As soon as I posted I thought, I'll give it one more try - and it connected
Isn't that always the way it goes?
Glad you got it sorted.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Hi all,
When I exporting Database table to Excel my date fields are giving 23/01/1990 00:00:00 in stat of 23/01/1990
Please can some one help me in the right way.
Quote: protected DataTable ExportDataFromSQLServer()
{
DataTable dataTable = new DataTable();
using (SqlConnection connection = new SqlConnection(@"Data Source= 127.0.0.1\MSSQLSERVERJAN; Initial Catalog=BHV; User ID=sa;Password=XXXXXXX"))
{
connection.Open();
// Define the query to be performed to export desired data
SqlCommand command = new SqlCommand("select * from BHVUsers", connection);
SqlDataAdapter dataAdapter = new SqlDataAdapter(command);
dataAdapter.Fill(dataTable);
var excelApplication = new Excel.Application();
var excelWorkBook = excelApplication.Application.Workbooks.Add(Type.Missing);
DataColumnCollection dataColumnCollection = dataTable.Columns;
for (int i = 1; i <= dataTable.Rows.Count + 1; i++)
{
for (int j = 1; j <= dataTable.Columns.Count; j++)
{
if (i == 1)
excelApplication.Cells[i, j] = dataColumnCollection[j - 1].ToString();
else
excelApplication.Cells[i, j] = dataTable.Rows[i - 2][j - 1].ToString();
}
}
excelApplication.Cells[1, 2] = "Voornaam";
excelApplication.Cells[1, 3] = "Achternaam";
excelApplication.Cells[1, 4] = "Geboorte Datum";
excelApplication.Cells[1, 5] = "Porto nummer";
excelApplication.Cells[1, 6] = "EHBO behaald";
excelApplication.Cells[1, 7] = "EHBO";
excelApplication.Cells[1, 8] = "BHV";
excelApplication.Cells[1, 9] = "Regisseur";
excelApplication.Cells[1, 10] = "Verlopen EHBO";
excelApplication.Cells[1, 11] = "Laatste herhaling EHBO";
excelApplication.Cells[1, 12] = "EHBO nummer";
excelApplication.Cells[1, 13] = "Verdieping";
excelApplication.Cells[1, 14] = "Ruimte";
excelApplication.Cells[1, 15] = "Email";
excelApplication.Cells[1, 16] = "Mobiel";
excelApplication.Cells[1, 17] = "Opmerking";
excelApplication.Cells[1, 18] = "BHV_Behaald";
excelApplication.Cells[1, 19] = "BHV_Verlopen";
excelApplication.Cells[1, 20] = "Laatste_Herhaling_BHV";
excelApplication.Cells[1, 21] = "Ruisnaam";
excelApplication.Cells[1, 22] = "Regisseur_Verlopen";
excelApplication.Cells[1, 23] = "Regisseur_Behaald";
excelApplication.Cells[1, 24] = "Regisseur_Herhaling";
excelApplication.Cells[1, 25] = "Active_no_yes";
excelApplication.Cells[1, 2].Font.Bold = true;
excelApplication.Cells[1, 3].Font.Bold = true;
excelApplication.Cells[1, 4].Font.Bold = true;
excelApplication.Cells[1, 5].Font.Bold = true;
excelApplication.Cells[1, 6].Font.Bold = true;
excelApplication.Cells[1, 7].Font.Bold = true;
excelApplication.Cells[1, 8].Font.Bold = true;
excelApplication.Cells[1, 9].Font.Bold = true;
excelApplication.Cells[1, 10].Font.Bold = true;
excelApplication.Cells[1, 11].Font.Bold = true;
excelApplication.Cells[1, 12].Font.Bold = true;
excelApplication.Cells[1, 13].Font.Bold = true;
excelApplication.Cells[1, 14].Font.Bold = true;
excelApplication.Cells[1, 15].Font.Bold = true;
excelApplication.Cells[1, 16].Font.Bold = true;
excelApplication.Cells[1, 17].Font.Bold = true;
excelApplication.Cells[1, 18].Font.Bold = true;
excelApplication.Cells[1, 19].Font.Bold = true;
excelApplication.Cells[1, 20].Font.Bold = true;
excelApplication.Cells[1, 21].Font.Bold = true;
excelApplication.Cells[1, 22].Font.Bold = true;
excelApplication.Cells[1, 23].Font.Bold = true;
excelApplication.Cells[1, 24].Font.Bold = true;
excelApplication.Cells[1, 25].Font.Bold = true;
int k = 0;
for (k = 1; k <= 23; k++) // this will aply it form col 1 to 10
{
excelApplication.Columns[k].ColumnWidth = 30;
}
Excel.Range formatRange; formatRange = excelApplication.get_Range("a1", "y1");
formatRange.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.LightBlue);
//excelApplication.Cells[1,1] = "LightBlue";
formatRange = excelApplication.get_Range("a1", "y1");
formatRange.BorderAround(Excel.XlLineStyle.xlContinuous,
Excel.XlBorderWeight.xlMedium, Excel.XlColorIndex.xlColorIndexAutomatic, Excel.XlColorIndex.xlColorIndexAutomatic);
// Save the excel file at specified location
excelApplication.ActiveWorkbook.SaveCopyAs(@"C:\tmp\test1.xlsx");
excelApplication.ActiveWorkbook.Saved = true;
MessageBox.Show("Excel file created , you can find the file " + "c:\\tmp\\test1.xlsx");
System.Diagnostics.Process.Start("c:\\tmp\\test1.xlsx");
// Close the Excel Application
excelApplication.Quit();
connection.Close();
//Release or clear the COM object
releaseObject(excelWorkBook);
releaseObject(excelApplication);
}
Thanks
Regards,
Jan Meeling
modified 19-Aug-19 7:40am.
|
|
|
|
|
Where is the code that transforms the Date fields?
|
|
|
|
|
Hi Richard,
I was trying that with no results.
Do yoy have a solution how to do this.
Thanks,
Regards,
Jan Meeling
|
|
|
|
|
|