Adding spaces before a value? [closed] - sql

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I need to add spaces before a column in SQL.
For example, I have a column with a value "00:99:88:aa". I need to add 3 spaces before the value to have the value " 00:99:88:aa". How would I do this?

You need to apply string concatenation.
For MySQL and MariaDB
SELECT concat(' ', '00:99:88:aa') FROM ...
or in the event of an update
UPDATE ...
SET value = concat(' ', value)
For SQL Server
SELECT ' ' + '00:99:88:aa' FROM ...
or in the event of an update
UPDATE ...
SET value = ' ' + value
For MS Access
SELECT ' ' & '00:99:88:aa' FROM ...
or in the event of an update
UPDATE ...
SET value = ' ' & value
For all the others
SELECT ' ' || '00:99:88:aa' FROM ...
or in the event of an update
UPDATE ...
SET value = ' ' || value

create a new column
alter table <table_name>
add <temp_column> varchar(10)
Update this new column with three space string
update <table_name> set <temp_column> = " "
concatenate the two columns
select concat(temp_column,column1) from <table_name>
OR add a new column with this value

ON MySql TRY:
UPDATE `table` SET `column` = CONCAT(" ", `column`);

Related

how to fill down when empty cells in access db [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
db access table
kindly i need help to fill down with previous string PS: i am a beginner
Your Date field seems to be of data type Text. If so, change that to DateTime.
Then you can run a loop to update the records having no date value:
Dim Records As DAO.Recordset
Dim Sql As String
Dim LastDate As Date
Sql = "Select * From YourTable Order By [Surevey ID]"
Set Records = CurrentDb.OpenRecordset(Sql)
If Records.RecordCount > 0 Then
LastDate = Date ' Adjust if needed.
Records.MoveFirst
While Not Records.EOF
If IsNull(Records!Date.Value) Then
Records.Edit
Records!Date.Value = LastDate
Records.Update
Else
LastDate = Records!Date.Value
End If
Records.MoveNext
Wend
End If
Records.Close

Using Sequel Pro: How do I update a column that contains a query?

So basically I have a column called 'query' which contains a query. I need to update that query but don't know how.
I tried a simple
UPDATE TABLE
SET QUERY = 'New Query'
WHERE ID = 1
but it's getting thrown out because of the pre-existing ' ' that I have in the query.
Way out of my league on this one. Trying to learn on the fly for a position that's going to open up.
You'll have to escape the ' in the query, possibly by doubling them up or using an escape char before them:
UPDATE t
SET query = 'DELETE FROM x WHERE y = ''z'' '
WHERE ID = 1
UPDATE t
SET query = 'DELETE FROM x WHERE y = \'z\' ' ESCAPE '\'
WHERE ID = 1
You can also make life easier/neater with REPLACE, like:
UPDATE t
SET query = REPLACE('DELETE FROM x WHERE y = "z" AND a = "b" AND c = "d" ', '"', CHAR(39))
This way you can use some char all through that doesn't occur naturally in the query, and replace it with ' at the end
I can't say which variant works for your DB because you didn't say what your DB was..
WHERE ID = 1

order by charindex >0 and SQL concat [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I want to order Recordset by setting priority when ID is in an array:
This is the comma separated myString which holds a series of ID: ,1,2,4,6,8,12,34,
I use concat to add comma before and after ID so I can compare it against the string:
sql="select * from customers order by case when charindex( concat(','," & id & ",',') , '" & myString & "' )>0 then 1 else 0 end desc"
I get syntax error message. I am not sure if there is error in SQL structure as I used concat in deep structure or there is error in using quote and double quote?
I found the problem when trying to answer #Diado Comment. The parameter ID should be used in SQL context not as a variable in VbScript context. So I have changed the query to this and it works:
sql="select * from customers order by case when charindex( concat(',',id,',') , '" & myString & "' )>0 then 1 else 0 end desc"

SQL Update, Set value has ' (single quote) and '' Double quote How do i proceed?

I'm doing an update but the value has ' and " quotes,
How do I proceed?
This is how my query looks like
Update Users
set info = '[user{"LinkAction('togglemore', 'companies','jobs')","IsVisible":true}]'
where user like '%admin%'
How do i make the SET to consider everything in the ' single quote '
You can use two single quote ''
Update Users
set info = '[user{"LinkAction(''togglemore'', ''companies'',''jobs'')","IsVisible":true}]'
where user like '%admin%'
sqlfiddle : http://sqlfiddle.com/#!18/402e8/1

Sql table with columns that can store arraylist [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have a parameters that contain 6 or more number and can be input with textbox
so for each parameter, put the numbers in a arraylist
like this:
Dim a As New ArrayList
a.Add(Val(B1_Left.Text))
a.Add(Val(B1_Down.Text))
a.Add(Val(B1_Right.Text))
a.Add(Val(BB1_Left.Text))
a.Add(Val(BB1_Down.Text))
a.Add(Val(BB11_Right.Text))
so for each record i have an arraylist parameter that should be insert the row of one column. Which column type should i apply in sql server table to store a vb.net arraylist?
my vb.net code for insert to database:
Dim DBConnection As New SqlConnection(My.Settings.CS)
Dim DBCommand As New SqlCommand
Dim _PID As Integer
Dim _Date As Date
Dim _txt As String
Dim _B1 As ArrayList = a.clone
DBCommand.Connection = DBConnection
DBCommand.CommandText = "insert into TOM_ShaftFix " &
" ( PID, [Date], txt, B1) " &
" VALUES(#PID,#Date,#txt,#B1)"
DBCommand.Parameters.AddWithValue("#pid", _PID)
DBCommand.Parameters.AddWithValue("#Date", _Date)
DBCommand.Parameters.AddWithValue("#txt", _txt)
DBCommand.Parameters.AddWithValue("#B1", _B1)
DBConnection.Open()
DBCommand.ExecuteNonQuery()
DBConnection.Close()
Depending on the version of SQL Server you could store the data as JSON.
In SQL 2016 it is supported natively, alternativelly you could store it as XML.
declare #country nvarchar(max) = '{
"id" : 101,
"name": "United States",
"continent": "North America"
}';
INSERT INTO Countries
SELECT * FROM OPENJSON(#country)
WITH (id int,
name nvarchar(100),
continent nvarchar(100))
MSDN
Depending on the type you could simply store it as comma seperated values.
1,2,3,4,5,6