Tera data to big query - google-bigquery

How do we convert a tera data query in big query if I need a field2 to give results as -00000000001
Select
Sum(case(when field1=‘C’
Then field2
Else 0)
Format (-9999999999)char(11))

select cast(sum(if(field1='C', field2, 0)) as string Format '00000000000')
in case if you want to see explicit sign - use below
select cast(sum(if(field1='C', field2, 0)) as string Format 'S00000000000')
For quick test rune below
select cast(-1 as string Format 'S00000000000'), cast(1 as string Format 'S00000000000')
with output

Regexp_replace(cast (sum(if field1=‘C’, field2,0) )as string format (“S0000000000”),’[+]’,’ ‘)
Output
Field2 is populated with negative sign for negative number and one space Infront of positive number
This I added with the give solution for format from MIkhail.

Related

Can't find a value in a column

I'm looking for an image in a column, I know it's there. But when I select, I can't find it. Does someone know what I am doing wrong?
SELECT
CONVERT(VARBINARY(MAX), Picture),
COUNT(*) AS Count -- convert(nvarchar,(MAX(employeeid)))
FROM
TABLENAME
GROUP BY
CONVERT(VARBINARY(MAX), Picture)
HAVING
COUNT(*) > 1
ORDER BY
Count
I get a result and a count 4. When I query the table using
Select CONVERT(VARBINARY(MAX), Picture),employeeid from TABLENAME where CONVERT(VARBINARY(MAX), Picture) = 'value from result 1'
and the value in a select statement, I get nothing.
If I understand correctly, and you want to identify rows where the same image is used more than once then you can do:
WITH CTE AS(
SELECT employeeid,
Picture,
COUNT(employeeid) OVER (PARTITION BY Picture) AS Entries
FROM YourTable)
SELECT *
FROM CTE
WHERE Entries > 1;
Also, I suspect your query didn't work because you had:
CONVERT(VARBINARY(MAX), Picture) = 'value from result 1'
You're implying that you put the value of the previous varbinary in literal string quotes. The values 0x01 and '0x01' are not the same (SELECT CASE WHEN 0x01 = '0x01' THEN 1 ELSE 0 END; returns 0). '0x01' is a string representation of a varbinary, however, if you cast that value to a varbinary you get 0x30783031 (SELECT CASE WHEN 0x30783031 = '0x01' THEN 1 ELSE 0 END; returns 1).
You get nothing because you are performing a STRING search on BINARY data.
where CONVERT(VARBINARY(MAX), Picture) = 'value from result 1'
The left part is Binary, the right part is a string (CHAR)
You should convert again your VARBINARY to VARCHAR to perform the string search :
Select CONVERT(VARBINARY(MAX), Picture),
employeeid
from TABLENAME
where CONVERT(varchar(max),CONVERT(VARBINARY(MAX), Picture),1) = 'value from result 1'
The ,1 is the "style" that tells SQL server to return the string in binary notation, ie with 0x appended at the begining, which is how your "binary strings" are supposed to be returned by your first query. See Binary styles on CAST and CONVERT (Transact-SQL)

How to preserve leading zeros when converting to a decimal in oracle

I am running the below query in oracle.
WITH
ta AS (
SELECT account_coid
,txn_id
,cbdev.cbzdt(effective_date) AS effective_date
,cbdev.cbchr(utl_raw.substr(txn_data, 113, 20)) AS CESG_amt
FROM bs_transaction
WHERE sub_type = 127469880)
SELECT
cast(ta.CESG_amt as DECIMAL (20,2)) AS cesg_amt
from ta
inner join ....
Here, i m getting the result (cesg_amt) as -156.57. But i need the result as -0000000000156.57.
I need the leading zeros with - retained (leading 0's and also the two digits after the decimal).
I have tried as to_char(ta.CESG_amt, '0000000000000.00') AS cesg_amt in the query but of no use.
Can you please help me what needs to be done in the DECIMAL field to get the result as below.
You may use such a formatting :
select to_char(-156.57,'fm0000000000000D00','NLS_NUMERIC_CHARACTERS = ''.,''')
as Result
from dual;
RESULT
-----------------
-0000000000156.57
select to_char(-156.57,'000000000.00')
from dual;

Sybase LIST function truncating results after 256 characters

I'm trying to aggregate a column of strings into one cell by concatenating them together and separating them with commas using syabase's LIST fuction. But the results get truncated after 256 characters. Does anyone know of a way to fix this or if there are any alternatives that would give me what I'm looking for.
For example, if I have a table myTable that looks like this:
myVal
-------------
'0000000001'
'0000000002'
'0000000003'
'0000000004'
'0000000005'
'0000000006'
'0000000007'
'0000000008'
'0000000009'
'0000000010'
'0000000011'
'0000000012'
'0000000013'
'0000000014'
'0000000015'
'0000000016'
'0000000017'
'0000000018'
'0000000019'
'0000000020'
'0000000021'
'0000000022'
'0000000023'
'0000000024'
'0000000025'
'0000000026'
'0000000027'
'0000000028'
'0000000029'
'0000000030'
then run the following query:
select list(myVal,',') as myResult from myTable
I get the following result
myResult
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
0000000001,0000000002,0000000003,0000000004,0000000005,0000000006,0000000007,0000000008,0000000009,0000000010,0000000011,0000000012,0000000013,0000000014,0000000015,0000000016,0000000017,0000000018,0000000019,0000000020,0000000021,0000000022,0000000023,000
Notice the result string gets truncated after 0000000023
Try this:
SELECT
LIST( CAST( myVal AS nvarchar(max) ), ',' )
FROM
myTable

Converting SQL varchar column values to $ format i.e. thousand separation

I have a varchar(256) column AttributeVal with all different type of text values.
I need to find out all $ values like $5000, $2000 etc & add thousand separator to them (only to these values, but not to the other text values present in that column).
Thus the updated values should look like $5,000 & $2,000.
If I am using following query, then it will end up converting all values & I need to concatenate $ manually :(
replace(convert(varchar, convert(Money, AttributeVal), 1), '.00', '')
NB : I know that these kind of formatting should be taken care in the application end, but our customer is adamant to have these customization to be stored in DB only.
I don't think you can do a replace statement based on a regular expression like that exactly. See this stackoverflow post asking the same question.
You may want to reinforce to your client that formatted data should not be stored in a database. That money value should probably be stored in a DECIMAL(13, 4) or something similar instead of a VARCHAR field mixed with other data as well.
Your question is a great example of why you don't want to do this. It makes simple things very difficult.
Try this
SELECT '$'+ PARSENAME( Convert(varchar,Convert(money,convert(Money, 100000)),1),2)
Output: $100,000
Hope this help!
try with this, this will take care of thousand separator :-)
'$'+convert(varchar(50), CAST(amount as money), -1) amount
Sample
;with cte (amount)
as
(
select 5000 union all
select 123254578.00 union all
select 99966.00 union all
select 0.00 union all
select 6275.00 union all
select 18964.00 union all
select 1383.36 union all
select 26622.36
)
select '$'+convert(varchar(50), CAST(amount as money), -1) amount
from cte
Here is my take on the problem:
select coalesce(cast(try_convert(money, value) as varchar(50)), value) converted
from (
values ('50')
, ('5000')
, ('3000.01')
, ('text')
) samples(value)
and the output:
converted
--------------------------------------------------
50.00
5000.00
3000.01
text
(4 row(s) affected)

How to limit length of data in the output -sql

SELECT po.po_number .... SOME QUERY
WHERE po.po_number not like '%[^0-9]%' AND (mai.removed = 0)
ORDER BY mai.unique_issue_id
Here the output that I am getting is 10-digit numeric value , but I want 10-digit as well as 8 digit numeric, please suggest.
On SQL Server, just CAST or CONVERT your data to the output type you want:
SELECT
CONVERT(NUMERIC(10,0), po.po_number) AS [Number10d],
CONVERT(NUMERIC(8,0), po.po_number) AS [Number8d]
...