Convert string ( JAN 2014) to date in SSIS 2005 - sql

The data from source has string data type (JAN-2014) I want valid date and convert string to date. How can I do this in SSIS 2005?
Example : Jan-2014.
I want convert this date to 201401 type. Can anyone explain how I can go about this..

Try this:-
SUBSTRING(Col,FINDSTRING(Col,"-",1) + 1,4) +
RIGHT("0" +
(DT_WSTR,2)DATEPART("mm",
(DT_DATE)(SUBSTRING(Col,1,4) +
SUBSTRING(Col,FINDSTRING(Col,"-",1) +
1,4)
)),2)
The above expression will convert Jan-2014 to 201401 string format.

Related

Convert data and time into a numeric number in sql server

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)

SQL convert datetime to varchar

I want to convert DATETIME to VARCHAR (month/day/year) like this:
10/09/2018 12:00:00.000
I tried using
Convert(VARCHAR(MAX),[Date & Time Added]),121)
but it returns
yyyy-mm-dd hh:mi:ss.mmm
I need the / format with time, I am Using SQL Server 2012.
You can use the FORMAT function:
SELECT FORMAT(GETDATE(), 'MM/dd/yyyy HH:mm:ss.fff')
-- 10/09/2018 00:58:52.557
Complete list of format specifiers is actually available in the .NET documentation.
If FORMAT function is unavailable you could simply format in a known format and use string functions to re-arrange the year, month, day and time parts. For example:
SELECT SUBSTRING(DateStr, 6, 2) + '/' + SUBSTRING(DateStr, 9, 2) + '/' + SUBSTRING(DateStr, 1, 4) + ' ' + SUBSTRING(DateStr, 12, 12)
FROM (
SELECT CONVERT(VARCHAR(23), GETDATE(), 126) -- ISO8601 / yyyy-mm-ddThh:mi:ss.mmm
) AS CA(DateStr)
-- 10/09/2018 01:12:50.833
SELECT CONVERT(VARCHAR(10), Date, 101) + ' ' + CONVERT(VARCHAR(12), Date, 114)
FROM (
SELECT GETDATE()
) AS CA(Date)
-- 10/09/2018 01:19:38:463
This is something that would usually be done in the presentation layer, but if you need to do it in the data layer you can use FORMAT (documentation here)
Syntax is: FORMAT ( value, format [, culture ] )
So in your case (edited to add AM/PM designation per your comments): FORMAT([Date & Time Added], 'dd/MM/yyy hh:mm:ss.fff tt')
Custom date format string options are detailed here

SQL Server: How to concatenate string constant with date?

select packageid,status+' Date : '+UpdatedOn from [Shipment_Package]
The below error is appeared when executing the above code in sql server. The type of UpdatedOn is DateTime and status is a varchar. We wanted to concatenate the status, Date and UpdatedOn.
error:
Conversion failed when converting date and/or time from character
string.
You need to convert UpdatedOn to varchar something like this:
select packageid, status + ' Date : ' + CAST(UpdatedOn AS VARCHAR(10))
from [Shipment_Package];
You might also need to use CONVERT if you want to format the datetime in a specific format.
To achive what you need, you would need to CAST the Date?
Example would be;
Your current, incorrect code:
select packageid,status+' Date : '+UpdatedOn from [Shipment_Package]
Suggested solution:
select packageid,status + ' Date : ' + CAST(UpdatedOn AS VARCHAR(20))
from [Shipment_Package]
MSDN article for CAST / CONVERT
Hope this helps.
To account for null values, try this:
select packageid,
'Status: ' + isnull(status, '(null)') + ', Date : '
+ case when UpdatedOn is null then '(null)' else convert(varchar(20), UpdatedOn, 104) end as status_text
from [Shipment_Package]
Convert Datetime column into varchar for sql Server you can use below code
select packageid,status+' Date : '+CONVERT(VARCHAR,UpdatedOn,120) from [Shipment_Package]
120 code will convert your date into this format : 2020-06-12 15:09:00
other format you can use from this link

Convert CMMDDYY into date in SQL Server

If I have a date string in the format of CMMDDYY
C = 0, 1 or 2 where 0 represents 19, 1 represents 20 and 2 represents 21
Example:
1022511 would be 02/25/2011 in mm/dd/yyyy format.
Is there a way to convert this into mm/dd/yyyy format in sql server?
Is CMMDDYY even a valid date format? I haven't found much information on it.
The Year algorithm is simply: (19 + int(C))*100 + int(yy)
Try this:
declare #st nvarchar(7)
set #st = '1030609'
select
cast(
substring(#st,2,2) + '/'
+ substring(#st,4,2) + '/'
+ cast((19 + cast(substring(#st,1,1) as int) )* 100
+ cast(substring(#st,6,2) as int) as nvarchar)
as datetime)
This outputs:
It doesn't sound like a built in date format but you should be able to find it on msdn if it is. If not you could write a function in SQL server that parses a string in that format to a DateTime object

SQL - convert long integer to datetime

I have a database with dates in the following long integer format:
20100101000000
Where that would be Jan 1st, 2010, 00:00:00 for the time of day.
I want to be able to convert this to normal SQL Datetime syntax and back. Is this possible? I can only get this far:
SELECT CAST(CURRENT_TIMESTAMP as int);
Which returns '40556' - not exactly what Im after.
You could use substring to convert your string to yyyy-MM-dd HH:mm:ss notation, and cast that to a datetime:
select convert(datetime,
substring(sub.Dt,1,4) + '-' +
substring(sub.Dt,5,2) + '-' +
substring(sub.Dt,7,2) + ' ' +
substring(sub.Dt,9,2) + ':' +
substring(sub.Dt,11,2) + ':' +
substring(sub.Dt,13,2))
from (
select '20100101000000' as Dt
) sub
You can stuff it with the spaces and colons required:
select
stuff(stuff(stuff('20100101000000',9,0,' '),12,0,':'),15,0,':') STR,
convert(datetime,stuff(stuff(stuff('20100101000000',9,0,' '),12,0,':'),15,0,':')) DT;
The result is
STR | DT
20100101 00:00:00 | 2010-01-01 00:00:00.000
The first one shows the string it is converted to, the 2nd the datetime value.
To go in reverse
select
convert(char(8),getdate(),112) + replace(convert(varchar(30),getdate(),108),':','');
Replace the constants '20100101000000' and "getdate()" with field names where required if selecting from a table.