Current date UPDATE statement not working in SQL Server 2012 - sql

I'm trying to update a column named dattime that is a date column (not a datetime column like the name would lead you to believe) in my table named pr-pre-a with the current date. I'm using SQL Server 2012 and when I use:
UPDATE pr-pre-a
SET [dattime] = getdate()
the getdate() is not bolded meaning it is not a recognized command, and when I try to run it, it tells me there is a syntax error. however when I use:
UPDATE pr-pre-a
SET [dattime] = current_timestamp
it is bolded, but it still says there is a syntax error. What do I need to change to get this to work?

May be you need to escape - in your table name using []
UPDATE [pr-pre-a]
SET [dattime] = getdate()

Have you tried wrapping your table name in []?
I don't think SQL Server likes hyphens in table names without the square brackets or double apostrophes.

Related

Error while trying to mask a column in SSMS

I'm trying to mask some information in my table, when I use this :
ALTER TABLE Person
ALTER COLUMN DoB add masked with (Function = 'default()')
I get those two error :
Incorrect syntax near the word 'masked'
and
Incorrect syntax near the keyword 'with'. If the statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon.
That's literally the same code as the Microsoft documentation, but for some reason it doesn't work
If somebody could explain me what is the problem, that would help me a lot
To mask a column using the ALTER statement the datatype of the column must be included in this statement. The ADD keyword is also not necessary. I don't know what the datatype of the DoB column from your example is but it will need to be changed as below. From the name I'm guessing this is a datetime column, but if it's not the matching datatype will need to be substituted in.
ALTER TABLE Person ALTER COLUMN DoB DATETIME MASKED WITH (FUNCTION = 'default()')
Update:
This error was a result of using SQL Server 2012 when this feature was released in SQL Server 2016.

Error show when update using phpmyadmin

When I update a column using phpmyadmin in database with following query
UPDATE members
SET `refered` = (SELECT COUNT (*)
FROM `user_details`
WHERE `user_details.sponser`=`members.username`
)
It show a error message like this
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '*) FROM `user_details` WHERE `user_details.sponser`=`members.username`)' at line 1
What may be reason?
Error is in
COUNT (*)
-----^
Remove the space between COUNT and (*).
Try the below query
UPDATE members SET refered = (SELECT COUNT(*) FROM user_details
WHERE user_details.sponser=members.username)
Does the Select query returns any result. If so what is the result. Looks like all your query is inside '' single quotes that you are using it should be removed. Single quotes need to be removed for example .
UPDATE members
SET refered = (SELECT COUNT (*)
FROM user_details
WHERE user_details.sponser=members.username
)
-- there is not single quotes in the query above. please remove it from yours.
Part of your problem, or maybe the whole problem, is the WHERE clause. You've used backticks for the table name, which is correct (or, at least, it's optional in this case; it's needed if your database name or table name has a MySQL reserved name or is otherwise ambiguous). The problem, though, is that the dot separating the database from the table needs to be outside the backticks. So your WHERE clause should look like this instead:
WHERE `user_details`.`sponser`=`members`.`username`

SQL - UPDATE / SET statement query

I wanted to update 1 column from a table present in a database maintained in SQL 2008 R2.
The entire column contains same value of data type int and I want to change to different value of same int data type.
When do we use single apostrophe in SET statement & when to avoid or not use it?
UPDATE TABLENAME
SET COLUMNNAME = X
OR
UPDATE TABLENAME
SET COLUMNNAME = 'X'
Appreciate any suggestions/advise. Thanks.
In SQL, single quotes are used around string values. Other "values", like integers, references to other columns etc, should never be single quoted.

update a part of a varchar column in sybase

I have a column in a table which has value
121Z000CH YY03+ W
is it possible using sybase query language to update this column only in the characters 14 to 2 using substring? i.e. i need to replace 03 with some other number say 05.
Currently i'm trying the below statement which is giving me syntax error. Please correct me in this.
update recordtable
set substring(recordtable.column,14,2) = '05'
you can use below query for the same -
update recordtable
set column = substring(recordtable.column,1,13)
||'05'||substring(recordtable.column,16)

error while using replace in an update statement in sql server 2008

I'm getting error while using replace in an update statement in sql server 2008.
the statement I'm trying to run is :
update table US14-HSS-SQUARE_AISC14-HSS-SQUARE set Designation = replace(Designation,'HSSSQUARE','HSS') where Designation like 'HSSSQUARE%';
want to change 'HSSSQUARE' to 'HSS' in each row of the column 'Designation'. e.g.
'HSSSQUARE16X16X5/8' to 'HSS16X16X5/8'.
is there any other syntax in sql server 2008?
You don't need table in the update statement, and you also need to wrap your table name in square brackets if you are going to put illegal characters in it.
UPDATE [US14-HSS-SQUARE_AISC14-HSS-SQUARE]
SET Designation = REPLACE(Designation,'HSSSQUARE','HSS')
WHERE Designation like 'HSSSQUARE%'