I want use 1000 separator(,) for numeric values.
how can i format strings with T-Sql Functions?
select convert(varchar(10), cast(1234.333 as money), 1)
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
I have a field that contains a value as Apr-14-2015 and I need to convert it to a Datetime field as 04/14/2015
You can convert the hyphens to spaces and use format 100:
select convert(date, replace(field, '-', ' '), 100)
You can use try_convert() instead if some of the values are not in the right format.
I have a column with this format 20150228 and I need to change it to 02/28/2015. Is there any function in SQL to do it.
Thanks
You can use this assuming the date is stored as a varchar:
SELECT CONVERT(VARCHAR(50),CONVERT(DATE,'20150228',112),101)
It returns:
02/28/2015
Use FORMAT function:
SELECT CONVERT(NVARCHAR(10), GETDATE(), 101)
I am writing a query where I need to calculate the number of days since a date that is stored in the database in the format "YYYYMMDD". Since this is not a Date datatype, I can't use native Date functions. What is the best way (performance-wise, readability-wise, etc.) to perform such a calculation in a SQL query.
Best? Convert that old table to use real date columns.
Next best? Write a database function to convert YYMMDD to a real date. Alan Campin's iDate can help. You'd end up with something akin to select cvty2d(date1)-cvty2d(date2) from ...
Better than nothing? Write ugly SQL to convert the number to character, split the character up, add hyphens and convert THAT to a real date. That beast would look something like
select
date(
substr(char(date1),1,4) concat
'-' concat
substr (char(date1),5,2) concat
'-' concat
substr(char(date1),7,2)
) -
date(
substr(char(date2),1,4) concat
'-' concat
substr (char(date2),5,2) concat
'-' concat
substr(char(date2),7,2)
)
from ...
Edit
The reason these gymnastics are necessary is that the DB2 DATE() function wants to see a string in the form of 'YYYY-MM-DD', with the hyphens being necessary.
Which version of SQL are you running? SQL2008 has no issues with that format as a date datatype
Declare #something nvarchar(100)
set #Something = '20120112'
select dateadd(dd, 1, #Something)
select datediff(dd, #Something, getdate())
2012-01-13 00:00:00.000
118
I want to SELECT a formatted date string from a datetime type in SQL Server 2005.
In the format "yyyy/mm/dd hh:mm:ss".
What is the best way to do using only a query?
Check out the CONVERT statement.
SELECT CONVERT(VARCHAR(20), getdate(), 120)
is closest to what you want. (Note the different separators (- instead of / ))
select convert(varchar, datetime_field, 120) from tablename;
will do almost what you want.
120 is the conversion "style", see here for more.