I want to add 2 zero to the right of the next function: UNIX_TIMESTAMP (NOW()),
So instead of: 1369047810, I would get: 136904781000
I try this:
SELECT (UNIX_TIMESTAMP (NOW()) + RIGHT(REPLICATE('0', 2))))
but it doesn't help.
SELECT UNIX_TIMESTAMP (NOW()) * 100
If you want it as a string, then you want to convert it to a string and then add the zeros. Something like:
SELECT (cast(UNIX_TIMESTAMP (NOW()) as varchar(255) + RIGHT(REPLICATE('0', 2))))
I think the string conversion is safer than doing arithmetic, if you want a string in the end. Multiplying values might cause arithmetic overflow.
Also, I associate the syntax UNIX_TIMESTAMP (NOW()) with MySQL (as I write this, there is no database tag on the question). The right syntax in that database would be:
select concat(cast(UNIX_TIMESTAMP (NOW()) as varchar(255), '00')
Related
I have values in a column named Date as a nvarachar data type in the form of mmddyy and want to convert the values to a date datatype in the form of yyyy-mm-dd, what sql colde can I use to convert the value.
example
02121955 -> 1955-02-12
You can use datefromparts():
select datefromparts(right(str, 4), left(str, 2), substring(str, 3, 2))
Or reconstruct it as yyyymmdd format and just convert:
select convert(date, left(right(str, 4) + str, 8))
Here is a db<>fiddle.
I have found a similar Question to yours that has been solved: https://stackoverflow.com/a/39139155/14940878
Here is the Code changed for your requests:
declare #date varchar(max)='02121955'
select convert(varchar(8),cast(CONCAT(SUBSTRING(#date,5,4),'/',SUBSTRING(#date,1,2),'/',SUBSTRING(#date,3,2)) as date),112)as [YYYYMMDD]
You need to take each field and concat it into one and then convert it to datetime.
SUBSTRING(#date,5,4) - Takes the last four characters.
SUBSTRING(#date,1,2) - Takes the first two.
SUBSTRING(#date,3,2) - Takes the two in the middle.
Assuming it's SQL Server, I think you just need
select cast(format(02121955,'##-##-####') as date)
DEMO
When running this query, I need output by combining
DATEDIFF(mm, StartTime, EndTime) + 'Minutes'
I'm getting this error
Conversion failed when converting the varchar value 'Minutes' to data type int
I need to achieve the output like 15 Minutes.
(15 represents the difference between StartTime and EndTime)
DATEDIFF function returns a INT values.
So you need to CAST it to a string.
Try something like
SELECT CAST(DATEDIFF(mm,StartTime, EndTime) AS NVARCHAR(2)) + 'Minutes'
Obviously you can use other destination string type like char\nchar etc... And use the CONVERT function instead of CAST.
Use CONCAT():
select concat(DATEDIFF(minute, StartTime, EndTime), 'Minutes')
CONCAT() will conveniently convert arguments to strings. Also note that this spells out minute. I recommend using the full name when using date functions.
For presentations sake I have a value that I want to show to only two decimal places before converting to varchar
select '£' + cast(round(amount,2) as varchar (10))
This works fine and would display the result as, say, £300.00. However the 'amount' also needs to be divided by 100 as part of the query. When I add that in to the code...
select '£' + cast(round(amount/100,2) as varchar (10))
..it displays as £3.000000. Is there any way to remove the extra 0s so only two are shown after the decimal point?
Just use format():
select '£' + format(amount, 2)
This also adds in commas, which seems desirable.
If you don't want the commas, then don't use round(), cast to a decimal type:
select '£' + cast(cast(round(amount, 2) as decimal(10, 2)) as varchar(10))
round() does change the value, but it doesn't change the storage mechanism. The cast( . .. as varchar) doesn't know -- or care -- how many significant values are in the result.
EDIT (for SQL Server):
Instead of format() you can use the str() function:
select '£' + ltrim(str(amount, 10, 2))
Or the last method of converting to a decimal before the conversion.
What is the datatype of amount column? cast the round part into float
select '£' + cast(round(cast(8375.8734/100 as float),2) as varchar (100));
Result is
£83.76
Also this should done in the front end application and not in sql
How can I convert my DECIMAL(11) field from 12345678 to a character value of 00012345678?
Only use the DIGITS function, because this verifies the length of the field numeric or decimal, etc and completes with zeros to the left when is necessary.
SELECT DIGITS(FIELD) FROM ...
The length of the resulting string is always:
5 if the argument is a small integer
10 if the argument is a large integer
19 if the argument is a big integer
Based on your comment in #Mr Fuzzy Botton's answer, I'm guessing you're on DB2 for i, which does not have the LPAD function. You could instead use a combination of the REPEAT and RIGHTfunctions:
SELECT RIGHT(REPEAT('0', 11) || LTRIM(CHAR(your_field)), 11)
FROM your_table
Using http://pic.dhe.ibm.com/infocenter/dzichelp/v2r2/topic/com.ibm.db2.doc.sqlref/castsp.htm for details on CAST
and http://pic.dhe.ibm.com/infocenter/dzichelp/v2r2/topic/com.ibm.db2z10.doc.sqlref/src/tpc/db2z_scalarfunctionsintro.htm for string functions,
I assume this should do the trick -
SELECT LPAD( CAST(FIELD AS CHAR(11)) ,11,'0') AS PADDEDFIELD
Don't know if you've worked it out, however try this:
SELECT LPAD( DIGITS( fieldName ), 11, '0') ) as paddedFieldName FROM yourTable
The LPAD is the left padding, but the DIGITS function was the only way I got DB2 to treat the numeric value like a string.
My LeftPad function without LeftPad function
REPEAT('0', 4-length(MY_COLUMN_VALUE))||CHAR(MY_COLUMN_VALUE) as NEW_COLUMN
MY_COLUMN_VALUE NEW_COLUMN
1 0004
23 0023
testing ...
SELECT '32' MY_VALUE, REPEAT('0', 4-length('23'))||CHAR('23') as LEFTPAB_MY_VALUE FROM sysibm.sysdummy1
If this is DB2 for i, and myColumn data type is DECIMAL with precision (11) and scale (0), then:
SELECT digits( myColumn ) FROM sysibm.sysdummy1
will return:
....+....1.
DIGITS
00001234567
Changing the number of leading zeros could be done in many ways. CASTing to a different precision before using DIGITS() is one way.
SELECT SUBSTRING(
CHAR(100000000000+fieldName),
2,11
) as paddedFieldName
FROM yourTable
I only wanted to define my field once in the select statement so the above worked for me and is tidy
I just went the other direction: cast(field as int)
Try this for your field x:
substr(digits(x), 33 - length(x), length(x) )
From Numeric 8,0 (datenumfld=20170101) to 01/01/2017 This works for me:
DATE(TO_DATE(CHAR(datenumfld), 'YYYYMMDD')) as YourDate
I have alphanumeric values like. XYZ1,XYZ2......XYZ11, XYZ12 and so on, now I want to select only the Max numeric value, i.e. 12 here.
I tried-
select max(REPLACE(ID,'XYZ','')) from myTable;
but this is returning 9. why?
Try converting to INT before max
select max(cast(REPLACE(ID,'XYZ','') as int)) from myTable;
It's still treating your value as a string instead of a number. Try:
select max(CAST(REPLACE(ID,'XYZ','') AS INT) from myTable;
Because you're still comparing strings. The fact that they contain only numeric digits doesn't mean that they're not strings. You need to convert them:
SELECT MAX(CAST(REPLACE(id, 'XYZ', '') AS INT)) FROM My_Table
Another method is
select max(REPLACE(ID,'XYZ','')*1) from myTable