How can i use a thousand separator in SQL Server? [duplicate] - sql

This question already has an answer here:
How can I get a number with a thounds separator on top?
(1 answer)
Closed 5 years ago.
SELECT count(id) FROM table a
it gives me a number lets say 36136, how can i make thousand separator on top?

Try (MSSQL)
SELECT REPLACE(REPLACE(
CONVERT(VARCHAR,CONVERT(MONEY, COUNT(ID)),1)
,'.00','') ,',','''')
FROM TABLE A
Example
SELECT REPLACE(REPLACE(CONVERT(VARCHAR,CONVERT(MONEY, 1000),1),'.00','') ,',','''') X
Output
X
----
1'000

Use TOP statement in SQL server :
SELECT TOP 1000 * FROM table a

Related

How to express percentage with decimal in SQL [duplicate]

This question already has answers here:
SQL Server, division returns zero
(6 answers)
Closed 1 year ago.
SELECT RecruitmentSource, COUNT(RecruitmentSource) AS NumberOfHired, COUNT(RecruitmentSource)/SUM(COUNT(RecruitmentSource)) OVER () AS PercentageHired
FROM HRDataset_v14$
GROUP BY RecruitmentSource
Percentage Hired that I expect as below link
FYI, Total Number of Hired is 311 so I expect the number would be like
1. 0.0739
2. 0.0932
3. 0.0997
.
.
.
9. 0.0418
Please help me to solve it.
SQL Server does integer division. I usually just multiply by 1.0 to avoid this:
SELECT RecruitmentSource, COUNT(RecruitmentSource) AS NumberOfHired,
COUNT(RecruitmentSource) * 1.0/SUM(COUNT(RecruitmentSource)) OVER () AS PercentageHired
FROM HRDataset_v14$
GROUP BY RecruitmentSource

Concatenate multiple rows to one row [duplicate]

This question already has answers here:
Simulating group_concat MySQL function in Microsoft SQL Server 2005?
(12 answers)
SQL Server: Combine multiple rows into one row from a join table?
(1 answer)
ListAGG in SQLSERVER
(4 answers)
how to stuff date and time in SQL
(2 answers)
Query to get multiple row into single row
(3 answers)
Closed 3 years ago.
I have the below data and I have to concatenate the long text column and make it as a single row. The challenge part is only one row has the notification number and other rows are null. You cannot group by the notification number.
I need the output as 2 rows
row number Notification Number Plant Creation Date Language Lineno Tag Long Text
1 10014354914 A057 43466 EN 1 >X aaabbbcccdddeeefffggghhhjjjkkklll
2 10014354915 A057 43466 EN 1 >X aaabbbcccdddeeefffgggpppqqqrrrsss
I have used cursor for this. But it is taking much time.
If you are using oracle:
with data("row number", "Notification Number","Plant","Creation Date","Language","Lineno","Tag","Long Text") as (
select 1,10014354914,'A057',43466,'EN',1,'>X','aaabbbcccdddeeefffggghhhjjjkkklll' from dual
union all
select 2,10014354915,'A057',43466,'EN',1,'>X','aaabbbcccdddeeefffgggpppqqqrrrsss' from dual)
select LISTAGG("Long Text",'') within group (order by "row number") from data;
if you are using ms-sql maybe try this:
SELECT u.[Long Text] AS [text()]
FROM yourtable u
ORDER BY u.[row number]
FOR XML PATH ('')

Select a range of numbers not in a table in SQL [duplicate]

This question already has answers here:
What is the best way to create and populate a numbers table?
(12 answers)
Closed 4 years ago.
I am wondering if it is possible to get a query that will take a range of numbers, in this case 8 to 17, compare it against a field in a table and remove the ones that do appear in the table and return the rest?
I assume the peusdo code would look something like
Select nums from range(8-17) where nums not in (select column from table)
Is this possible at all?
Edit
To clarify my question.
In table I might have the following:
Intnumber
9
10
16
I would like to have the numbers between 8-17 that do not appear in this table, so 8,11,12,13,14,15,17
Kind regards
Matt
select nums from table where nums not between 8 and 17;

BigQuery split column and get count of count each substring [duplicate]

This question already has answers here:
How can I compute TF/IDF with SQL (BigQuery)
(3 answers)
Closed 4 years ago.
In BigQuery I would like to create a query to count the occurrence of words in a comments field and group by a count of each occurrence. This would me get a sense of what words were used more than others and get a sense of user behavior and moods. Pretty new to bigquery so any ideas will be helpful.
What I ended up doing was using the split function...
SELECT
COUNT(JJ) AS STUFF, JJ
FROM
(SELECT SPLIT(text, ' ') AS JJ FROM [bigquery-public-
data:hacker_news.comments] LIMIT 1000 )
GROUP BY JJ
ORDER BY STUFF DESC
LIMIT 5
Obviously it can be manipulated more by using replace to remove other characters before splitting.

fetch values into a comma separated string in postgresql [duplicate]

This question already has answers here:
Postgresql GROUP_CONCAT equivalent?
(9 answers)
Closed 7 years ago.
This should be simple one. A query like
SELECT code_id FROM code WHERE status = 1
gives normally a result like
code_id
10
11
12
But my goal is to fetch into a string like
10,11,12
In order to use it in an other query
SELECT x FROM table WHERE status in (10,12,13)
Preferable in the same query. Is this possible using "standard" Postgresql WITHOUT adding extra extension?
Everything so far is using extension that not are available as standard.
Thanks in advance.
Whatever tool you are using just shows you the data like that for convenience. But you can also use the resultset in a subquery, like this
SELECT x FROM table WHERE status in (
SELECT code_id FROM code WHERE status = 1
)
You can try this way to get result as comma-separated
But my goal is to fetch into a string like
SELECT string_agg(code_id, ',') FROM code WHERE status = 1