I need to move around 12000 documents from one directory to another using PowerShell, however, I also need to rename each document based upon a SQL query which strips out any spaces, removes the file extension etc.
I've written the SQL for it (as shown below), but I'm not sure how/if it's possible to do the above using PS?
'Author' + CAST(AuthorId as varchar) + '_' + REPLACE(REVERSE(SUBSTRING(REVERSE(originalFilename), CHARINDEX('.', REVERSE(originalFilename)) + 1, 999)), ' ','')
Thanks for any help
Related
I'm trying to back up all the tables in a SQL Server Database. Here's the logic I'd like to execute:
For Each TableName in MyDatabase
SELECT *
FROM TableName
Export Results to C:\TableName.csv
Loop
*Disclaimer that none of these will ever be a better option than a native backup in terms of backing of data. I'm assuming you simply want some flat file backups.
Several manual options are available from generating the scripts and data via Tasks->Generate Scripts, to manually running a select * from tablename and then right clicking -> Copy including header columns into their own excel files.
More automated options involve SSIS packages to handle this sort of enumerator logic. I believe that the best way to handle this would be a powershell script. Please see #Smor's comment to the following answer: SQL Server Management Studio 2012 - Export all tables of database as csv , for a detailed powershell solution.
The only T-SQL based querying options I'm aware of would be to attempt something of the following:
SELECT
'insert into OPENROWSET(''Microsoft.Jet.OLEDB.4.0'',
''Excel 8.0;Database=#filepath\'' + t.name + '.csv '', --#filepath would be replaced with your logic for determining filename+path
''SELECT * FROM [SheetName$]'')
select * from ' + t.name + ';'
FROM
sys.tables
And executing the results of this script.
I have searched for an answer on this, but have come up short. When exporting a table from SSMS, from time to time some of the larger files will have issues where something like an address will trigger a CR/LF and spread a record like an address across multiple rows in the exported CSV file. If I copy an paste the record directly from SSMS and paste it into another program such as word, it will do the same thing. I cant exactly put my thumb down on what is happening here. Other records will export correctly, and then all the sudden one of the records will come up like below...
Looks something like this:
1|"Apartment Katha
2|Flat No 9999 Garia Place
3|West Bengal"
But I need it like this
1|"Apartment Katha Flat No 9999 Garia Place West Bengal"
I use Unicode and " as the text qualifier.
CR and LF in SQL server are Char(10) and Char(13) Try to take out these characters from your records, and see if it works:
SELECT REPLACE(REPLACE(#str, CHAR(13), ' '), CHAR(10), ' ')
I am new to SQL and just know basic insert, update, and delete syntax.
I have an excel file, that I imported into the SQL server, but somehow, it brought in weird symbols and characters.
When I checked the excel file ,cleared all formatting, and re-uploaded, it would still show up, not sure how to clean it up.
Is there an easy replace syntax that you can suggest for me to use to do a global cleanup?
Sample values inside the columns are:
Lisa Hettinger  Cherry Creek Prop
Lisa J Hernandez
I would need to remove the weird ┬ and á characters.
If all you have are the " " characters, you can try using the REPLACE command like this:
SELECT REPLACE(N'Lisa J Hernandez', N' ', N' ')
I suspect the source code page has " " as its space character.
Update: To update the values in the entire column, you can use
UPDATE [MyTableName] SET [MyColName] = REPLACE([MyColName], N' ', N' ');
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
When exporting some data from MS SQL Server using Python, I found out that some of my data looked like computer \xa0systems which is causing encoding errors. Using SQL Management Studio the row simply appears to be double spaced: computer systems. It seems that this is the code for : how can I query MS SQL Server within management studio to find all instances of this? things like WHERE ColumnName LIKE % % are not working, nor are querying on nbsp; or \\xa0
I used WHERE ColumnName LIKE '%' + CHAR(160) + '%'
160 is the ascii for "non breaking space".. which is 0xA0 in hex as per your \xao