Regex or conversion for 'YYYY-MMM-DD' - sql

I'm working with data in t-SQL and in order to automate my package in SSIS that will process each file with each given date, I need to figure out the regex or conversion for a file such as 'leads_2019-Dec-22' so the package can complete.
So far this is as far as I've gone, however this only works for 'YYYY-MM-DD' formats. I'm unable to change the format from the data loader tool I have, so there's no easy fix to this other.
#[User::UploadedFile] =
REPLACE(REPLACE(REPLACE(REPLACE(#[User::UploadedFileName], "yyyy",
(DT_STR,4,1252)DATEPART( "yyyy" , getdate())),"mm",RIGHT("0"+
(DT_STR,4,1252)DATEPART("mm",getdate()),2)), "dd", RIGHT("0"+
(DT_STR,4,1252)DATEPART("dd",getdate()),2)), "hh", right("0" +
(DT_STR,4,1252)DATEPART("Hh",getdate()),2) )
Has anyone dealt with this before and if so, how did/would you solve this using an Expression?

SQL Server does not support regular expressions.
However, the format used in the file name is very similar to a format you can use with convert to get the actual date from it's string representation.
The format convert supports under style 106 is dd mom yyyy - meaning all you have to do is isolate the date part from the string, replace the hyphens with spaces, and convert.
Please note that if the default language of the current login is not English, you might get errors because the month names depends on the language settings.
This is why I've included the set language statement in my code:
SET LANGUAGE us_english;
DECLARE #FileName varchar(20) = 'leads_2019-Dec-22';
SELECT CONVERT(Date, REPLACE(RIGHT(#FileName, 11), '-', ' '), 106);
Result:
2019-12-22

If you can be sure, now and forever, that this will never-ever run in systems with a different culture you can use easy conversions or set the language and culture for a session specifically.
But - if ever run on different systems - this might pass all your internal tests and may break in production with silly errors.
Culture specific approaches (and even worse: language specific ones) are very dangerous...
To overcome this you can use the following culture-safe approach (but it will be slower than simple conversions):
--Your question is not clear for me about the actual input.
--The string I use here seems to be your needed outcome...
--However, you will get the ghist how you can approach this issue with any given value...
DECLARE #TheFileName varchar(20) = 'leads_2019-Dec-22';
--not needed, just for testing... (in Germany "Dec" needs to be "Dez"...)
SET LANGUAGE GERMAN;
SELECT FORMAT(TRY_PARSE(RIGHT(#TheFileName,11) AS DATE USING 'en-us'),'yyyy-MMM-dd','en-us');
You have three obstacles:
First is to cut off the pure date. I do this by using RIGHT assuming we always need the rigth-most eleven characters.
Second is to get a date-typed value out of this. TRY_PARSE() accepts a culture parameter to ensure correct reading.
Third is to create the correct output. Here I use FORMAT(), which again allows for a specific culture.

Assuming you want the an output string in YYYY-MM-DD format from a variable called UploadedFileName that has data like 'leads_2019-Dec-22', you can just replace the "leads_" part and convert the rest to a date. Then you can parse the date to change it to the desired YYYY-MM-DD format:
(DT_WSTR, 4) YEAR((DT_DATE) REPLACE(#[User::UploadedFileName],"leads_","")) + "-" +
(DT_WSTR, 2) MONTH((DT_DATE) REPLACE(#[User::UploadedFileName],"leads_","")) + "-" +
(DT_WSTR, 2) DAY((DT_DATE) REPLACE(#[User::UploadedFileName],"leads_",""))

Related

T-SQL format date by pattern

Is possible change format date by specific pattern ? I need to made a function which has a two parameters. First is date and second is pattern. I need convert more date variants. Goal this function is change US and European date format.
For example i need convert
EU: dd:MM:yyyy hh:mm:ss
to
US: MM:dd:yyyy hh:mm:ss
On another page i need change
EU: dd/MM/yyyy
to
US: MM/dd/yyyy
And i have a several next variant to convert
And i want to made a similar function
Formater(euDate, pattern)
BEGIN
....
RETURN usDate
My production server is unfortunately SQL server 2005 and doesn't support function FORMAT(). And function CONVERT() doesn't support some variant of date, which i need convert. So in my current solution i parse EU date at individualy parts (#day = day(#euDate), #month, #year, ...) and join them in new string . And i compare it with input parameter in pattern and return CASE which is equal like pattern. I want to this function make general and simplier.
Thank you for Your advice.
You almost certainly can use the convert function. You can read more about all the options here.
If there is some obscure invariant you need, check out this blog by Anubhav Goyal.

Conversion of String to int not getting desired format

I want to convert a string to int like this:
CAST(TestSth3.Emp_Code AS int)
I need output as 1234 in crystal report, shows me correctly when i get the output from database, but in Crystal it again converts to 1,234 instead of 1234.
I am using vb.net 2008 and SQL server.
I think you need to try this solution, Replace ',' to '' in the string
Take a look on below code
CAST(REPLACE(TestSth3.Emp_Code, ',', '') AS int) As Clm
it is unclear your question i just point out some facts here
if your string is 1234
then when you try select cast(yournum,int) will give you the correct result
example
select cast('1234'as int) as num
will results
1234
or if your string is something like 1,234
then
try the following it will give results
declare #num varchar(10) ='1,234'
select CONVERT(int, replace(#num,',',''))as number
or
select CAST(replace(#num,',','') AS int)as number2
both results 1234
also you need to double check TestSth3.Emp_Code what is that ?? is it a parameter ??
if yes then need #before that and i think it is not possible to create a parameter name including .(dot) so double check that too.
UPDATE
if the problem based on crystal report then follow below steps
Right mouse click on that field, and select "Format object". Select
"Custom Style" in the Style list, and click "Customize". Untick
"Thousands Separator", and any other unwanted formatting.
Failing that, you could try selecting the field and deleting the ","
value from the property "ThousandSeperator" in your properties window.
or use below formula
CStr(YourField, 0, '')
I think this post summarises the problem much better. #MattWhitfield says:
Formatting numbers for display is something that should be done in the
display layer, and not within the database. So, in whatever
application this data ends up being used, you should format it there.
Management Studio, unfortunately, does not offer much control in this
regard.
Edit: Integers don't have formats. E.g. Six eggs are formatted as eggs, not digits, but they are still 6. The formatting has nothing to do with the number. However if you want to display the integer in a particular format (with or without thousands separator - commas), then convert it back to a string again in the report or VB form, not in the database (or leave it as a string since it's already a string).
Easy way is to use crystal report functions:
ToNumber(TestSth3.Emp_Code)

Specify scale in a ABS function?

Is it possible to specify scale in a ABS function from a decimal ?
= ABS(([Accounting].salary+ABS([Accounting].expenses))/2)
As of right now everything works correctly but there is a problem with results when the number has only 1 number after the decimal point:
100.12 - All ok
230.1 - Not ok, it should be 230.10
I want the outcome to always have two numbers after the decimal point.
The query has to work on both MS SQL Server and Oracle. I can specify the query for each db manually.
To me, this is more a user interface issue than a SQL issue.
230.1 - Not ok, it should be 230.10
Seriously, that's both the same number.
In Oracle you can convert a number into a string in order to present it to an end user.
select to_char(234.1,'$9,999.99') from dual
$234.10
Have a look at format models in the Oracle documentation.
http://docs.oracle.com/cd/B19306_01/server.102/b14200/sql_elements004.htm
Just change code:
= ABS(([Accounting].salary+ABS([Accounting].expenses))/2)
By the following:
= CAST(ABS(([Accounting].salary+ABS([Accounting].expenses))/2) AS DECIMAL(18 /*set your format here instead of 18*/,2))
I've just checked it in code:
SELECT CAST(230.1 AS DECIMAL(18 /*set your format here instead of 18*/, 2))
And all works fine
In fact, it's just a question of representation of your numbers in the UI for the user. You can convert your number to the string with using to_char () with the format specified. But it is better to define the output format on the front-end

Format date like M/D/Y

I got a date of the type SYDATUM and wondering how to format it in a format like m/d/y.
I've found some snippets on the web, but they were not really helpful.
-thanks yor your help.
You should be more specific - what exactly do you want to do with the date (use type D internally, it's shorter and does the same thing).
Do you want to WRITE it to a list screen? Use the formatting options described in the documentation and online help:
WRITE l_my_date MM/DD/YYYY.
Do you want to convert it to a text variable? Very similar:
WRITE l_my_date TO l_my_text MM/DD/YYYY.
To set the date format in a SAPscript form, see the SET DATE MASK command.
To print the formatted date in a SmartForm, use the WRITE command and a temporary variable (yes, ugly, I know...)
Most controls (ALV Grid for example) should take care of the format automatically.
However - be careful when hard-coding the format into your application. Usually you don't have to do this because the system automatically uses the format specified in the user master data. This ensures that every user will see the date formatted according to their locale settings.
Normally it's better to export the date into the plant level country specific date format:
if w_country is initial.
select single LAND1
from T001W
into w_country
where WERKS eq w_the_plant.
endif.
SET COUNTRY w_country.
write w_the_date to w_export.
for example 03/04/2002 could be different date in different country.
You can try the keyword TRANSLATE. Alternatively suggest you could have a look at this link

VisualBasic Month function inconsistency

I'm working in a web application using VB.NET. There is also VisualBasic code mixed in it, in particular the Date variable and the Month function of VB.
The problem is this part:
Month("10/01/2008")
On the servers, I get 10 (October) as the month (which is supposed to be correct). On my machine, I get 1 (January) (which is supposed to be wrong).
Two of my colleagues (on their own machines) get different answers, one got 1, the other got 10.
The question is, why is this so?
On my end, I can solve the problem by using .NET's DateTime's Parse (or ParseExact) function to force everything to be "dd/MM/yyyy" format. This works. I'm just wondering why there's an inconsistency.
Extra info: I know the parameter for Month function is supposed to be a Date variable. The code used a string as parameter, and Option Strict was off, and the developers mainly let VB do its own conversion thing. (Legacy code maintenance has a lot of inertia...)
If it helps, the version of Microsoft.VisualBasic.dll on the servers is 7.10.6310.4 (under the Framework folder v1.1.4322). The version on mine (and my 2 colleagues') machine is 7.10.6001.4.
Edit: Regional settings for all machines already set to dd/MM/yyyy format (short date format).
This normally has to do with the regional settings, and more specifically the date/time formats. If you set these formats so that they are all the same on the machines you're testing on, the results should be consistent.
Your idea of using ParseExact is definitely the better solution to go with, IMHO.
This is because the runtime has to convert your given value "10/01/2008" which is indeed a string implicitly to the DateTime datatype.
When converting strings to dates and the other way round, the string format depends on the locale settings of windows.
See this link on msdn.
In this article a way to specify a date literal which is independent of your locale settings:
Just enclose the date with the sign # and specify it in the form mm/dd/yyyy:
So the code
Month(#10/01/2008#)
should give you the answer 10 on any machine.
Ther a two more worarounds given in that msdn article:
1. Use the Format Function with predifned Date/Time Format
To convert a Date literal to the
format of your locale, or to a custom
format, supply the literal to the
Format Function, specifying either
Predefined Date/Time Formats (Format
Function) or User-Defined Date/Time
Formats (Format Function). The
following example demonstrates this.
MsgBox("The formatted date is " &
Format(#5/31/1993#, "dddd, d MMM
yyyy"))
2. Use the DateTime-Class Constructor to construt the right DateTime value
Alternatively, you can use one of the
overloaded constructors of the
DateTime structure to assemble a date
and time value. The following example
creates a value to represent May 31,
1993 at 12:14 in the afternoon.
Dim dateInMay As New
System.DateTime(1993, 5, 31, 12, 14,
0)