update column data using access sql query - sql

I'm trying to update column data,i.e if column data contains '2u' or '3u' at end and replace with 'ui'. How do i achive this using the access sql query
sample table
col1 col2
562u yu3u

You might need to provide a bit more information but if I get what you're after you could use a SQL command like this:
UPDATE <tablename>
SET col1 = MID(col1, 1, LEN(col1) - 2) & 'ui'
WHERE col1 LIKE '*2u' or col1 LIKE '*3u'
You'll need to run the query for the second column separately:
UPDATE <tablename>
SET col2 = MID(col2, 1, LEN(col2) - 2) & 'ui'
WHERE col2 LIKE '*2u' or col2 LIKE '*3u'

Related

Convert each parameter to question mark for query statement

I mean from this:
SELECT * FROM my_tab WHERE col1 = 'name1' AND col2 in(1,2,3);
how to get this:
SELECT * FROM my_tab WHERE col1 = ? AND col2 in(?,?,?);
pg_stat_statements does similar but it not saves original(with parameters) statement. I need original query also.
May be there is some open source or regex expression, which does this?
If you have access to Python, you may try:
sql = "SELECT * FROM my_tab WHERE col1 = 'name1' AND col2 IN (1,2,3);"
output = re.sub(r'\bIN\s*\((.*?)\)', lambda m: 'IN (' + re.sub(r'\w+', '?', m.group(1)) + ')', sql)
print(output)
This prints:
SELECT * FROM my_tab WHERE col1 = 'name1' AND col2 IN (?,?,?);
You might be able to do this for specific queries, but a general solution for this question would require parsing the SQL statement, which is complicated, to say the least.
Try to find a different solution for the problem underlying your question.

Update the column format from string to date in Hive

I've an external table in Hive
Current:
col1 - string
col2 - string
col3 - string
col4 - float
col5 - int
I want to change the date type of col3 to date
Expected:
col1 - string
col2 - string
col3 - date
col4 - float
col5 - int
I tried regular sql command but not useful
alter table table_name modify col3 date;
Error:
Error while compiling statement: FAILED: ParseException line 1:32 cannot recognize input near 'modify' 'col3' 'date' in alter table statement
Requesting assistance here. Thanks in advance.
Correct command is:
alter table table_name change col3 col3 date;
The column change command will only modify Hive's metadata, and will
not modify data. Users should make sure the actual data layout of the
table/partition conforms with the metadata definition.
See syntax and manual here: Change Column Name/Type/Position/Comment

MSSQL Server 2008 - Convert Nvarchar to numeric

im using mssql 2008 and Im permanently failing to convert an nvarchar to numeric values.
Can you please advise? I have different solutions I found over the www, but all of them are failing with the error message:
Msg 8114, Level 16, State 5, Line 15 Error converting data type
nvarchar to numeric.
I have built an reduced example for demonstration purpose:
IF OBJECT_ID('tempdb..#temptable', 'U') IS NOT NULL
DROP TABLE dbo.#temptable
create table #temptable(
col1 nvarchar(10),
col2 numeric(10,5)
)
insert into #temptable values ('0,5','0')
select *,convert(numeric(18,2),col1) from #temptable
UPDATE #temptable
SET col2 = CAST(col1 AS numeric(10,5))
WHERE ISNUMERIC(col1) = 1
SELECT col1
, CASE ISNUMERIC(col1)
WHEN 1 THEN CONVERT(numeric(18,2),col1)
ELSE 0.00
END
from #temptable
I alreay found an strong hint whats going wrong... the issue seems to be related to the , as seperator while the SQL server expects an .
if you change the following line to:
insert into #temptable values ('0.5','0')
its working
The problem is you are using ISNUMERIC(col1) = 1 which fails for a ton of cases like ISNUMERIC('1e4') or ISNUMERIC('$') or in your case, ISNUMERIC('1,000,000'). Don't use ISNUMERIC in this fashion.
Instead, try this...
UPDATE #temptable
SET col2 = CAST(col1 AS numeric(10,5))
WHERE col1 not like '%[^0-9.]%'
Use try_convert() in SQL Server 2012+:
UPDATE #temptable
SET col2 = TRY_CONVERT(numeric(10,5), col1)
WHERE ISNUMERIC(col1) = 1;
SQL Server re-arranges expression valuation in a query. So, the CAST() might be implemented before the WHERE -- resulting in the error. You can make a similar change to the SELECT version of your query.
In SQL Server 2008, you should be able to do effectively the same thing using CASE:
UPDATE #temptable
SET col2 = (CASE WHEN ISNUMERIC(col1) = 1 THEN CONVERT(numeric(10, 5), col1) END)
WHERE ISNUMERIC(col1) = 1;
Note: There may be cases where ISNUMERIC() returns "1", but the value cannot be converted (for instance, overflow). In that case, this version would still fail.

Referring to column values directly without using variables in T-SQL

Is there a way in T-SQL (SQL Server 2005) to assign a whole record to a record variable and then refer to the particular values using column names?
I mean, instead of:
select #var1 = col1,
#var2 = col2
from mytable
where ID = 1;
and referring to them as #var1 and #var2, something like
#record =
select col1, col2
from mytable
where ID = 1;
and referring to them like #record.col1 and #record.col2 .
I am beginner in t-sql, so hopefully the question is not too trivial.
You can create a table variable and select the whole resultset into it:
DECLARE #tt TABLE (col1 INT, col2 INT)
INSERT
INTO #tt
SELECT col1, col2
FROM mytable
WHERE id = 1
, but you cannot access its data except than in the SELECT query as well.
With pure TSQL (that it without custom datatypes) the thing you ask is impossible.
sounds like you are a programmer ... look at linq maybe as it does what you want.
You can use a temporary table and SELECT...INTO to avoid specifying the column names at the beginning :
SELECT Field1, Field2
INTO #TempTable
FROM MyTable
WHERE MyTable.MyID = 1
but of course you'll still need the FROM #TempTable part when referring to the column names.
SELECT Field1, Field2
FROM #TempTable
and of course to remember to drop the table at the end :
DROP #TempTable
The app code is where you'd normally refer to a single row at a time as a variable.
You could use XML, but you'd have to play with this...
DECLARE #MyRecord xml
DECLARE #Mytable TABLE (col1 int NOT NULL, col2 varchar(30) NOT NULL)
INSERT #Mytable (col1, col2) VALUES (1, 'bob')
select #MyRecord =
(SELECT *
from #Mytable
where col1 = 1
FOR XML AUTO)
SELECT #myRecord.value('./#col', 'int') --also #myRecord.value('#col', 'int')
--gives error
Msg 2390, Level 16, State 1, Line 12
XQuery [value()]: Top-level attribute nodes are not supported
Buried in the Transact SQL documentation I came across this restriction on variables:
Variables can be used only in expressions, not in place of object names or keywords.
Since you'd need to use an object name to qualify a column I don't believe that this is allowed.

MSSQL Server - get a whole part of a decimal value in the computed column

Here's my simplified table (SQL Server 2005):
table1: col1 int, col2 int, col3 cast(col1/col2 as int) [computed column]
for some reason the above doesn't work. i just want to save a WHOLE part of col1/col2, how do i do that?
example: col1 = 5, col2 = 3 ... col3 should be 1
One option would be to use the floor function:
FLOOR(col1/col2)
or
CONVERT(int, FLOOR(col1/col2)) -- Might be overkill