I want to remove blank values commas(first,last and consecutive empty commas) in the below table.My table contains below values
x
---
,1,2,3,
4,5,6
7,8,,,9
,10,11
I want the below result.
O/P
---
1,2,3
4,5,6
7,8,9
10,11
CREATE TYPE TableVariable AS TABLE
(
id int identity(1,1),
field_ids INT,
value VARCHAR(MAX)
)
inserting values in this type
DECLARE #DepartmentTVP AS TableVariable;
insert into #DepartmentTVP values(1994,',85574,,,85538,')
when i select from this type i want to remove this commas(first,last and consecutive commas in the value)
Related
I have calculated a list of values from an equation and I want to print all 140 of the output values in a single column, so that I can convert it to a txt document with one column of data. When I say print(values), it prints the output in multiple columns.
For example:
N = [1,2,3,4,5]
print(N)
This is the result: [1, 2, 3, 4, 5]
I want these values in a single column.
l=[1,2,3,4] #let this be the list
for i in range(len(l)):
print(l[i],"/n")
N = [1,2,3,4,5] #your list here
for i in range(0,len(N)):
print(N[i],"\n")
Just rmb to use backslash in "\n" to go to next line instead of "/n".
I am trying to take a list of points, and query a geospatial database, to find all matching rows.
I have a computed SQL statement that looks like this:
cursor = connection.cursor()
cursor.execute(
'''
SELECT g.ident
FROM (VALUES %s) AS v (lon, lat)
LEFT JOIN customers g
ON (ST_Within(ST_SetSRID(ST_MakePoint(v.lon, v.lat), %s), g.poly_home));
''', [AsIs(formatted_points), SRID]
)
Here is an example of what the formatted_points variable looks like:
(-115.062,38.485), (-96.295,43.771)
So, when that is inserted into the SQL expression, then VALUES expression reads:
(VALUES (-115.062,38.485), (-96.295,43.771)) AS v (lon, lat)
So far so good. However, when the list of points is empty, the VALUES expression looks like this:
(VALUES ) AS v (lon, lat)
.. which causes me to get this error:
django.db.utils.ProgrammingError: syntax error at or near ")"
In other words, (VALUES ) is not legal SQL.
Here's the question: How do I represent an empty list using VALUES? I could special case this, and just return an empty list when this function is passed an empty list, but that doesn't seem very elegant.
I have looked at the PostgreSQL manual page for VALUES, but I don't understand how to construct an empty VALUES expression.
If you can put your lons and lats in separate arrays, you could use arrays with unnest:
select * from unnest(ARRAY[1, 2, 3]::int[], ARRAY[4, 5, 6]::int[]) as v(lon, lat);
lon | lat
-----+-----
1 | 4
2 | 5
3 | 6
(3 rows)
select * from unnest(ARRAY[]::int[], ARRAY[]::int[]) as v(lon, lat);
lon | lat
-----+-----
(0 rows)
You'll have to cast the arrays to the appropriate type (probably not int[]). Postgres will guess the type if the arrays aren't empty, but it will throw an error if they are empty and you don't cast them to a specific type.
Example 1
asdk[wovkd'vk'psacxu5=205478499|205477661zamd;amd;a;d
Example 2
sadlmdlmdadsldu5=205478499|205477661|234567899amsd/samdamd
u5 can have multiple values separated by |
How can I capture all u5 values from a long string I have?
Below is for BigQuery Standard SQL
#standardSQL
WITH data AS (
SELECT 1 AS id, "asdk[wovkd'vk'psacxu5=205478499|205477661zamd;amd;a;d" AS junk UNION ALL
SELECT 2, "sadlmdlmdadsldu5=205478499|205477661|234567899amsd/samdamd"
)
SELECT id, SPLIT(REGEXP_EXTRACT(junk, r'(?i)u5=([\d|]*)'), '|') AS value
FROM data
with output as below
id value
1 205478499
205477661
2 205478499
205477661
234567899
I am creating a table in hive;
create table patients(
patient_id INT,
age_group STRING,
gender STRING,
income_range STRING
)
ROW FORMAT DELIMITED FIELDS TERMINATED BY ' ';
load data local inpath '/mnt/patients.csv' into table patients;
Now when I am using the command:
hive>select * from patients limit 5;
I am getting the output:
NULL 75-84, F, 32000-47999
NULL 75-84, M, 16000-23999
NULL 85+, M, <16000
NULL 65-74, F, 32000-47999
NULL <65, M, <16000
But when I am using assigning patient_id as string its showing:
910997967, 75-84, F, 32000-47999
506013497, 75-84, M, 16000-23999
432041392, 85+, M, <16000
633048699, 65-74, F, 32000-47999
I tried to use :
hive>select CAST(patient_id AS int) from patients;
But its not changing the values to int and only showing
NULL
NULL
...
How could the values of patient_id can be converted to int values?
Thanks
As #visakh pointed out that there is a comma(,) in your 1st column: patient_id.
You need to remove this.
You may use
CAST(regexp_replace(patient_id, ',' , '') AS INT)
This is similar to
Hive function to replace comma in column value
I have converted grid1 and grid2 into arrays and using following function which iterates through table and should return corresponding value form table when grid1 and grid2 values are matched. But somehow the final output contain only 4 integer values which isn't correct. Any suggestion what is possibly wrong here?
def grid(grid1,grid2):
table = {(10,1):61,(10,2):75,(10,3):83,(10,4):87,
(11,1):54,(11,2):70,(11,3):80,(11,4):85,
(12,1):61,(12,2):75,(12,3):83,(12,4):87,
(13,1):77,(13,2):85,(13,3):90,(13,4):92,}
grid3 = np.zeros(grid1.shape, dtype = np.int)
for k,v in table.iteritems():
grid3[[grid1 == k[0]] and [grid2 == k[1]]] = v
return grid3
I think what's happening is that the assignment to the variables "k" and "v" not done using "deepcopy". This means the assignment is just to the variables and not their values. For example, when the value of "k" changes on subsequent iterations, all previous "gridx" assignments now reflect the new/current status of "k".