I am trying to update/replace column values in the table with hard coded value.
value = "c:\temp\"
This:
COLUMN
file.txt
file1.txt
Should become this:
FINAL COLUMN
c:\temp\file.txt
c:\temp\file1.txt
Attempted solution:
SELECT REPLACE(t.column, t.column, 'c:\temp't.column)
FROM TABLE t
Is this correct logic? Do we have another function I can use?
Assuming Oracle:
If you want to change the values in the table permanently you can just run an update query:
update your_table
set your_column = 'C:\temp\' || your_column;
Sample SQL Fiddle
If you're using MS SQL you can do concatenation like this:
MS SQL (all versions?):
update your_table
set your_column = 'C:\temp\' + your_column;
MS SQL 2012 and later:
update your_table
set your_column = concat('C:\temp\',your_column);
Do a UPDATE statement like below in case you want to change it permanently
update table1 set [column] = 'c:\temp\' + [column];
Else, if you just want to display it that way then SELECT query should be
select 'c:\temp\' + [column] as new_col
from table1
NOTE: above code syntax is for SQL Server. Not sure since you tagged as tsql
Related
I have a column as int and it stores year like 2017.
How can I convert this to save the result to another actual datetime column like 2017-01-01?
DATEFROMPARTS is a new SQL Server function, (from SQL Server 2012), that allows to build a date value using its parts: Year, Month, Day.
Have a look at DATEFROMPARTS at MS Docs.
UPDATE TableName
SET <UpdColName> = DATEFROMPARTS(<IntColName>,1,1)
WHERE <some condition>
Use the function DATEFROMPARTS.
select datefromparts(colname,1,1)
from tablename
Try using DATEFROMPARTS,
UPDATE table SET coldatetime= datefromparts(colname,1,1) WHERE colname = 2017
This is complicated. First, convert the column to a varchar():
alter table t alter column col varchar(255);
When you do this, the integer will be converted to a varchar().
Next, append '0101' to the value:
update t
set col = col + '0101';
This puts the value in the form 'YYYYMMDD', which SQL Server recognizes as a date.
Finally, alter to a date:
alter table t alter column col date;
If you like, you can add another column to the table for the date and do this in two steps:
alter table t add column datecol date;
update t
set datecol = concat(intcol, '0101');
Although you can also use datefromparts(), the above should work in earlier versions of SQL Server as well.
i have an SQL database with a field which is filled by now() function.
i want to update the present data only with date part of it.
Example :
Present Data :
20.08.2015 13:10:31
21.08.2015 14:00:29
22.08.2015 05:55:42
Target
20.08.2015
21.08.2015
22.08.2015
thanks,
Assuming you are using the MySQL database, you can use date function
update yourtable
set yourColumnName = date(yourColumnName)
In SQL Server try using the Convert function
update yourtable
set yourColumnName =CONVERT(date, yourColumnName)
or
update yourtable
set yourColumnName CONVERT(varchar(10),yourColumnName,104)
or else you can use the LEFT function like
update yourtable
set yourColumnName =LEFT (yourColumnName, 10)
SQL FIDDLE
Assuming the data is in a table, you would do something like:
update t
set col = cast(col as date);
The exact syntax for converting to a date varies by database. It might also be:
date(col)
date_trunc('day', col)
trunc(col)
I have the following value format :
1234567890
I want to convert it to the following format : 123-45A67890
Table Name: Test
Column Name : MyCode
Note that I am using Microsoft SQL 2012.
You can use STUFF . Something like this
DECLARE #v VARCHAR(15) = '1234567890'
SELECT STUFF(STUFF(#v,4,0,'-'),7,0,'A')
Your SELECT would be
SELECT STUFF(STUFF(MyCode,4,0,'-'),7,0,'A')
FROM Test
Your UPDATE would be
UPDATE Test
SET MyCode = STUFF(STUFF(MyCode,4,0,'-'),7,0,'A')
Does SQL Server support the PASTE function?
paste(paste('1234567890',7,0,'A'),4,0,'-')
For example:
select paste(paste(column_name,7,0,'A'),4,0,'-') from table_name
or
update table_name set column_name = paste(paste(column_name,7,0,'A'),4,0,'-')
Here what tried with a variable #a:
declare #a varchar(100)='1234567890'
select STUFF(STUFF(#a,4,0,'-'),7,0,'A') --gives--> '123-45A67890'
Likely you can use it to update your table, which
Adds Hyphen (-) after first 3rd character,
Adds letter 'A' after 2 letters just after hyphen...
update Test set MyCode = STUFF(STUFF(MyCode,7,0,'A'),4,0,'-')
More about STUFF() function in SQL
How do I achieve the following by a stored procedure or sql script in sql server?
Say for example, I have a-b in one of the columns and I need to update that column to a - b instead.
How do I insert spaces before and after the dash in existing data in my database.
UPDATE YourTable
SET YourColumn = REPLACE(YourColumn, '-', ' - ')
update TableName set ColumnName = Replace(ColumnName, '-', ' - ') where SomeCondition ...
I am trying to run an update query which will strip - from a field. For example:
ID NAME
------------
1 you-me
On updating, I want to strip the "-". The result should be:
ID NAME
------------
1 youme
Is there any sql command to do this? I am using DB2.
A straight REPLACE function then?
update tbl set col = REPLACE(col, '-', '')
You can just use SQL REPLACE function
UPDATE table
SET column = REPLACE(column, '-', '');
See this documentation