How to give 0 at the beginning of hour or giving AM and PM in postgresql [duplicate] - postgresql

I am relatively new to PostgreSQL and I know how to pad a number with zeros to the left in SQL Server but I'm struggling to figure this out in PostgreSQL.
I have a number column where the maximum number of digits is 3 and the min is 1: if it's one digit it has two zeros to the left, and if it's 2 digits it has 1, e.g. 001, 058, 123.
In SQL Server I can use the following:
RIGHT('000' + cast([Column1] as varchar(3)), 3) as [Column2]
This does not exist in PostgreSQL. Any help would be appreciated.

You can use the rpad and lpad functions to pad numbers to the right or to the left, respectively. Note that this does not work directly on numbers, so you'll have to use ::char or ::text to cast them:
SELECT RPAD(numcol::text, 3, '0'), -- Zero-pads to the right up to the length of 3
LPAD(numcol::text, 3, '0') -- Zero-pads to the left up to the length of 3
FROM my_table

The to_char() function is there to format numbers:
select to_char(column_1, 'fm000') as column_2
from some_table;
The fm prefix ("fill mode") avoids leading spaces in the resulting varchar. The 000 simply defines the number of digits you want to have.
psql (9.3.5)
Type "help" for help.
postgres=> with sample_numbers (nr) as (
postgres(> values (1),(11),(100)
postgres(> )
postgres-> select to_char(nr, 'fm000')
postgres-> from sample_numbers;
to_char
---------
001
011
100
(3 rows)
postgres=>
For more details on the format picture, please see the manual:
http://www.postgresql.org/docs/current/static/functions-formatting.html

As easy as
SELECT lpad(42::text, 4, '0')
References:
http://www.postgresql.org/docs/current/static/functions-string.html
sqlfiddle: http://sqlfiddle.com/#!15/d41d8/3665

The easiest way:
ltrim(to_char(Column1, '000'))

Related

how to cast varchar with 3 numbers after comma to float type PostgreSQL

I have a varchar type where each value looks like
539,000
153,300
55,300
752,500
4410,000
I need change column type with number format
I tried use to_number but it is not working
In case your text representation is a German decimal value (comma is decimal separator):
demo:db<>fiddle
Replace comma with dot and simply cast into float:
SELECT replace('123,456', ',', '.')::float
In case the comma separates the thousand groups you need to use the G formatter, which describes this:
demo:db<>fiddle
SELECT to_number('123,456', 'FM999G999')
FM supresses leading blanks and zeros, so even smaller number would work:
SELECT to_number('56', 'FM999G999')
Make sure you have German numeric configuration set and use to_number:
SHOW lc_numeric;
lc_numeric
------------
de_DE.utf8
(1 row)
SELECT to_number('539,000', '99999D000');
to_number
-----------
539.000
(1 row)
You can alter like this
ALTER COLUMN my_column TYPE float USING replace(my_column, ',','.')::float
https://dbfiddle.uk/?rdbms=postgres_10&fiddle=2ff308849d986d26637a011102bb752c

Substring SQL Select statement

I have a number of references with a length of 20 and I need to remove the 1st 12 numbers, replace with a G and select the next 7 numbers
An example of the format of the numbers being received
50125426598525412584
I then need to remove first 12 digits and select the next 7 (not including the last)
2541258
Lastly I need to put a G in front of the number so I'm left with
G25412584
My SQL is as follows:
SELECT SUBSTRING(ref, 12, 7) AS ref
FROM mytable
WHERE ref LIKE '5012%'
The results of this will leave me with
25412584
But how do I insert the G in front of the number in the same SQL statement?
Many thanks
SELECT 'G'+SUBSTRING(ref, 12, 7) AS ref FROM mytable where ref like '5012%'
SELECT CONCAT( 'G', SUBSTRING('50125426598525412584', 13,7)) from dual;

Adding leading zero if length is not equal to 10 digit using sql

I am trying to join 2 tables but my problem is that one of the table has 10 digit number and the other one may have 10 or less digit number. For this reason, i am loosing some data so i would like to do is check the length first if the length is less than 10 digit then i want to add leading zeros so i can make it 10 digit number. I want to do this when i am joining this so i am not sure if this is possible. Here is an example if i i have 251458 in the TABLE_WITHOUT_LEADING_ZERO then i want to change it like this: 0000251458. Here is what i have so far:
select ACCT_NUM, H.CODE
FROM TABLE_WITH_LEEDING_ZERO D, TABLE_WITHOUT_LEADING_ZERO H
WHERE substring(D.ACCT_NUM from position('.' in D.ACCT_NUM) + 2) = cast (H.CODE as varchar (10))
thanks
Another alternative:
SELECT TO_CHAR(12345,'fm0000000000');
to_char
------------
0000012345
In Netezza you can use LPAD:
select lpad(s.sample,10,0) as result
from (select 12345 as sample) s
result
-------
0000012345
However it would be more efficient to remove the zeros like in the example below:
select cast(trim(Leading '0' from s.sample) as integer) as result
from (select '0000012345' as sample) s
result
-------
12345

postgres coalesce fields, sum and group by

maybe someone can help me out with a postgres query.
the table structure looks like this
nummer nachname vorname cash
+-------+----------+----------+------+
2 Bert Brecht 0,758
2 Harry Belafonte 1,568
3 Elvis Presley 0,357
4 Mark Twain 1,555
4 Ella Fitz 0,333
…
How can I coalesce the fields where "nummer" are the same and sum the cash values?
My output should look like this:
2 Bert, Brecht 2,326
Harry, Belafonte
3 Elvis, Presley 0,357
4 Mark, Twain 1,888
Ella, Fitz
I think the part to coalesce should work something like this:
array_to_string(array_agg(nachname|| ', ' ||coalesce(vorname, '')), '<br />') as name,
Thanks for any help,
tony
SELECT
nummer,
string_agg(nachname||CASE WHEN vorname IS NULL THEN '' ELSE ', '||vorname END, E'\n') AS name,
sum(cash) AS total_cash
FROM Table1
GROUP BY nummer;
See this SQLFiddle; note that it doesn't display the newline characters between names, but they're still there.
The CASE statement is used instead of coalesce so you don't have a trailing comma on entries with a last name but no first name. If you want a trailing comma, use format('%s, %s',vorname,nachname) instead and avoid all that ugly string concatenation business:
SELECT
nummer, string_agg(format('%s, %s', nachname, vorname), E'\n'),
sum(cash) AS total_cash
FROM Table1
GROUP BY nummer;
If string_agg doesn't work, get a newer PostgreSQL, or mention the version in your questions so it's clear you're using an obsolete version. The query is trivially rewritten to use array_to_string and array_agg anyway.
If you're asking how to sum numbers that're actually represented as text strings like 1,2345 in the database: don't do that. Fix your schema. Format numbers on input and output instead, store them as numeric, float8, integer, ... whatever the appropriate numeric type for the job is.

How can we use prefix as zero in SQL?

How can we use zero's in prefix of the number column feedback_refno like
select #refno = '0001'
i need to insert this value into that column feedback_refno but it is inserting like 1 only..but i need those prefix before those 1
I have tried like this
declare #refno int
select max(feedback_refno)+1 from EDK_Customer_Feedback(nolock)
if not exists(select feedback_refno from EDK_Customer_Feedback(nolock))
Begin
select #refno = '0001'
end
else
Begin
select #refno
End
insert into EDK_Customer_Feedback values(#refno)
I need the result like 0002 then 0003 like that but it is giving like 2 then 3..
Any suggestion?
try this
SELECT RIGHT('000'+ CONVERT(varchar,feedback_refno),4) AS NUM FROM EDK_Customer_Feedback;
#refnois of type int, so leading zeros wont work. If you change it to varchar(4) you can use these two answers:
In SQL Server 2000 how do you add 0's to beginning of number to fill nchar(n)
In SQL Server 2000 how do you add {n} number of 0's to a nchar(n)?