PowerShell date format not accepted in SQL - date

I'm having kind of a strange problem. When the server's Region and Language settings is set to English (United States) there are no issues with objects containing the date and time. But when I change it to my countries local Dutch (Belgium) I am experiencing problems in some of my PowerShell scripts.
Is there somewhere a variable in PowerShell that needs to be set to fix this? Or do I have to default to English date formats on the server all the time?
We use for example the SQL module provided by Don Jones for filling up an SQL database. This line generates an error when it's set to Dutch, but not when it's set to English:
if ($properties[$property] -eq 'datetime2') { [datetime]$prop = $result.GetDateTime($result.GetOrdinal($property)) }
The only thing I do to retrieve the date is Get-Date without anything special. And it generates this error when set to Dutch (Belgium):
Exception calling "ExecuteNonQuery" with "0" argument(s): "Conversion failed when converting date and/or time from char
acter string."
I've experienced the same problem when generating stuff for Excel sheets. It also complains about the date time format.

For exporting information like DateTime fields to SQL, the default language setting is English (United States).
So when your Region and Language settings are different from the default for SQL, being: English (United States) <> Dutch (Belgium), you should use the following format in your code to comply to the English defaults:
Get-Date -Format 'yyyy-MM-dd HH:mm:ss'
$_.whenCreated.ToString('yyyy-MM-dd HH:mm:ss')
Another solution is to change the default language for input in SQL Express 2014:
Right click the server instance and select Properties > Advanced
Change Default language to Dutch (or what you use)
Confirm with Ok
Still in SQL Management Studio go to Security > Logins (below Databases)
Double click your user name and select Dutch below as Default language
Restart SQL Management Studio and you should be good to go
Result:
When you use the CmdLet Get-Date in your PowerShell code it will work as designed when transferring data to SQL. The system language for SQL and Windows is still English but the formatting used is Dutch.

I use this for Datetime2 in SQL Server.
Function Get-Datetime2([datetime]$Date = $(Get-Date) ) {
$DT = Get-Date -Date $Date
[string]$DateTime2 = $DT.Month.ToString() + '/'
[string]$DateTime2 += $DT.Day.ToString() + '/'
[string]$DateTime2 += $DT.Year.ToString() + ' '
[string]$DateTime2 += $DT.TimeOfDay.ToString()
return $DateTime2
}
Get-Datetime2
returns something that looks like this.
3/12/2018 03:04:34.7496659

If you already use Don Jones' module you may already be familiar with type-extension through the *.types.ps1xml... There should be one in his module too, if I remember correctly.
When adding the following type declaration,
<Type>
<Name>System.DateTime</Name>
<Members>
<ScriptMethod>
<Name>ToString</Name>
<Script>
$(Get-Date $This -Format "yyyy-MM-dd HH:mm:ss")
</Script>
</ScriptMethod>
</Members>
</Type>
You basically override System.DateTime's ToString() method to the format needed by a standard SQL Server installation.
This way you can make sure that every time you load the SQL module dates they are being formatted the right way.

Related

Why is Powershell Write-Output Date Format not the System Setting?

We have a Windows Server with the Region settings for short dates set to dd.MM.yyyy. However powershell outputs the dates as MM/dd/yyyy:
$d = (Get-Item .\somefile.txt).CreationTime
Write-Output "$d" # => 09/26/2016 15:35:35
Also, the toString() function returns a different (correct) format
Write-Output "$($d.toString())" # => 26.09.2016 15:35:35
Questions:
Why does powershell use MM/dd/yyyy?
Why are the 2 formats above different?
I know we can set the format in our powershell profile but is there no "System" setting which determines it?
Scripts are often used for automation, rarely for interaction with users or creating UIs. The automatic conversion to string that happens when you put a variable inside a string, e.g. "$d" will always use the invariant culture and never the user's preference. Same goes for numbers, for example. This is precisely to avoid issues that arise where a string would contain a different format for a different user or on a different machine.
If you need control over the format, convert to string explicitly, not implicitly.
The same holds for parsing, incidentally. You can cast a string to a datetime, or number, but this requires a certain format to work. If you want to use the user's preference, then use [DateTime]::Parse instead.

Classic ASP return dd/mm/yyyy when append with string

Please see the issue details and share the solution if you have. Thank you in advance.
Issue: Windows 2012/IIS 8.5 Classic ASP date format changing when we append with string. For example, date value 01/12/2016 (Jan 12th 2016) have been changed to 12/01/2016 (Dec 1st 2016) if we append with the string.
Source Code: (date.asp)
<%
Dim fordate
Dim fdate
fordate = Request.QueryString("fordate")
Response.Write(fordate)
fdate = cdate(fordate)
Response.Write(fdate)
Dim strsql
strsql = "exec testsp " & fdate
Response.Write(strsql)
%>
When casting Date / Time to a String in VBScript the following applies
Quote from MSDN - VBScript Reference - CStr Function
A String containing a date in the short-date format of your system
The effect by default whatever the "short-date" format settings are for dates values on the system# (usually found under Control Panel -> Language but varies by OS) is used when displaying String representations of date values in VBScript.
# - By "System" we mean the Server / Computer whether it be in a client / server side environment.
Useful Links
Format current date and time

Change currency sign oracle d2k reports?

I want to replace $ sign to 'Rs.' in oracle d2k reports. In some system it is displaying Rs but in some system it is showing $. From where I have to change the sign.
You can use the currency in your NLS_TERRITORY settings as follows:
select to_char(123456789.91, 'L999,999,999,990.00') from dual;
L999,999,999,990.00 is the format mask you may be able to set in the property sheet (it's a while since I used Reports) or you can use a sql function like in the example above.
Or you can take the date and format it as a string (as above) and concatenate with the character you want to display. Obviously this isn't as flexible.
select 'Rs'||to_char(123456789.91, '999,999,999,990.00') from dual;
You can check your nls_settings by connecting in sqlplus
SELECT * FROM nls_session_parameters;
You can use this code as well.
SELECT TO_CHAR
(-10000,
'L99G999D99MI',
'NLS_NUMERIC_CHARACTERS = '',.''
NLS_CURRENCY = ''RS'' '
) "Amount"
FROM DUAL;

How do I check if a date in format "yyyymmdd" is a weekday or not?

My program accepts date in the format of "yyyymmdd", I don't know how to check if it's a weekday or not. I've seen ppl using $(date +%u) -gt 5 or "$(date +%a)" in Sat|Sun echo "weekend" in other threads, but that ${date} is like Tue Nov 22 14:16:35 EST 2011 I guess. So is there a good way to convert "yyyymmdd" to ${date} format? Or is there a simple way to check if "yyyymmdd" is a weekday or not? Any language is fine. Thanks.
Any language? In Java I'd use SimpleDateFormat or Joda Time's DateTimeFormatter. In C# I'd either use DateTime.ParseExact or Noda Time's LocalDatePattern. In all of these cases, the result is a value which can be asked for things like the day of the week.
In Python I suspect you want datetime.strptime, e.g.
date = datetime.strptime(text, "%Y%m%d")
day = date.weekday()
if day < 5 # Monday(0) to Friday(4)
# Do something here
This is completely untested, however...
Normally, in a typed language (like Java, C# or Python), you'd 1) initialize your "time" to a language-specific "timedate" type, then 2) get the 'day-of-week" from some method of that time.
It looks like you're using Bourne shell. Which is an untyped language, with no native date/time functions.
Here's one possible solution:
http://unix.ittoolbox.com/groups/technical-functional/shellscript-l/day-of-week-bash-script-2018531
Here are several time/timing related things you can do in the (Linux) Bourne shell:
http://tldp.org/LDP/abs/html/timedate.html

Internationalized date formatting with Zend_Date ( Japanese )

Disclaimer: you might need to install
a font/typeface which supports
Japanese if you see messed up
characters.
I'm trying to replicate what I've been doing so far with setlocale and strftime:
setlocale(LC_ALL, 'ja_JP.utf8');
$time = mktime();
echo strftime('%x', $time), '<br>';
Output:
2010年01月06日
Using Zend_Date - but I haven't been able to reproduce the same formatting with the japanese symbols for year, month and day.
Attempt #1:
$locale = new Zend_Locale('ja_JP');
$date = new Zend_Date( strtotime('yesterday'), null, $locale);
//echo $date->toString('YYYY abcdefghijklmnopqrstuvwxy M dE');
echo $date->get('YYYY MMM DD');
Output:
2010 1月 004
Attempt #2:
echo $date->get(Zend_Date::DATE_FULL);
Output:
2010年1月5日火曜日
My first attempt I can't seem to find a working constant to produce the YEAR and day symbols. The latter uses a standardized format but I need to customize it so there's a 0 preceding the month, and I want to be more in control.
In the future I may want to make it flexible so for example, en_US dates won't have those letters coming after the year/month/day but it would only apply to languages such as Japanese and others, where it's more common, or if I misunderstood and it isn't really common then please inform me.
Thanks in advance.
Seems what I needed was the DATE_LONG constant, which internally points to 'FFFF' - I'm trying to learn the inner workings of how the Date class corresponds with the Locale class to generate the whole string including the symbols now.
Update: I kept trying to find where it actually used date units instead of date formats, found the right data I need:
<dateFormatLength type="long">
<dateFormat>
<pattern>y年M月d日</pattern>
</dateFormat>
</dateFormatLength>
So it parses this and replaces the y, M, d, returns the formatted date.