SQL - Remove trailing " where it exists in column - sql

I have a SQL table that will be receiving new data daily. At times, the data in 3 of the 10 columns contains a trailing double quote ("). Is there an easy way to remove that final quote where it exists? Whatever the query or procedure is, I will be running it from either python or vba, depending on where this project goes in the next 2 weeks - but I think if I can get it to work from Microsoft SQL Server Mgmt Studio, I'll be able to modify for either one.

Try:
UPDATE table
SET column = left(column,length(column)-1)
WHERE column like '%"'

Related

SQL Server "n*" wildcard in contains problem

I have a problem with SQL Server contains.
I have 2 data in some table, the code of data is "CJK-7123" and "CJK-7890".
When I use SQL Server contains like this "contains((table.code),'"n*"')" in my select query, those of two data is still showing in my query, even though the code doesn't contain n character.

table with "." in its name

I was trying to use sqlFetch. The fetch works perfectly when I change the name of my table to have underlines instead of periods. So if I use the command
sqlFetch(conn, "HelloWorld_40")
It works fine. Unfortunately, my friends are all using the real name of the table
sqlFetch(conn, "HelloWorld.40")
But then it crashes and it tells me that
Error in sqlColumns(conn, "HelloWorld.40") :
'HelloWorld.40': table not found on channel
I'm guessing the period "." is illegal name for a table. But I don't want my friends to change it because it's a lot of people who would be affected. Is there a way I can call the table, or do I have to secretly go to their database, change the name while I use it and then change it back to a period (risking that I will forget, someone will read, blah blah).
Thanks.
put the table name in square brackets:
[HelloWorld.40]
It is a problem with sqlFetch which parse table name. Unfortunately it did not handle table quotes, so it's search for table 40 in schema HelloWorld. You need to directly call sqlQuery (with quoted table name, brackets for MS SQL Server):
sqlQuery(dbhandle, "SELECT * FROM [HelloWorld.40]")
Side note: you should specify which database you are using.
The best delimiter is double quotes -- that should work in most underlying databases:
"HelloWorld.40"
In MySQL, you can also use back ticks (`):
`HelloWorld.40`
In SQL Server, Access, and I think Sybase, you can also use square braces:
[HelloWorld.40]

SQL Left/Deliminated Character

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

Regex to split SQL script but ignore splitting GO under commented script

I'm trying to parse a sort of big SQL script containing commands like create functions and store procedures. I want to split the file in strings whenever I find a GO statement (I want to execute it using ordinary ado.net instead of SMO).
The problem is that I haven't find a suitable regular expression for that so far. Using a simple \bGO\b ignoring case will split it. But will also split all go(s) inside a comment like
/*****************************\
sql statement 1
GO
sql statement 2
GO
\****************************/
My requirement is : Do not split the script if it is under comment even though the script contains GO statement. Suppose my entire script is as below :
sql statement 1
GO
/*****************************\
sql statement 2
GO
sql statement 3
GO
\****************************/
sql statement 4
Expected output should be like
First command :
sql statement 1
Second command :
/*****************************\
sql statement 2
GO
sql statement 3
GO
\****************************/
sql statement 4
Have any idea on this ?
Thanks in advance. :)
You can remove all the comments and then split by GO:
/\/\**\\[^\\]*?\\\**\// # match the comment
\/\**\\ # matches /*****\
[^\\]*? # any text within the comments
\\\**\/ # matches \*****/
Remove above finds and split the result by GO.

Importing Data from .txt file to SQL Server 2005 Express

I have a tab separated .txt (Very Small file with just 10 to 15 datasets) and this file is having some columns as PrdName, PrdSize, PrdWeight, PrdCode and so on.
Now I want to import the two columns which are PrdSize and PrdCode and import it in the columns of my Database table.
I have created the columns but how do I create import clause and transfer data from .txt file to SQL Server? Thanks
Take a look at this post: Import/Export data with SQL Server 2005 Express, there are multiple options that you can use.
Since you have the express edition you'll need to either use BCP or write a program with something else.
If you have a large amount of data, or need to automate the process, definitely look into BCP as mentioned already. However, I often use excel to load one-time data sources (a few hundred to a few thousand) rows of data from odd sources into SQL Server by doing the following:
Get the data into excel (that's usually easy), assuming you get column A with 'Prdsize' and column B with PrdCode, in column C put the formula:
="INSERT INTO MYTABLE(PRDSIZE, PRODCODE) VALUES (" & a1 & "," & B1 & ")"
(in other words create syntactically correct SQL using an Excel formula - you may need to add quotes around string values etc)
and then paste that formula all the way down the column C. Then copy/paste the resultant sql insert statements into SQL Management Studio, or any other tool that can execute SQL and execute it.
Defintely a 'manual' effort, but for one-time data loads it words great.
PS: You'll need to verify the XL formula and the resultant sql syntax - my example is close, but I didn't test it.