Format TSQL onto one line - sql

Our company uses an old app which reads TSQL from a .INI file. Due to how the app process the INI file the TSQL has to be all on one line. I use Poor Mans TSQL Formatter to get everything nice and tidy for things like SPs, but am wondering if there's something out there to do the reverse - take nicely formatted TSQL and shove it all onto one line (removing carriage returns , line breaks etc).
I'm working in SSMS but also use Notepad++, and will happily use some other editor if it has the functionality.

Using Notepad++ (Without any plugin)
After lot's of googling I found that there are no plugins like TextFX and PoorMansTSqlFormatter are available in x64 bit version of notepad++ even not needed.
Notepad++ --> Write Query --> Edit --> Blank Options --> Remove Unnecessary Blank and EOL.
That's it.

Using Notepad++
Select the statement that is over multiple lines then on the menu: TextFX>TextFX Edit>Unwrap Text
And for even greater ease you can assign it to a keyboard shortcut using the shortcut mapper (Settings>Shortcut Mapper)

You can use the minify comment to remove all the unnecessary space in the Poor Mans TSQL Formatter
[minify]
[/minify]

I like Martin's answer and that is probably the way to go. But I'll point out that you can just use string manipulation to turn carriage returns and line feeds into spaces. This is particularly easy in the later versions of SSMS that enable limted use of regex in the find/replace dialog.

In SQL Server 2012:
Highlight the selected text and use 'Find and Replace' (ctrl + h)
Check: use Regular Expressions
Find: \n
Replace with: LEAVE BLANK

Related

Intellij: how to convert indents for all files

I know how to convert indent for a single file. I go to edit -> convert indent -> space/tab.
But I want to do this for all files under a directory.
I try click on a directory and then go to edit -> convert indent, but the options are grayed out.
You can use the shortcut Ctrl+ALT+L (Windows/Linux) or ⌥⌘+L (MAC OS X) and select the Rearrange entries option to reformat the code in the current file or reformat a module or directory.
You can also Right-click a module, file, or directory from the context menu and select Reformat Code and also select the Rearrange entries option.
This will convert the indents for all files/directories selected:
This works on most of the Jetbrains IDES (iDea, PyCharm, WebStorm, RubyMine, and so on.)
It seems there is no such dedicated option in IntelliJ, but you could just work around it using a "low-level" Replace All action.
Open the Edit → Find → Replace in Files... dialog
In case you want to convert spaces to tabs, you should
Enter in the Find field (i.e. four spaces (or whatever number of spaces the project is currently indented with))
Press the Regex search modifier (Alt + X)
Enter \t in the Replace field
NB: In case you have valid strings with 4+ spaces in them, they will get replaced too. In most use cases, however, this is not happening.
In case you want to convert tabs to spaces, you should do the same as above, but swap the Find and Replace field contents
NB: Again, if you have valid strings with tabs in them, they will get replaced too. I haven't had this use case, because I've only needed to convert in the opposite direction.
You will probably also want to set a File mask in order not to replace spaces in code-irrelevant files

escaping xml in intellij 14

in Intellij, I'm looking for a single keystroke method to transform escaped XML to XML and vice versa I've looked at macros with a file find/replace but they are not doing what I need - any suggestions?
e.g.
<Alpha>
<Beta>3030</Beta>
<Beta>3030</Beta>
</Alpha>
TO
<Alpha>
<Beta>3030</Beta>
<Beta>3030</Beta>
</Alpha>
Take a look at this plugin, it can do what you want and much more in terms of escaping and string manipulation in general.
You can bind some custom shortcut to Escape XML and Unescape XML actions. Or you can hit SHIFT+ALT+M and then 8 to escape or SHIFT+ALT+M and then 9 to unescape by default.
It's probably not the exact solution you're looking for, but hopefully it will help.

Intellij - Reformat Code - Insert whitespace between // and the comment-text?

I am working with another human being on project from that the professor expects to have uniform code-style. We have written large separate junks of code on our own, in which one has written single line comments without a white-space between the single-line-comment-token and the other one has inserted a white-space. We are working with IntelliJ and have failed to find an option to enable the Reformat Code function, to insert a white-space.
TLDR:
Can you tell us how to convert comments from that to this in IntelliJ?
// This is a load bearing comment - don't dare to remove it
//This is a load bearing comment - don't dare to remove it!
You can do a global search and replace (ctrl-shift-r on windows with default keyboard layout, or Replace in Path under the Edit/Find menu).
Check the regular expression option and enter //(\S.*) as the text to find and // $1 as the replacement. Check the whole project option, and clear any file masks. You can single step through the replacements, or simply hit the All Files option.

Mgmt Studio save results as issue

I have a SQL query that returns 1 column. I run it in SQL Server Management Studio 2008R2.
I use File\Save Grid Results and create a .TXT file.
My problem is the first record of the file has 3 bytes inserted in front of the data. They three bytes are x'EFBBBF'. This causes problem when I use the file in another process.
I get the same thing wheter I save as .TXT or as .CSV.
Any ideas?
Found it.
Save Results As...
Choose a folder
Enter a file name
Save button now has a dropdown arrow to the right
Click on dropdown arrow and select Save with Encoding...
Select ANSI
Click OK
The ANSI-encoded file will not contain a UTF-8 BOM.
kuru kuru na is on the right track, those bytes are the UTF-8 BOM. I haven't found any settings to change the file encoding that Management Studio uses for saving results. I just use Vim to remove the BOM after saving the file. Your favorite text editor may have a similar option, or you could use a tool like iconv if you need to remove the mark or re-encode the file in a script.
I think it's called a "bom" (byte order mark) signature, which has something to do with telling whatever reads your file that it contains utf characters. I suspect it might be in your SMSS settings somewhere. But at least this is a place to start.

vb.net VB 2010 Underscore and small rectangles in string outputs?

I've made some good progress with my first attempt at a program, but have hit another road block. I'm taking standard output (as a string) froma console CMD window (results of dsquery piped to dsget) and have found small rectangles in the output. I tried using Regex to clean the little bastards but it seems they are related to the _ (underscore), which I need to keep (to return 2000/NT logins). Odd thing is - when I copy the caharcter and paste it into VS2K10 Express it acts like a carrige return??
Any ideas on finding out what these little SOB's are -- and how to remove them?
Going to try using /U or /A CMD switch next..
The square is often just used whenever a character is not displayable. The character could very well be a CR. You can use a Regular Expression to just get normal characters or remove the CR LF characters using string.replace.
You mentioned that you are using the string.replace function, and I am wondering if you are replacing the wrong character or something like that. If all your trying to do is remove a carriage return I would skip the regular expressions and stick with the string.replace.
Something like this should work...
strInputString = strInputString.replace(chr(13), "")
If not could you post a line or two of code.
On a side note, this might give some other examples....
Character replacement in strings in VB.NET