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.
Related
I have a data & time format column which I would like to convert to the following format in Microsoft SQL Server: yyyymmddhhmmss00000
So for example if I have 2021-02-04 11:49:50 this will be converted to 2021020411495000000.
Any one knows how to do it please?
You can use the FORMAT function:
SELECT FORMAT(myDate, 'yyyyMMddHHmmss00000')
By converting the date to NVARCHAR once with format 112 and once with format 8 you can extract the numeric date and the time without milliseconds. After removing : from the time you can concat these two strings and convert them to bigint. Following an example:
DECLARE #d DATETIME = GETDATE()
SELECT CAST(CONVERT(NVARCHAR(8), #d, 112) + REPLACE(CONVERT(NVARCHAR(8), #d, 8), ':', '') + '00000' AS BIGINT)
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 am trying to convert this into a period format, so e.g. 2018_05 (YYYY_MM). currently the data is in DD/MM/YYYY format.
I tried a cast code but it returns me YYYY_DD.
SELECT
CASE WHEN RESERVED_FIELD_4 IS NULL THEN NULL
ELSE cast(year(RESERVED_FIELD_4) as Nvarchar (4))
+'_'+right('00'+cast(month(RESERVED_FIELD_4) as Nvarchar (2)),2)
END AS [DATAFEED_PERIOD]
I expect/want to see YYYY_MM.
Assuming RESERVED_FIELD_4 is a string type (char/nchar/varchar/nvarchar) the simplest solution would be to use substring:
CASE
WHEN RESERVED_FIELD_4 IS NULL THEN NULL
ELSE SUBSTRING(RESERVED_FIELD_4, 7, 4) + '_'+ SUBSTRING(RESERVED_FIELD_4, 4, 2)
END AS [DATAFEED_PERIOD]
If it's a date/datetime/datetime2 data type, the simplest solution would be to use format:
FORMAT(RESERVED_FIELD_4, 'yyyy_MM')
But for better performance you can use convert and stuff:
SELECT STUFF(CONVERT(char(6), RESERVED_FIELD_4, 112), 5, 0, '_')
In case your format is actually d/m/y the simplest option is to convert to date and than back to string:
SELECT STUFF(CONVERT(char(6), CONVERT(Date, RESERVED_FIELD_4, 103), 112), 5, 0, '_')
This is the common problem of storing a date with a VARCHAR column. You are guessing that the stored pattern is DD/MM/YYYY but the SQL engine doesn't know that and is currently assuming the MM/DD/YYYY pattern.
Please check these results:
-- MM/DD/YYYY
SELECT
DAY ('05/01/2019'), -- 1
MONTH('05/01/2019') -- 5
-- DD/MM/YYYY
SELECT
DAY ('25/05/2019'), -- Conversion failed when converting date and/or time from character string
MONTH('25/05/2019') -- Conversion failed when converting date and/or time from character string.
To display what you want correctly use string functions:
SELECT
RIGHT(RESERVED_FIELD_4, 4) + '_' + SUBSTRING(RESERVED_FIELD_4, 4, 2)
But you should actually fix the values on your VARCHAR column, cast them to DATE and store the values as DATE.
ALTER TABLE YourTable ADD ReservedField4Date DATE
UPDATE YourTable SET
ReservedField4Date = CONVERT(DATE,
RIGHT(RESERVED_FIELD_4, 4) -- Year
+ '-' + SUBSTRING(RESERVED_FIELD_4, 4, 2) -- Month
+ '-' + LEFT(RESERVED_FIELD_4, 2)) -- Day
ALTER TABLE YourTable DROP COLUMN RESERVED_FIELD_4
EXEC sp_rename 'SchemaName.YourTable.ReservedField4Date', 'RESERVED_FIELD_4', 'COLUMN'
Beware that changing the column type might affect other queries that assume this is a VARCHAR column.
If your data is in DD/MM/YYYY format, then it is being stored as a string. Hence, string functions come to mind:
select right(RESERVED_FIELD_4) + '_' + substrint(RESERVED_FIELD_4, 4, 2)
In SQL-SERVER you can use 'format'
format(dy,#your_date) as day_of_year
month(#your_date) as month
Try this:
Select concat(month(#your_date),'_'year(#your_date)) as your_period
this is a reference
Why not just do conversations ? :
SELECT REPLACE(CONVERT(VARCHAR(7), CONVERT(date, RESERVED_FIELD_4, 101), 102), '.', '_')
This assumes RESERVED_FIELD_4 is date type.
I have a field [Privacy Notice] that is varchar(50) and the field has a MMDDYY format.I want to convert it to YYYYMMDD.
How do I do this?
Use substring to extract the parts and shuffle the year,month,day and convert.
select CONVERT(varchar,
CONVERT(datetime,RIGHT([Privacy Notice],2)+LEFT([Privacy Notice],2)+SUBSTRING([Privacy Notice],3,2)),
112)
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)