I have a NUMERIC(5, 0) field in a IBM db2 database from which I only want the first 4 digit. I haven't been able to achieve what I want with using CAST.
How can I achieve this in SQL without casting as a String and using substring ?
Ex: 13525 --> 1352
Thanks for your help!
Why not cast as a string and use substring?
You can also do:
select (case when field >= 10000 then floor(field / 10) else field end)
This assuming that if the field has 1234, then you want 1234 rather than 0123.
EDIT:
You can also use a string by using two calls to cast():
select cast(left(cast(field as varchar(5)), 4) as numeric(4, 0))
I should also note that in many databases, you can just do:
select left(field, 4)
and the database will do the appropriate conversions. I don't have DB2 nearby to check this.
Related
I'm trying to add a wildcard to my date select query so i only pull the day not time. I.e. 2021-03-11 17:54:30.123. I thought a number could be substituted for a #.
select AID, LocalCoAltIn,LocalCoAltOut,EventTime
from EXCDS.dbo.WKS_LOG_VIEW
where EventTime like '2021-03-11 ##:##:##:###';
My query is returning no values even though they are in the table. Thanks.
No! Don't use strings! One method is to convert to a date:
select AID, LocalCoAltIn,LocalCoAltOut,EventTime
from EXCDS.dbo.WKS_LOG_VIEW
where convert(date, EventTime) = '2021-03-11';
Another method is to use a range:
where EventTime >= '2021-03-11' and
EventTime < '2021-03-12'
The LIKE operator in most flavors of SQL only support _ and * wildcards (matching any one single, or multiple characters). Gordon has given you a better approach, but if you wanted to fix your current query on SQL Server you could try:
SELECT AID, LocalCoAltIn, LocalCoAltOut, EventTime
FROM EXCDS.dbo.WKS_LOG_VIEW
WHERE EventTime LIKE '2021-03-11 [0-9][0-9]:[0-9][0-9]:[0-9][0-9]:[0-9][0-9][0-9]';
SQL Server extended the LIKE operator to accept a few extra things, such as character classes. Here [0-9] inside LIKE would match any single digit.
Not sure that like operator would work for date as you want, but you still have few options.
Use DATEPART function to retrieve year\month\etc and compare it with exact value that you need
select AID, LocalCoAltIn,LocalCoAltOut,EventTime from EXCDS.dbo.WKS_LOG_VIEW where DATEPART(year,EventTime) = 2021 AND DATEPART(month,EventTime) = 3 AND DATEPART(day,EventTime = 11);
Or use Gordon Linoff suggestion if you dont care about exact date part and only need to compare entire date without time
How can I add 5596 + 00003 to get 559600003 in SQL Server?
I tried with the following query:
select 5596 + '00003'
But it is giving 5599 and I want this to show 559600003.
You can also use CONCAT():
SELECT CONCAT(5596, '00003')
CONCAT() does not require any explicit conversion.
SQL Server attempts to return the result in the datatype of the first part of the calculation which in your case is a number. It can happily convert the second part of the calculation into a number, and therefore does so.
To obtain the result you want you must convert the first part of the calculation to a string e.g.
select convert(varchar, 5596) + '00003'
Note: convert(varchar,x) uses a default length of 30 which is probably enough for most numbers.
CONCAT(), as in one of the other answers is probably a better solution.
You can use CAST
SELECT CAST(5596 AS varchar) + '00003'; -- return 559600003
Because :
CAST is more easier to read than CONVERT
CAST is ANSI-SQL compliant : it means that the CAST function can be used by many databases
But in case you have a date type CONVERT is more flexible and contains more options than CAST
For more details take a look : https://learn.microsoft.com/en-us/sql/t-sql/functions/cast-and-convert-transact-sql?redirectedfrom=MSDN&view=sql-server-ver15
I am looking to put a comma after two digits from right. For example i have 250$ and in SQL I want the select to return 2,50$. Example2: 500$ should be 5.00$. For 1000$. It should be 10,00$. How can it be done in select. I have a column that holds those value in my SQL database.
Thanks you in advance for your help.
My column data type is bigint
Ironically, you can do this the same way in almost any database:
select cast( (col / 100.0) as decimal(20, 2))
I have a table with a column Quantity; in the original table this column is defined as nvarchar(100) so I need to cast it as float to be able to do some analysis:
CAST([Quantity] AS FLOAT) AS Quantity
The issue is that I have some values which can not be converted to float like No-Quantity, Return etc. I to have filter to exclude these values and then convert rest to float.On option is use where clause:
WHERE Quantity IN ('Return', 'Sales')
This is not the best way since if we have anew values in the original table then I need to figure out what it is and add it to the where clause.
I am wondering is there is better way to identify non-convertible values?
If your SQL Server supports TRY_CONVERT, this could provide a nice solution:
SELECT TRY_CONVERT (float, [Quantity]) ...
will give you the converted values or NULL depending on the input. This could be helpful if you don't have strict control over the data.
In any database, you can use cast() and something like this:
(case when quantity not in ('No-Quantity', 'Return', . . .)
then CAST([Quantity] as float)
end) as Quantity
The in list would be the list of known string values.
You can also do a fast-and-dirty check like this:
(case when left(quantity, 1) between '0' and '1'
then CAST([Quantity] as float)
end) as Quantity
(Note: you may need to use substr() or substring() instead of left().)
And, in general, any specific database has specific functions that can help with the conversion, such as try_convert() mentioned in a comment.
Another way (if you can't use TRY_CONVERT)
SELECT CAST(quantity AS float)
FROM myTable
WHERE IsNumeric(quantity) = 1 AND quantity IS NOT NULL
I am trying to query a column of type (int) into a string of format (000). For example: If the column value was 1 then the output of the query should be 001
Depends on which database engine you use.
In SQL Server 2012 and higher:
SELECT FORMAT(7, 'D3')
The result is this string:
007
For older versions of SQL Server, see the accepted answer by shree.pat18
For MySQL, look here: Adding a leading zero to some values in column
For PostgreSQL, look here: Padding zeros to the left in postgreSQL
Try this:
select right('000' + convert(varchar(3), intcolumn), 3) from yourtable
Note that the output is of type varchar. If you will need this output as a number somewhere else, I would suggest doing the formatting in your UI code and keeping it as a number in the query.