Tag name is not right - sql

Trying to learn SQL queries, I practice on the data explorer.
I try this query:
Select *
FROM Tags
WHERE TagName = "scala"
but have this error:
Invalid column name 'scala'
Any suggestion what is it going wrong?

use single quotes ('') instead of double quotes ("") for string constants ?

Tags must be a table of columns. Table which contain column TagName. And you are trying to find a record with 'scala' value in TagName column. But this column doesn't exist.
That all about the process. Now if you want some more help give us a context (examples).

Related

single quote in column name: SQL expression working fine in SQL Developer but failing in PowerBI

I do have a piece of SQL code that gave me some problems. The pivot operation results in a table with a column name called 'INITIAL' which has the single quotes in the name. But the values in that column are integers, or NULLs. I managed to access the numbers in SQL developer by enclosing the column name with double quotes: "'INITIAL'". When copying the code to PowerBI, it is not accepted. I returns a
Expression.SyntaxError: Token Comma expected
at the location of the first use of the single quote.
Any idea how to fix this for powerBI?
PIVOT (
SUM(conversion)
FOR PHASE
IN ('INITIAL')
))
SELECT REQUEST, SUM("'INITIAL'")
FROM Pivoted
WHERE 'INITIAL' IS NOT NULL
GROUP BY REQUEST
that nearly did the trick. I misread your post and used this:
SUM(""'02_INITIAL'"")
And that works in PowerBI!
So in SQL Developer I need to use SUM("'INITIAL'")
Best is fo course to avoid quotes in column names, but since my Pivot is based on string values, I get them for free.
Many thanks for your suggestion that put me on the right track!
To escape a character you need to use double quotation ("") mark. Please try this:
PIVOT (
SUM(conversion)
FOR PHASE
IN ('INITIAL')
))
SELECT REQUEST, SUM(""'INITIAL'"")
FROM Pivoted
WHERE 'INITIAL' IS NOT NULL
GROUP BY REQUEST

Unable to get a result that is in front of my eyes?

I have a database that uses fields of type VARBINARY(18) to store the primary keys
A sample PK: 0x001B7431C732005C4785A14F168EBD1FC5E4
When I try to run a simple query such as
SELECT * FROM mytable WHERE ID = '0x001B7431C732005C4785A14F168EBD1FC5E4'
I get no results, even though I can see the PK in mytable.
Does anyone know why this would be happening and how to fix it? I'm using SQL 2008 R2.
Thank you!
Don't use quotes.
That represents a string literal not a binary literal. The effect of it is an implicit cast of all the binary column data to string using the code page of your default collation. This will not match.
The below is all you need.
SELECT *
FROM mytable
WHERE ID = 0x001B7431C732005C4785A14F168EBD1FC5E4

sql attribute name

I have a table with five columns and there is an attribute with the name NoOfCreditCards. This attribute's data type is varchar with length 2. When the table is shown in the SQL command line the name of the NoOfCreditCards column appears only as No, the rest part of the name does not appear. I have tried to use "set lin" with different sizes, but it did not work. How can i resolve this issue?
You have to name the columns in your select statement.
Something like this:
SELECT NoOfCreditCards "NoOfCreditCards"
FROM TABLE
WHERE ...;

Replace NOT working!

I have column with values that have wrong character ?. Now I want to change it to character b. For this I am using this statement:
SELECT REPLACE(name,'?','b') from contacts;
But when I do this nothing's happening, it return value with ?.
What I am doing wrong? How I can replace this?
Are you actually trying to change the values in the table? In that case, you'll need to do an UPDATE:
UPDATE contacts
SET name = Replace(name,'?','b')
Otherwise, if you are simply trying to retrieve a modified value, your syntax should work just fine. (I tested it, ? doesn't have to be escaped or anything):
SELECT name, Replace(name,'?','b') as Fixed
FROM contacts
Another possibility that I've seen before is that the character looks like a regular old ASCII question mark but it's not really. It's actually a different character. I'd select the text and paste it into Notepad and then copy and paste it into my query.
If your name column data type is NVARCHAR you should use N prefix. NVARCHAR and VARCHAR types have unicode differance. Look at this link for more information about differance between NVARCHAR and VARCHAR types.
SELECT REPLACE(name,N'?', N'b') from contacts;
Try this
update contacts set name=replace(name, '?', 'b')

SQL query to extract text from a column and store it to a different column in the same record

I need some help with a SQL query...
I have a SQL table that holds in a column details of a form that has been submitted. I need to get a part of the text that is stored in that column and put it into a different column on the same row. The bit of text that I need to copy is always in the same position in the column.
Any help would be appreciated guys... my mind has gone blank :">
UPDATE mytable
SET other_column = SUBSTRING(column, begin_position, length)
You may just want to use a computed column. This way if the source string changes, your computed column is still correct. If you need to seek to this substring then you might want a persisted computed column if your db supports it.
UPDATE table
SET Column2 = SUBSTRING(Column1, startPos, length)
What if the value you wanted to copy was in a different position in each record, but always followed the same text?